Skip to content

deathkel/easy-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A Trait to use cache easily for Laravel

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

INSTALL

composer require deathkel/easy-cache

USAGE

  • this trait will auto cache protect function
  • default cache time is 60 minutes. you can define a static variable $expire for each function
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
    }
}

delete cache

  • call method forgetCache to delete all cache in class
  • call method forgetMethodCache to delete all cache in the method

when in debug pattern

  • add 'skipCache=1' to http query param will skip cache and exec function
  • add 'forgetCache=1' to http query param will forget cache and restore cache

TODO

  • add test example

License

The MIT License (MIT). Please see License File for more information.

About

a cache trait for Laravel to do cache eaily

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages