Laravel 4:记住()结果来自:: all()

Is it possible to apply the remember(60) function to something like Service::all()?

This is a data set that will rarely change. I've attempted several variations with no success:

  • Service::all()->remember(60);
  • Service::all()->remember(60)->get();
  • (Service::all())->remember(60);

Of course, I am aware of other caching methods available, but I prefer the cleanliness of this method if available.

Yes, you should be able to simply swap the two such as

Change

Service::get()->remember(60);

to

Service::remember(60)->get();

An odd quirk I agree, but after I ran into this a few weeks back and realized all I had to do was put remember($time_to_remember) in front of the rest of the query builder it works like a charm.

For your perusing pleasure: See the Laravel 4 Query Builder Docs Here

/**
  * Indicate that the query results should be cached.
  *
  * @param  int  $minutes
  * @param  string  $key
  * @return \Illuminate\Database\Query\Builder
  */

 public function remember($minutes, $key = null)
 {
     list($this->cacheMinutes, $this->cacheKey) = array($minutes, $key);
     return $this;
 }

L4 Docs - Queries