欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

分享一个 Trait 来易用 Laravel 的缓存

发布时间:2025/3/19 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 分享一个 Trait 来易用 Laravel 的缓存 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

求星求关注 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' 来忘记你的缓存并重新缓存

总结

以上是生活随笔为你收集整理的分享一个 Trait 来易用 Laravel 的缓存的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。