rickysu/tagcache-bundle

此包提供带标签的缓存和控制器缓存

安装数: 18,557

依赖关系: 1

建议者: 0

安全性: 0

星标: 7

关注者: 3

分支: 4

公开问题: 0

类型:symfony-bundle

v0.3.2 2017-03-21 01:47 UTC

This package is auto-updated.

Last update: 2024-09-14 18:25:51 UTC


README

简介

此包提供带标签的缓存。

特性

  • 存储带多个标签的缓存。并通过标签删除缓存。
  • 提供控制器缓存。

要求

  • 控制器注解。

安装

编辑根项目中的 composer.json 文件。

在 require: {} 部分编辑 composer.json 添加

"rickysu/tagcache-bundle": "1.0.*",

更新包

php composer.phar update

实例化包

<?php
//app/AppKernel.hpp
public function registerBundles()
{
   $bundles = array(
        // ...
        new RickySu\TagcacheBundle\TagcacheBundle(),
   );
}

配置

配置缓存适配器

# app/config/config.yml
tagcache:
    driver:                     Memcache
    debug:                      %kernel.debug%
    options:
        hashkey:                true
        enable_largeobject:     false
        cache_dir:              "%kernel.cache_dir%/tagcache"
        servers:
            -    'localhost:11211:10'
            -    'otherhost:11211:20'
# app/config/config_dev.yml
tagcache:
    namespace:                  'Name_Space_For_Your_Project:dev'
# app/config/config_prod.yml
tagcache:
    namespace:                  'Name_Space_For_Your_Project:prod'

driver

缓存驱动。目前支持 "Memcache,Memcached,File,Sqlite,Apc,Nullcache"。Nullcache 仅用于测试。

hashkey

一些驱动,如 Memcached,仅支持 250 个字符的键长度。启用此选项将使用 md5 哈希键。

enable_largeobject

Memcache 无法存储超过 1MB 的对象。启用这些选项将修复此问题,但会导致性能降低。默认为 false。

servers

Memcache 服务器配置。格式 => "Host:Port:Weight"

如何使用

使用 Tagcache 存储缓存数据。

<?php
$Tagcache=$container->get('tagcache');

//store cache with Tags:{TagA,TagB} for 300 secs.
$Tagcache->set('Key_For_Store','Data_For_Store',array('TagA','TagB'),300);

//get cache.
$Tagcache->get('Key_For_Store');

//delete cache.
$Tagcache->delete('Key_For_Store');

//delete cache by Tag.
$Tagcache->deleteTag('TagA');

//acquire a lock.If a lock already exists,It will be blocked for 5 secs.
$Tagcache->getLock('Your_Lock_Name',5);

//release a lock.
$Tagcache->releaseLock('Your_Lock_Name');

//increment a cache
$Tagcache->inc('Key_For_increment');

//decrement a cache
$Tagcache->dec('Key_For_decrement');

控制器缓存

控制器设置

<?php
//in Controller
namespace Acme\DemoBundle\Controller;

// these import the "@Tagcache" annotations
use RickySu\TagcacheBundle\Configuration\Tagcache;

class DemoController extends Controller
{
    /**
     * @Route("/hello/{name}", name="_demo_hello")
     * @Tagcache(expires=600,cache=true)
     * @Template()
     */
    public function helloAction($name)
    {
        return array('name' => $name);
    }

    /**
     * @Route("/test", name="_demo_test")
     * @Tagcache(expires=600,tags={"TagA","TagB"},key="custom_cache_key",cache=true)
     * @Template()
     */
    public function testAction()
    {
        return;
    }
}

视图设置(Twig) (针对 Symfony 2.1)

{#in view render a controller#}
{%render 'AcmeDemoBundle:Demo:test' with {
     'tagcache':    {
           'key':    'custom_cache_key',
           'tags':   ['TagA','TagB'],
           'expires': 300
     }
}%}

视图设置(Twig) (针对 Symfony 2.2)

{#in view render a controller#}
{%render(
    controller(
        'AcmeDemoBundle:Demo:test',
        {
            'tagcache':    {
                'key':    'custom_cache_key',
                'tags':   ['TagA','TagB'],   
                'expires': 300
            }
        }
    )
)%}

您熟悉的局部缓存回来了

first access

cached access

清除缓存

app/console cache:clear

注意

如果同时定义了视图和控制器中的缓存参数。视图中的 "tagcache" 变量将覆盖控制器注解。但请记住,如果想要关闭控制器缓存,控制器注解配置中的 "cache" 必须设置为 false。

待办事项

许可协议

MIT