查看了Php的缓存机制已经S函数,其实S函数的缓存他是直接调用Cache(thinkphp3.2版本中的类在核心目录ThinkPHP/Library/Think/Cache.class.php,对应的功能库在ThinkPHP/Library/Think/Cache/Driver中)这个类库中的东西的。
我们从S函数分析ThinkPHP框架缓存机制,像快速缓存、查询缓存、SQL解析缓存、静态缓存根本上都是在这种机制下运行,只是切入方式不同。
首先,看下S函数定义:
<pre name="code" class="php"><?php
/**
* 缓存管理
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
* @param mixed $value 缓存值
* @param mixed $options 缓存参数
* @return mixed
*/
function S($name, $value = '', $options = null) {
static $cache = '';
if (is_array($options) && empty($cache)) {
# 缓存操作的同时初始化
$type = isset($options['type']) ? $options['type'] : '';
$cache = Think\Cache::getInstance($type, $options);
} elseif (is_array($name)) { // 缓存初始化
$type = isset($name['type']) ? $name['type'] : '';
$cache = Think\Cache::getInstance($type, $name);
return $cache;
} elseif (empty($cache)) { // 自动初始化
$cache = Think\Cache::getInstance();
}
if ('' === $value) { // 获取缓存
return $cache->get($name);
} elseif (is_null($value)) { // 删除缓存
return $cache->rm($name);
} else { // 缓存数据
if (is_array($options)) {
$expire = isset($options['expire']) ? $options['expire'] : NULL;
} else {
$expire = is_numeric($options) ? $options : NULL;
}
return $cache->set($name, $value, $expire);
}
}
?>
这个函数封装了缓存的创建、获取、删除操作,这部分功能很清新,关键在创建cache对象的时候函数调用了缓存基类:Think\Cache::getInstance 静态方法,通过这个方法框架支持:File、Db、Apc、Memcache、Shmop、Sqlite、Xcache、Apachenote、Eaccelerator不同类型的缓存方式,为了弄清楚支持这些缓存类型的代码结构,那么我们把这个方法代码看下:
/**
* 取得缓存类实例
* @static
* @access public
* @return mixed
*/
static function getInstance($type = '', $options = array()) {
static $_instance = array();
$guid = $type . to_guid_string($options);
if (!isset($_instance[$guid])) {
$obj = new Cache();
$_instance[$guid] = $obj->connect($type, $options);
}
//print_r($_instance);
return $_instance[$guid];
}
该方法调用了本类connect方法,在看下代码:
/**
* 连接缓存
* @access public
* @param string $type 缓存类型
* @param array $options 配置数组
* @return object
*/
public function connect($type = '', $options = array()) {
if (empty($type))
$type = C('DATA_CACHE_TYPE');
$class = strpos($type, '\\') ? $type : 'Think\\Cache\\Driver\\' . ucwords(strtolower($type));
if (class_exists($class))
$cache = new $class($options);
else
E(L('_CACHE_TYPE_INVALID_') . ':' . $type);
return $cache;
}
真正的区别缓存类型的代码在这里了,“Think\\Cache\\Driver\” 定义了框架支持的缓存类型,每种支持的缓存类型的文件在这里就是一个类文件。如果,没有定义缓存类型,框架获取了配置文件中“ C('DATA_CACHE_TYPE')”的默认值,我们再看下框架默认定义的缓存类型是什么:'DATA_CACHE_TYPE' => 'File', 很明显,框架默认使用的是File类型缓存机制。当然,弄清楚这样的代码框架,那天有兴趣我们自己也能定义一款更适用项目的缓存类型,不是嘛!!
有很多编程大神并不喜欢使用ThinkPHP提供的S等封装的函数,更喜欢直接使用Cache\Driver下的类,通过上面的了解,我们也可以试试吧^_^!
下面是关于cache的基本用法
注意:首先这个代码是写在控制器中的!
$cache=Cache::getInstance('File');就是开启缓存的标志
$cache->set('username','孙亚龙',10); //设置一个变量username叫孙亚龙,在10秒内缓存起来
$cache->get('username'); //获得缓存起来的username变量
serialize("数组"),将一个数组序列化成字符串(为什么要序列化呢?就是因为字符串比数组咱内存小)
unserialize('被序列化的数组');反序列化,将字符串有反序列化成数组
class CacheAction extends Action {
function cache() {
$cache = Cache::getInstance('File');
$cache->set('username', '孙亚龙', 10); //设置了username这个变量,缓存在Runtime目录的temp目录下,缓存时间是10秒
//echo $cache->get('username');
$cache->set('people', array('name' => 'xiaobai', 'age' => '23', 'gender' => 'wan'), 12);
$this->display();
}
function test() {
$cache = Cache::getInstance('File');
echo "注意十秒后刷新页面,同时看着runtime/temp的缓存文件的存在与消失" . "<br />";
echo $cache->get('username'); //获取缓存起来的变量username
dump($cache->get('people'));
}
function rm() {
$cache = Cache::getInstance('File');
$cache->rm('people'); //rm删除缓存变量
}
function update() {
$cache = Cache::getInstance('File');
$cache->set('username', 'sunyalong', 10); //修改缓存值相当于重新给username赋值
}
function add() {
$arr = array('name' => 'syl', 'age' => '23');
$str = serialize($arr); //序列化serialize序列化它是php中的一种将数组序列化成一个字符串
echo $str;
echo "<br />序列化可以将一个数组的信息序列化之后,存到一个字段中!";
$date = array('username' => $str, 'password' => '123456');
$model = M('usertable');
if ($model->add($date)) {
echo "插入数据库成功!";
} else {
echo "插入数据失败!";
}
}
function select() {
$model = M('usertable');
$rows = $model->field('username')->select();
$row = unserialize($rows[1]['username']);
echo "反序列化 将数据库中存好的已经序列化的信息,反序列化后成为之前的数组<br />";
dump($row);
echo $row['name'] . '------' . $row['age'];
//dump($rows);
}
}