Db缓存策略实现
Db缓存允许用户配置一个缓存数据表,将缓存数据保存到缓存表中. 提供对方访问接口如下: 它接收如下配置: array(
'table-name' => 'pw_cache', //缓存的表名
'field-key' => 'key', //缓存key的字段名,唯一键值
'field-value' => 'value', //缓存数据存储的字段名
'field-expire' => 'expire', //缓存数据的过期时间字段名,为int类型,默认为0
'security-code' => '', //继承自AbstractWindCache,安全码配置
'key-prefix' => '', //继承自AbstractWindCache,缓存key前缀
'expires' => '0', //继承自AbstractWindCache,缓存过期时间配置
)
Db缓存的使用: 1、像使用普通类库一样使用该组件:
$cache = new WindDbCache($dbHandler, array('table-name' => 'pw_cache', 'field-key' => 'key', 'field-value' => 'value', 'field-expire' => '0'));
$cache->set('name', 'windDbTest');
<note>注意:需要传入dbHandler(数据库链接对象)</note> 2、采用组件配置的方式,通过组件机制调用 在应用配置的components组件配置块中,配置dbCache(该名字将决定调用的时候使用的组件名字): 'dbCache' => array(
'path' => 'WIND:cache.strategy.WindDbCache',
'scope' => 'singleton',
'properties' => array(
'connection' => array('ref' => 'db');
),
'config' => array(
'table-name' => 'pw_cache',
'field-key' => 'key',
'field-value' => 'value',
'field-expire' => 'expire',
'security-code' => '',
'key-prefix' => '',
'expires' => '0',
),
), 在应用中可以通过如下方式获得dbCache对象: $cache = Wind::getApp()->getComponent('dbCache'); //dbCache的名字来自于组件配置中的名字
$cache->set('name', 'test');
<note>注意:组件配置需要配置属性(preperties),connection其值为db组件的一个引用</note>the last known user to change this file in the repository <LastChangedBy: xiaoxiao >
Located in /cache/strategy/WindDbCache.php [line 71]
WindModule
|
--AbstractWindCache
|
--WindDbCache
Author(s):
Information Tags:
|
Properties
|
Methods
|