缓存

缓存 Cache


目前缓存支持两种类型文件缓存和 Redis 缓存;正式项目中建议大家使用 Redis 缓存,文件读写效率低;也可以自行拓展其他类型的缓存;

使用文件缓存

  1. 'cache'=>[
  2. 'type'=>'file'
  3. ]

使用Redis 缓存

  1. 'cache'=>[
  2. 'type'=>'file',
  3. 'host' => '',
  4. 'port' => 6379,
  5. 'password' => 'rz4rLmc6S3eZorVH',
  6. 'select' => 1,
  7. 'timeout' => 0,
  8. 'expire' => -1,
  9. 'persistent' => false
  10. ]

Redis缓存 参数说明

参数说明
host地址
port端口
password密码
selectselect分区
timeout连接超时时间
expire数据默认过期时间
persistent是否持久化连接

Cache方法


set:设置缓存

  1. //设置缓存有效期一个小时
  2. Cache::set('name',$value,3600);

get:获取缓存

  1. //获取缓存如果不存在返回默认值
  2. $value=Cache::set('name',"默认值");

has:检查缓存

  1. //检查缓存是否存在
  2. $has=Cache:: has('name');

inc:自增

  1. //自增1
  2. Cache:: inc('name',1);

dec:自减

  1. //自减1
  2. Cache:: dec('name',1);

remove:删除缓存

  1. //删除缓存
  2. Cache:: remove('name');

clear:清空缓存

  1. //清空缓存
  2. Cache:: clear();

hashSet:设置hash 缓存

注意 hash 类型的缓存不可以设置过期时间

  1. //在$name的hash 里设置 缓存$key,值为$value
  2. Cache:: hashSet($name,$key,$value);

hashGet:获取hash 缓存

注意 hash 类型的缓存不可以设置过期时间

  1. //获取在$name的hash 里设置的缓存$key
  2. Cache:: hashGet($name,$key);

hashRemove:删除 hash 缓存

  1. //删除$name的hash 里设置的缓存$key
  2. Cache:: hashRemove($name,$key);

上一篇:其他功能   下一篇:redis