deathkel/easy-cache

一个用于Laravel轻松使用缓存的特质

0.0.1 2017-04-13 09:54 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:36:26 UTC


README

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

安装

composer require deathkel/easy-cache

用法

  • 此特质将自动缓存保护函数
  • 默认缓存时间为60分钟。您可以定义每个函数的static变量$expire
public class test(){

    use EasyCacheTrait;
    
    public function __construct(){
        $this->default_expire = 1; //change default expire time (min)
    }
    
    public function DontWantToBeCache(){ // public function will not be cached
        //.....
    }

    protected function WantToBeCache(){ // protected function will be cached automatically
        static $expire = 60; //minute that this function want to be cached
    }
    
    private static function _getCacheKeyPrefixLevel1(){
        return "test:"; //overwrite cache prefix level 1, default is class name
    }
    
    private static function _getCacheKeyPrefixLevel2($method){
        return self::_getCacheKeyPrefixLevel1() . $method . ":"; //overwrite cache prefix level 2
    }
    
    private static function _getCacheKey($method, $params){
        return self::_getCacheKeyPrefixLevel2($method) . md5(json_encode($params)); //overwrite cache key
    }
}

删除缓存

  • 调用方法forgetCache以删除类中的所有缓存
  • 调用方法forgetMethodCache以删除方法中的所有缓存

在调试模式中

  • 将'skipCache=1'添加到http查询参数将跳过缓存并执行函数
  • 将'forgetCache=1'添加到http查询参数将忘记缓存并恢复缓存

待办事项

  • 添加测试示例

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件