winzou/cache-bundle

此包已被废弃且不再维护。作者建议使用doctrine/doctrine-cache-bundle包。

此包为Symfony2提供简单的缓存管理。现在您可以使用缓存系统,无需重新发明。它支持Apc、XCache、File、ZendData和Array。

安装数: 28,951

依赖关系: 0

建议者: 0

安全: 0

星标: 30

关注者: 5

分支: 10

开放问题: 5

类型:symfony-bundle

1.0 2013-02-18 17:02 UTC

This package is auto-updated.

Last update: 2022-02-01 12:21:39 UTC


README

该包已弃用。请使用https://github.com/doctrine/DoctrineCacheBundle代替。

这是什么?

winzouCacheBundle提供简单的缓存管理。现在您可以使用缓存系统,无需重新发明。它支持Apc、XCache、File、ZendData和Array。

安装

1. 将此包添加到您的项目中

使用composer

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

"require": {
    ...
    "winzou/cache-bundle": "dev-master"
}

现在,运行composer以下载此包

$ composer update

2. 将此包添加到您的应用程序内核中

<?php
// app/AppKernel.php
public function registerBundles()
{
  $bundles = array(
      // ...
      new winzou\CacheBundle\winzouCacheBundle(),
      // ...
  );
}

使用方法

在您的控制器中

$cache = $this->get('winzou_cache.apc');
// or
$cache = $this->get('winzou_cache.file');
// or
$cache = $this->get('winzou_cache.memcache');
// or
$cache = $this->get('winzou_cache.array');
// or
$cache = $this->get('winzou_cache.xcache');
// or
$cache = $this->get('winzou_cache.zenddata');
// or
$cache = $this->get('winzou_cache'); // in that case, it will use the default driver defined in config.yml, see below

$cache->save('bar', array('foo', 'bar'));

if ($cache->contains('bar')) {
    $bar = $cache->fetch('bar');
}

$cache->delete('bar');

查看Cache\AbstractCache以获取所有可用方法。

配置

当使用FileCache时,如果您不想将缓存文件存储在%kernel.cache_dir%/winzou_cache(默认值),则可以在您的config.yml中定义绝对路径

winzou_cache:
    options:
        cache_dir: %kernel.cache_dir%/MyAppCache
# or    cache_dir: /tmp/MyAppCache/%kernel.environment%

如果您只想在一个地方定义要使用的驱动程序,您会喜欢default_driver选项

winzou_cache:
    options:
        default_driver: apc # default is "lifetimefile"

# and then $cache = $this->get('winzou_cache')

您现在可以通过winzou_cache服务访问ApcCache。如果您想更改驱动程序,您只需在您的config.yml中修改一个值。

如果您未定义default_driver并使用$this->get('winzou_cache'),则您正在使用FileCache。

原始访问

您可以通过使用工厂服务来覆盖任何选项。查看这两个非常类似的方法

$factory = $this->get('winzou_cache.factory');
$cache = $factory->getCache('file', array('cache_dir' => '/tmp/cache'));

或者通过定义您自己的服务

your_cache:
    factory_service: winzou_cache.factory
    factory_method:  get
    class:           %winzou_cache.driver.abstract%
    arguments:
        - file                       # just modify this value to use another cache
        - {'cache_dir': /tmp/cache } # you can omit this if you don't use FileCache or if the default value is ok for you

# and then $cache = $this->get('your_cache')