分享一個 Trait 來易用 Laravel 的快取

KEL發表於2017-04-13

求星求關注 https://github.com/deathkel/easy-cache

安裝

composer require deathkel/easy-cache

使用

  • 這個包會自動快取使用了該 'trait' 的 'class' 的protect方法
  • 預設快取時間是60分鐘,你可以新增一個static 變數 $expire 到你的 'fucntion' 中來單獨設定該 'function' 的快取時間
public class test(){

    use EasyCacheTrait;

    public function DontWantToBeCache(){ // public function 不會被快取
        //.....
    }

    protected function WantToBeCache(){ // protected function 會被自動快取
        static $expire = 60; //預設快取時間
    }
}

在Laravel的bug模式下

  • 你可以新增 'skipCache = 1' 你的http查詢引數來跳過快取直接執行方法
  • 你還可以新增 'forgetCache = 1' 來忘記你的快取並重新快取
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章