【TP5 :数据库:查询构造器:链式操作】cache

版权声明:本文为ywcmoon原创文章,未经允许不得转载。 https://blog.csdn.net/qq_39251267/article/details/82252921

cache

cache方法用于查询缓存操作,连贯操作方法

用于selectfindvaluecolumn方法,以及其衍生方法

在缓存有效期之内不会再次进行数据库查询操作,而是直接获取缓存中的数据

//find方法使用cache方法
Db::table('think_user')->where('id=5')->cache(true)->find();

第一次查询结果会被缓存,第二次查询相同的数据的时候就会直接返回缓存中的内容,而不需要再次进行数据库查询操作。

缓存有效期

默认由缓存配置参数决定,可指定

Db::table('think_user')->cache(true,60)->find();

// 或者使用下面的方式 是等效的

Db::table('think_user')->cache(60)->find();

//表示对查询结果的缓存有效期60秒。

缓存标识

$result = Db::table('think_user')->cache('key',60)->find();
$data = \think\Cache::get('key');

外部就可以通过 \think\Cache 类直接获取查询缓存的数据,使查询缓存更有效率

缓存标签

Db::table('think_user')->cache('key',60,'tagName')->find();

缓存更新或清除

指定缓存标识能让updatedelete操作更新清除缓存

Db::table('think_user')->cache('user_data')->select([1,3,5]);

Db::table('think_user')->cache('user_data')->update(['id'=>1,'name'=>'thinkphp']); //更新缓存

或

Db::table('think_user')->cache('user_data')->delete(['id'=>1); //清除缓存

使用find方法且主键查询,不需指定缓存标识,会自动清理缓存

Db::table('think_user')->cache(true)->find(1);
Db::table('think_user')->update(['id'=>1,'name'=>'thinkphp']);
Db::table('think_user')->cache(true)->find(1);

//最后查询的数据会是更新后的数据。

猜你喜欢

转载自blog.csdn.net/qq_39251267/article/details/82252921