cache组件为添加cache到应用中提供了一个严格的psr-6实现.低开销以及即将为最流行的后台缓存使用适配器.
key concepts(概念)
Before starting to use the cache component, it's important that you learn the meaning of
some key concepts:
item
一个信息存储的独立单元,作为一个key/value对,key是唯一定义的,value是key的内容
pool
缓存的一个逻辑仓库.所有的cache操作,(保存,查看等)都是通过pool展示.应用可以定义足够多的你需要的
pool.
adapter
它实现了真正的cache机制,用于在文件系统,数据库等等中存储信息.这个component为常用的cache后台提供了准备
使用的adapter.
Basic Usage
该组建严格实现psr-6,意味着它的api一同样的标准定义.before starting to cache information, create the
cache pool using any of the built-in adapters. for example, to create a filesystem-based cache, instantiate(实例化) FilesystemAdapter:
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter();
现在,你可以使用cache pool创建,检索,更新以及删除元素:
// create a new item by trying to get it from the cache
$numProducts = $cache->getItem('stats.num_products');
// assign a value to the item and save it
$numProducts->set(4711);
$cache->save($numProducts);
// retrieve the cache item
$numProducts = $cache->getItem('stats.num_products');
if (!$numProducts->isHit()) {
// ... item does not exists in the cache
}
// retrieve the value stored by the item
$total = $numProducts->get();
// remove the cache item
$cache->deleteItem('stats.num_products');
Cache Items
缓存项目是以键值对的方式被存储在告诉缓存中的信息单元.在Cache模块,他们通过CacheItem类来代表,
Cache item keys and values
cache item的key是utf-8编码的字符串,因此它对每一个cache pool都必须是唯一的.你可以自由的选择keys,
但是他们只能包含(A-Z,a-z),数字0到9和_和.symbols(符号).
cache item的value可以是任意被php序列化的类型代表,列入基本类型,和arrays和objects
creating cache items
通过cache pool的getItem($key)方法创建.参数是item的key.
// $cache pool object was created before
$numProducts = $cache->getItem('stats.num_products');
然后通过set()方法在cache item中储存数据:
// storing a simple integer
$numProducts->set(4711);
$cache->save($numProducts);
// storing an array
$numProducts->set(array(
'category1' => 4711,
'category2' => 2387,
));
$cache->save($numProducts);
cache item给出的任意key和value可以通过对应的getter方法被获取:
$cacheItem = $cache->getItem('exchange_rate');
// ...
$key = $cacheItem->getKey();
$value = $cacheItem->get();
cache item到期
默认的cache item是被永久存储的,在实践中,这个永久存储根据缓存类型的使用而产生很大的改变,
如在告诉缓存池解释的.
然而,在一些应用中,使用短生命周期的cache是非常普遍的.考虑一种情况,一个缓存最新的新闻只保留一
分钟的引用.在这些情况中,使用expiresAfter()方法去给cache设置seconds值
$latestNews = $cache->getItem('latest_news');
$latestNews->expiresAfter(60); // 60 seconds = 1 minute
// 这个方法还可以接受\DateInterval实例
$latestNews->expiresAfter(DateInterval::createFromDateString('1 hour'));
cache items定义了另一个相关的方法:expiresAt(),用来设置item即将过期的准确的日期和时间
$mostPopularNews = $cache->getItem('popular_news');
$mostPopularNews->expiresAt(new \DateTime('tomorrow'));
cache item hits and misses
使用缓存机制对提高应用的性能非常的重要,但是它不应该被应用与应用的工作.事实上,psr-6标准规定caching errors
不会导致程序故障.
实践中,这意味着getItem()方法总是返回一个实现了Psr\Cache\CacheItemInterface接口的对象,甚至cache item不存在的时候也是如此.therefor,你不需要处理空的返回结果并且你可以很安全的存储类似false和null之类
的值.
为了判断返回的对象是否正确,caches使用了hits和misses观点:
cache hits当在缓存中找到请求项的时候出现,它的值没有被破坏或者失效并且还没有过期.
cache misses是和hits相反的,所以他们是当在缓存中未找到item的时候出现,它的值是被破坏的或者
因为任何一个理由失效的或者item已经过期的.
cache item对象定义了一个为hits返回布尔类型的方法isHit():
$latestNews = $cache->getItem('latest_news');
if (!$latestNews->isHit()) {
// do some heavy computation
$news = ...;
$cache->save($latestNews->set($news));
} else {
$news = $latestNews->get();
}
Cache Pools
cache pools是cache items的逻辑仓库.他们执行所有的在items上通用的操作,例如保存item或者查找
item.cache pools are independent from 显示的缓存实现.因此,应用可以不断使用cache pool,
即使潜在的缓存机制从一个基础的缓存文件系统改变为redis或者数据库基础缓存.
creating cache pools
cache pools通过cache适配器创建,实现了AdapterInterface接口的类.这个组件提供了几个给程序使用的
适配器.
Array Cache Adapter
这个适配器仅仅有用于测试目的,因为内容被存储在内存中并且不已任何方式支持.除此之外,一些解释之后的特性是
不能使用的,例如:延迟保存:
Filesystem Cache Adapter
这个适配器当你想提高应用的性能的时候是游泳的,但是不能安装像APC或者redis的工具在你的服务器中,此
适配器将内容存储在本地文件系统上的一组目录中的常规文件中.
.....
key concepts(概念)
Before starting to use the cache component, it's important that you learn the meaning of
some key concepts:
item
一个信息存储的独立单元,作为一个key/value对,key是唯一定义的,value是key的内容
pool
缓存的一个逻辑仓库.所有的cache操作,(保存,查看等)都是通过pool展示.应用可以定义足够多的你需要的
pool.
adapter
它实现了真正的cache机制,用于在文件系统,数据库等等中存储信息.这个component为常用的cache后台提供了准备
使用的adapter.
Basic Usage
该组建严格实现psr-6,意味着它的api一同样的标准定义.before starting to cache information, create the
cache pool using any of the built-in adapters. for example, to create a filesystem-based cache, instantiate(实例化) FilesystemAdapter:
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter();
现在,你可以使用cache pool创建,检索,更新以及删除元素:
// create a new item by trying to get it from the cache
$numProducts = $cache->getItem('stats.num_products');
// assign a value to the item and save it
$numProducts->set(4711);
$cache->save($numProducts);
// retrieve the cache item
$numProducts = $cache->getItem('stats.num_products');
if (!$numProducts->isHit()) {
// ... item does not exists in the cache
}
// retrieve the value stored by the item
$total = $numProducts->get();
// remove the cache item
$cache->deleteItem('stats.num_products');
Cache Items
缓存项目是以键值对的方式被存储在告诉缓存中的信息单元.在Cache模块,他们通过CacheItem类来代表,
Cache item keys and values
cache item的key是utf-8编码的字符串,因此它对每一个cache pool都必须是唯一的.你可以自由的选择keys,
但是他们只能包含(A-Z,a-z),数字0到9和_和.symbols(符号).
cache item的value可以是任意被php序列化的类型代表,列入基本类型,和arrays和objects
creating cache items
通过cache pool的getItem($key)方法创建.参数是item的key.
// $cache pool object was created before
$numProducts = $cache->getItem('stats.num_products');
然后通过set()方法在cache item中储存数据:
// storing a simple integer
$numProducts->set(4711);
$cache->save($numProducts);
// storing an array
$numProducts->set(array(
'category1' => 4711,
'category2' => 2387,
));
$cache->save($numProducts);
cache item给出的任意key和value可以通过对应的getter方法被获取:
$cacheItem = $cache->getItem('exchange_rate');
// ...
$key = $cacheItem->getKey();
$value = $cacheItem->get();
cache item到期
默认的cache item是被永久存储的,在实践中,这个永久存储根据缓存类型的使用而产生很大的改变,
如在告诉缓存池解释的.
然而,在一些应用中,使用短生命周期的cache是非常普遍的.考虑一种情况,一个缓存最新的新闻只保留一
分钟的引用.在这些情况中,使用expiresAfter()方法去给cache设置seconds值
$latestNews = $cache->getItem('latest_news');
$latestNews->expiresAfter(60); // 60 seconds = 1 minute
// 这个方法还可以接受\DateInterval实例
$latestNews->expiresAfter(DateInterval::createFromDateString('1 hour'));
cache items定义了另一个相关的方法:expiresAt(),用来设置item即将过期的准确的日期和时间
$mostPopularNews = $cache->getItem('popular_news');
$mostPopularNews->expiresAt(new \DateTime('tomorrow'));
cache item hits and misses
使用缓存机制对提高应用的性能非常的重要,但是它不应该被应用与应用的工作.事实上,psr-6标准规定caching errors
不会导致程序故障.
实践中,这意味着getItem()方法总是返回一个实现了Psr\Cache\CacheItemInterface接口的对象,甚至cache item不存在的时候也是如此.therefor,你不需要处理空的返回结果并且你可以很安全的存储类似false和null之类
的值.
为了判断返回的对象是否正确,caches使用了hits和misses观点:
cache hits当在缓存中找到请求项的时候出现,它的值没有被破坏或者失效并且还没有过期.
cache misses是和hits相反的,所以他们是当在缓存中未找到item的时候出现,它的值是被破坏的或者
因为任何一个理由失效的或者item已经过期的.
cache item对象定义了一个为hits返回布尔类型的方法isHit():
$latestNews = $cache->getItem('latest_news');
if (!$latestNews->isHit()) {
// do some heavy computation
$news = ...;
$cache->save($latestNews->set($news));
} else {
$news = $latestNews->get();
}
Cache Pools
cache pools是cache items的逻辑仓库.他们执行所有的在items上通用的操作,例如保存item或者查找
item.cache pools are independent from 显示的缓存实现.因此,应用可以不断使用cache pool,
即使潜在的缓存机制从一个基础的缓存文件系统改变为redis或者数据库基础缓存.
creating cache pools
cache pools通过cache适配器创建,实现了AdapterInterface接口的类.这个组件提供了几个给程序使用的
适配器.
Array Cache Adapter
这个适配器仅仅有用于测试目的,因为内容被存储在内存中并且不已任何方式支持.除此之外,一些解释之后的特性是
不能使用的,例如:延迟保存:
Filesystem Cache Adapter
这个适配器当你想提高应用的性能的时候是游泳的,但是不能安装像APC或者redis的工具在你的服务器中,此
适配器将内容存储在本地文件系统上的一组目录中的常规文件中.
.....