Cakephp Autocache Behavior

UPDATE: this Behavior is now a Plugin, making it even easier to use - check it out here:-
* https://github.com/ndejong/CakephpAutocachePlugin

Q: What's this all about?
A: AutocacheBehavior is a CakePHP 2x Behavior that makes query caching as easy as adding a 'cache'=>true condition to your find statement.

Q: How do I use it?
A: (Step 1) - copy/symlink into place Model/Behavior/AutocacheBehavior.php

(Step 2) - copy/symlink into place Model/Datasource/AutocacheSource.php

(Step 3) - add the $autocache datasource to Config/database.php like this:-

public $dummy = array('datasource' => 'AutocacheSource');

(Step 4) - define at least one cache configuration to use in core.php or bootstrap.php, call your first one 'default' like this:-

Cache::config('default', array('engine' => 'File'));

(Step 5) - Tell your model(s) they $actsAs Autocache by adding this:-

public $actsAs = array('Autocache');

(Step 6) - add a 'cache' condition to your find query

(Step 7) - drink a beer, 'cause that was easy!

Q: Do you have any examples?
A: Yep, take a look at exampleApp - go ahead and install it, give it a spin!

Q: What are my options for attaching this Behavior to my models?
A: Just like any other Behavior you can use the following approachs
- Attach through the controller with something that looks like this:-

$this->ModelName->Behaviors->attach('Autocache');

- Attach through the Model by declaring the $actsAs attribute like this:-

public $actsAs = array('Autocache');

- Check out your other options here because there are others:-
http://book.cakephp.org/2.0/en/models/behaviors.html

Q: What Autocache Behavior settings exist?
A: There are just three, they are:-
- default_cache << defines the default cache configuration name to use when a find query contains a 'cache' condition without an explicit cache configuration name. By default the name is 'default'
- check_cache << tells Autocache to check if the cache configuration name that is about to be used has actually been defined, this helps to prevent silly mistakes. By default this is set true
- dummy_datasource << defines the dummy datastore name that needs to be defined in your database.php file. By default it's named 'dummy'

Q: What are the find query condition cache parameters available?
A: There are three in total, they are in the form:-

- $conditions = array('cache'=>true)
- $conditions = array('cache'=>'default')
- $conditions = array('cache'=>array('config'=>'default'))
- $conditions = array('cache'=>array('name'=>'some_name'))
- $conditions = array('cache'=>array('flush'=>true))

The first three are essentially the same thing expressed differently

Q: How does Autocache name cached data?
A: Take a look at _generateCacheName() the crux of the matter is that we take the all query parameters, serialize them and take a hash of the result thus ensuring a useful unique name per query - yes, there is overhead in doing this but it's still less than doing a database query!

Q: What's AutocacheSource (DummySource) all about?
A: In order to prevent the CakePHP Model class from making a full request to the database when we have a cached result we need a way to quickly cause the find() query to return with nothing so we can re-inject the result in the afterFind() callback - it's unfortunate this behavior requires more than one .php file, but that's the way it is - still much tider than the previous approach that involved cutting'n'pasting code into the AppModel.

Q: What's the history?
A: AutocacheBehavior is an improvement on "Automatic model data caching for CakePHP" that I wrote a while back which itself borrowed from "jamienay"
- nicholasdejong.com/story/automatic-model-data-caching-cakephp
- github.com/jamienay/automatic_query_caching

Download tarball here

Or head over to GitHub here:-
https://github.com/ndejong/CakephpAutocacheBehavior

Hi I would ask if exists a

Hi
I would ask if exists a version for cake 1.3.x?
Many Thanks

Hi Bysqsri, Yes, the original

Hi Bysqsri,

Yes, the original work I did was for 1.3, but it is not a neat and tidy plugin like this one
http://www.nicholasdejong.com/story/automatic-model-data-caching-cakephp

Cheers,
N

This seems identical to the

This seems identical to the Cacheable behavior that's been around for a while. Handles changes to the table to reduce data becoming stale.

Hi Dean! Yes the

Hi Dean!

Yes the functionality is quite similar - I kinda like your use of cache() instead of overloading the usual find()

I was interested in being able to use other caching engines other than File, which in my case is why autocache does not do a purge or flush of namespaced cache objects on save or delete. For the find conditions I really need fresh data for the easy solution is to make sure those finds are setup with flush=>true - yep, that means there is no caching on those find queries.

I've re-packaged this Behavior as a Plugin which makes it easier to deploy and use:-
* https://github.com/ndejong/CakephpAutocachePlugin

Enjoy!
N

Hi Mark, Yeah - the way to

Hi Mark,

Yeah - the way to deal with different cache configurations is to specify them in your config file then refer to them when you are doing an autocache.

For example:-

Cache::config('level_1', array('engine' => 'File', 'duration' => '+5 minutes'));
Cache::config('level_2', array('engine' => 'File','duration' => '+20 minutes'));
Cache::config('level_3', array('engine' => 'APC','duration' => '+60 minutes'));

Then then you make your Model->find() you can decide which cache configuration you want to use - doing things this way means that if you need to change your cache timing at some later stage you only need to change a config item and not something buried down in the code

Examples:-

$this->Model->find('all',array('cache'=>'level_1'));
$this->Model->find('all',array('cache'=>'level_2'));
$this->Model->find('all',array('cache'=>array('config'=>'level_2')));

Cache delete sounds useful, can you help me understand a case where cache flush (per item) does not address the need though?

array('cache'=>array('flush'=>true))

N

Is it possible to change the

Is it possible to change the length of the cache storage on runtime? passed as parameter maybe ("+ 2 hours")?
I find it unpractical to cache everything the same length - some things are valid longer than others. or will the model have to delete all cached files itself after edit/delete? it might be a good idea to add some Cache::delete() method in the behavior then.

Hi, can you make it for 1.3.x

Hi, can you make it for 1.3.x versions? This is very good I still need it for my current projects working under 1.3.x version.

I'd be tempted to rename

I'd be tempted to rename 'DummyDatasource' as 'CacheDatasource' but other than that, good stuff :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.