栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > thinkphp

缓存操作 - TP5源代码阅读

thinkphp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

缓存操作 - TP5源代码阅读



[TOC]

* * * * *
# 1 缓存
>>缓存,使用外部存储机制存储框架的运行信息

>相对于配置,缓存可用跨请求使用。
>配置的修改与存储只在当前请求中发挥作用,而缓存的修改与存储则会影响到下个请求。

## 1-1 缓存初始化

### Cache::init()
>>初始化配置中指定类型的缓存

public static function init(array $options = [])
{
if (is_null(self::$handler)) {
// 自动初始化缓存
if (!empty($options)) {
self::connect($options);
} elseif ('complex' == Config::get('cache.type')) {
self::connect(Config::get('cache.default'));
} else {
self::connect(Config::get('cache'));
}
}
}



## 1-2 缓存存储连接

### Cache::connect()
>>创建配置中指定类型的缓存存储。

public static function connect(array $options = [], $name = false)
{
$type = !empty($options['type']) ? $options['type'] : 'File';
if (false === $name) {
$name = md5(serialize($options));
}

if (true === $name || !isset(self::$instance[$name])) {
$class = false !== strpos($type, '\') ? $type : '\think\cache\driver\' . ucwords($type);

// 记录初始化信息
App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');
if (true === $name) {
return new $class($options);
} else {
self::$instance[$name] = new $class($options);
}
}
self::$handler = self::$instance[$name];
return self::$handler;
}

>>由上可知缓存机制默认使用File存储,其多种存储驱动实现在(cachedriver)目录下

## 1-3切换缓存存储类型
>>可以用来切换缓存的存储类型

### Cache::store()

public static function store($name)
{
if ('complex' == Config::get('cache.type')) {
self::connect(Config::get('cache.' . $name), strtolower($name));
}
return self::$handler;
}


# 2 缓存操作
>>缓存的操作包括,存储数据到缓存存储器中,读取缓存存储器中的数据

### Cache::get()
>>读取缓存中数据

public static function get($name, $default = false)
{
self::init();
self::$readTimes++;
return self::$handler->get($name, $default);
}


### Cache::set()
>>存储数据到缓存

public static function set($name, $value, $expire = null)
{
self::init();
self::$writeTimes++;
return self::$handler->set($name, $value, $expire);
}


### Cache::has()
>>检查缓存数据

public static function has($name)
{
self::init();
self::$readTimes++;
return self::$handler->has($name);
}


### Cache::inc()
>>缓存自增

public static function inc($name, $step = 1)
{
self::init();
self::$writeTimes++;
return self::$handler->inc($name, $step);
}

### Cache::dec()
>>缓存自减

public static function dec($name, $step = 1)
{
self::init();
self::$writeTimes++;
return self::$handler->dec($name, $step);
}


### Cache::rm()
>>删除缓存

public static function rm($name)
{
self::init();
self::$writeTimes++;
return self::$handler->rm($name);
}


### Cache::clear()
>>清空缓存

public static function clear($tag = null)
{
self::init();
self::$writeTimes++;
return self::$handler->clear($tag);
}


### Cache::tag()
>>带标签缓存

public static function tag($name, $keys = null, $overlay = false)
{
self::init();
return self::$handler->tag($name, $keys, $overlay);
}


# 3 内置缓存驱动

>>tp5内置了多种缓存驱动实现不同形式的存储方式。如File,Redis,Memacahce.

>这里简单分析File类缓存驱动
>其他类型缓存接口相似

## 3-1 缓存驱动抽象类(Driver)

### $driver->getCacheKey()
>> 获取缓存标识

protected function getCacheKey($name)
{
return $this->options['prefix'] . $name;
}


### $driver->pull()
>>读取缓存并删除

public function pull($name)
{
$result = $this->get($name, false);
if ($result) {
$this->rm($name);
return $result;
} else {
return null;
}
}


### $driver->tag()
>>带标签缓存

public function tag($name, $keys = null, $overlay = false)
{
if (is_null($keys)) {
$this->tag = $name;
} else {
$key = 'tag_' . md5($name);
if (is_string($keys)) {
$keys = explode(',', $keys);
}
$keys = array_map([$this, 'getCacheKey'], $keys);
if ($overlay) {
$value = $keys;
} else {
$value = array_unique(array_merge($this->getTagItem($name), $keys));
}
$this->set($key, implode(',', $value));
}
return $this;
}


### $driver->setTagItem()
>>设置缓存标签

protected function setTagItem($name)
{
if ($this->tag) {
$key= 'tag_' . md5($this->tag);
$this->tag = null;
if ($this->has($key)) {
$value = $this->get($key);
$value .= ',' . $name;
} else {
$value = $name;
}
$this->set($key, $value);
}
}


### $driver->getTagItem()
>>获取缓存标签

protected function getTagItem($tag)
{
$key = 'tag_' . md5($tag);
$value = $this->get($key);
if ($value) {
return explode(',', $value);
} else {
return [];
}
}


## 3-2 缓存驱动实现类(File)
### $file->__construct()
>>文件存储缓存构造函数

### $file->init()
>>文件缓存初始化

### $file->getCacheKey()
>>获取变量的存储文件名

### $file->has()
>>判断缓存是否存在

### $file->get()
>> 读取缓存

### $file->set()
>> 写入缓存

### $file->inc()
>>自增缓存

### $file->dec()
>>自减缓存

### $file->rm()
>>删除缓存

### $file->clear()
>>清空缓存

### $file->unlink()
>>文件删除。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/213764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号