Note: *For the purpose of this conversation, let's assume that people have already read the docs at http://laravel.com/docs/cache and we want to push beyond those examples to actual implementation examples including the context of the file that the code is contained in. If you have not read the docs, please check them out.
I would like to hear/see how others implementing caching in their websites and applications. Most examples I have seen show snippets of code that show the process of checking and/or creating cached objects, but outside the context of an actual implementation. (Aside: this practice is actually my biggest frustration with learning Laravel so far... many of the examples of usage are so abstract.)
This is the one clear, non-abstract example of cache usage I have found: https://github.com/cribbb/cribbb/tree/master/app/Cribbb/Cache
In my own application, I have a boilerplate code for Model methods, something like this:
class Example extends Eloquent
{
//...
public function exampleMethod()
{
$cache_id = 'example_output_' . $this->id;
if (null === $output = Cache::get($cache_id)) {
// Code to generate cache $output
Cache::put($cache_id, $output, Config::get('cache.lifetime'));
}
return $output;
}
}
I now understand that this sort of thing would be better put into a decorator/presenter class, although I'm not entirely sure how exactly to go about implementing that. Regardless of any design flaws, the above code snippet works well enough. but I would like to know how to do it better, especially if there are best practices in this regard.
How do you use Caching in your applications?
The below code is a little shorter but does the same thing
10 min cache
$users = Cache::remember('users', 10, function() {
return User::get();
});
cache forever but attach a tag so you can flush it
$users = Cache::tags('user')->rememberForever('users', function() {
return User::get();
});
Cache::tags('user')->flush();
source: http://laravel.com/docs/cache
I don't thing stuff like caching should be in a presenter, a presenter to me is a class that formats 1.66666 into € 1.67
My repositories handle the cache. I usually cache every thing with rememberForever()
and flush the cache based on tags when CRUD the same type of object
zenry said:
I don't thing stuff like caching should be in a presenter, a presenter to me is a class that formats 1.66666 into € 1.67
My repositories handle the cache. I usually cache every thing with
rememberForever()
and flush the cache based on tags when CRUD the same type of object
The design pattern we're thinking of is a Decorator (http://en.wikipedia.org/wiki/Decorator_design_pattern), of which a Presenter is a subset.
zenry said:
I don't thing stuff like caching should be in a presenter, a presenter to me is a class that formats 1.66666 into € 1.67
My repositories handle the cache. I usually cache every thing with
rememberForever()
and flush the cache based on tags when CRUD the same type of object
I would be interested in seeing some code examples in the context of the actual files you use them in.
I don't like the idea of mixing both cache and data persistence/fetching. I haven't done much yet in regards of caching but I've thought about creating a Cached[Resource]Repository which decorates a concrete implementation of a repository contract.
I've wrote a quick snippet to show you what I think I'll be doing (or along the lines of), for my project, you can see that here
This is a much more ideal solution than putting the caching AND fetching in one place, I create a new implementation of ProductRepositry I don't want to rewrite the caching layer.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community