[development] Proper way to cache data

Robert Douglass rob at robshouse.net
Fri May 18 21:21:29 UTC 2007


William Smith wrote:
> The cid value doesn't matter -- you make your own (any string).  
> You'll want to make it something that you can easily recreate when it 
> comes time to cache_get the stored data.  So your cid could be 
> something like "foo_" . $node->nid or whatever.  I usually create a 
> table if I'm going to be caching a lot of entries.
It doesn't matter which cid as long as you take reasonable efforts to 
make sure that it will never collide with any other cid (if you're 
putting it in the cache table and not your own cache_foo table). Using a 
strategy like foo::id is probably fine.

As for clearing the data, you decide when it is no longer relevant and 
then call cache_clear_all with the cid you want to clear.

In Drupal 5 you should serialize and unserialize complex data structures 
when calling cache_get and cache_set. In Drupal 6 these structures will 
be recognized automatically and the un/serialization is handled by the 
cache API.

-Robert


More information about the development mailing list