doufei4923 2014-01-18 11:41
浏览 123
已采纳

使用load('relation')和缓存

I know you can cache a query by appending ->remember($minutes).

However, this doesn't work when using loading on an existing Eloquent result. This is because load doesn't return a querybuilder but directly executes it.

Say you want to load the friends of a user:

$user=Auth::user();
$user->load('friends');  // How to cache these results ?

How can I cache these ?

  • 写回答

2条回答 默认 最新

  • dqzg62440 2014-01-20 02:17
    关注

    You can't using this code on its own. You do have two options, though:

    1) Wrap your code in a wrapper somewhere (essentially abstracting away this call), which does a cache check first, and runs these queries if the cached item does not exist or is expired.

    2) Extend the Auth library, specifically its EloquentUserProvider class, and modify the query performed by the retrieveById() method.

    You can see one method of abstracting methods and caching in Chris Fidao's Implementing Laravel. The accompanying book is handy!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?