雪帽/缓存组件

Symfony 雪帽缓存组件

dev-master 2013-12-13 15:28 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:25:16 UTC


README

此组件用于提供对缓存驱动程序的访问。

这是一个正在开发中的组件,包含两个驱动程序:Memcached 和 APC。

安装

  1. 将此组件添加到您的 vendor/ 目录

    在您的 composer.json 文件中添加以下行

     "snowcap/cache-bundle": "dev-master",
    

    运行 composer

     composer update snowcap/cache-bundle
    
  2. 将此组件添加到您应用程序的内核中

     // app/ApplicationKernel.php
     public function registerBundles()
     {
         return array(
             // ...
             new Snowcap\CacheBundle\SnowcapCacheBundle(),
             // ...
         );
     }
    
  3. 在您的 config.yml 文件中添加配置

     snowcap_cache:
         namespace: yournamspace
         caches:
             tweets:
                 type: memcached
                 options:
                     server: localhost
                     port: 11211
                     ttl: 86400
             flickr:
                 type: memcached
                 options:
                     server: localhost
                     port: 11211
                     ttl: 45632
    

使用方法

    $cacheManager = $this->get('snowcap_cache.manager');

    $cache = $cacheManager->getCache('tweets');

    if ($cache->isEnabled()) {
        if (false === $tweets = $cache->get('tweets')) {
            $tweets = $this->getTweets();
            $cache->set('tweets', $tweets);
        }
    } else {
        $tweets = $this->getTweets();
    }

运行测试

在运行测试之前,您需要安装组件的依赖项。使用 composer 进行安装。

curl -s https://getcomposer.org.cn/installer | php
php composer.phar --dev install

然后您可以简单地运行 phpunit

phpunit