cscfa/cache-system-bundle

CacheSystemBundle 允许将信息文件存储到应用程序缓存目录中,并自动管理过时的值。

dev-master 2015-12-17 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:17:14 UTC


README

版本 1.0.1-dev

cscfa 缓存系统工具允许将信息文件存储到应用程序缓存目录中,并自动管理过时的值。

#####安装

将捆绑包注册到 app/appKernel.php

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            [...]
            new Cscfa\Bundle\CacheSystemBundle\CscfaCacheSystemBundle(),
        );
        
        [...]
    }
}

#####使用管理器

缓存管理器允许通过闭包处理缓存存储。如果存在缓存,它将自动使用缓存;如果没有,它将使用闭包的结果创建缓存。

// in controller

	return new Response($this->get("cscfa_cache_system_manager")->process("id@key", function($controller, $name){
		return $controller->renderView("AcmeDemoBundle:Default:index.html.twig", array("name"=>$name)); 
	}, $this, $name));
	

#####获取缓存

主缓存系统注册为 'cscfa_cache_system_cache' ID 的服务。它用于获取缓存提供者。

缓存对象返回的提供者实现了 CacheProviderInterface,并使用配置参数 'cscfa_cache_system.provider' 初始化。注意,默认情况下已设置文件系统提供者。

// in controller

	//Cscfa\Bundle\CacheSystemBundle\Object\Cache
	$cache = $this->get("cscfa_cache_system_cache");
	
	//Cscfa\Bundle\CacheSystemBundle\Object\provider\CacheProviderInterface
	$provider = $cache->getProvider();

#####使用缓存

缓存使用缓存 ID 来选择一组缓存元素。每个元素可以通过其键进行选择。这些元素具有内容和过时元素。

在检索缓存集合时,自动删除过时的元素。

	//Cscfa\Bundle\CacheSystemBundle\Object\Element\CacheCollection
	$collection = $provider->get("cacheId");
	
	// create a cache key with a content :
	$collection->create("20151216", "cache key creation");
	
	if ($collection->has("20151216")) {
		//Cscfa\Bundle\CacheSystemBundle\Object\Element\CacheElement
		$element = $collection->get("20151216");
	}
	
	// persist cache collection
	$provider->save($collection);
	
	/* 
	 * note the element is soft removed as long as 'save' is not called
	 * and it will be returned at the next get('id') request 
	 * (while the out of time is not reached)
	 */ 
	$collection->rem("20151216");

#####配置捆绑包

配置文件可以按照以下方式编写

// app/config

cscfa_cache_system:
	provider:  'provider complete class (Cscfa\Bundle\CacheSystemBundle\Object\provider\FileSystemCache as default)'
	prefix:    'string (null as default)'
	timestamp: integer (null as default)

'provider' 指示要使用的提供者类。任何实现 CacheProviderInterface 的类都可以使用。

'prefix' 指示一个缓存前缀。该前缀的使用取决于提供者。例如,FileSystemCache 使用它来在缓存目录中定义一个特定的存储库。

'timestamp' 指示在使缓存元素无效之前的时间(秒)数。