Hello, I cannot find in the handbook a tutorial on the proper way to cache data. A function (http://drupal.org/node/144969) is calculating the values in a large array, and I want to serialize the result to be able to cache it on the database. This way, the array can be made static between requests, for better performance. What would be the way to go about it? Which {cache_*} table to use, what value to use for cid, etc? How often or in what conditions are each of the cache tables emptied? Is any of the above already documented somewhere? Thanks, Augustin. -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
You can use the base cache table (cache) or make your own by creating a new table with the same structure. 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. I'm not sure if there is an existing handbook page detailing how/when the cache is cleared based on expire argument you pass in, but the api page for cache_set does have a reasonable overview ( http://api.drupal.org/api/5/function/cache_set) William On 5/18/07, Augustin (Beginner) <drupal.beginner@wechange.org> wrote:
Hello,
I cannot find in the handbook a tutorial on the proper way to cache data.
A function (http://drupal.org/node/144969) is calculating the values in a large array, and I want to serialize the result to be able to cache it on the database. This way, the array can be made static between requests, for better performance.
What would be the way to go about it? Which {cache_*} table to use, what value to use for cid, etc? How often or in what conditions are each of the cache tables emptied?
Is any of the above already documented somewhere?
Thanks,
Augustin.
-- http://www.wechange.org/ Because we and the world need to change.
http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
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
I just spent some time answering this question for a client, so I had a quick write-up lying around. I polished it up into an article and published it on the Lullabot site -- http://www.lullabot.com/articles/a_beginners_guide_to_caching_data has an overview of the various bits and pieces I've picked up. The early bits might be a bit unnecessary given that you already know the value of caching ;-) but a tutorial it is! --Jeff Augustin (Beginner) wrote:
Hello,
I cannot find in the handbook a tutorial on the proper way to cache data.
A function (http://drupal.org/node/144969) is calculating the values in a large array, and I want to serialize the result to be able to cache it on the database. This way, the array can be made static between requests, for better performance.
What would be the way to go about it? Which {cache_*} table to use, what value to use for cid, etc? How often or in what conditions are each of the cache tables emptied?
Is any of the above already documented somewhere?
Thanks,
Augustin.
Thank you William, Robert and Jeff, I used the information you gave me to create this new handbook page: http://drupal.org/node/145279 I put it in the Drupal API section, because that's where I looked for some information in the first place. Those who can, feel free to correct my mistakes and improve the page. With that, I think I know enough to be able to provide a fix for a core bug (http://drupal.org/node/144969), and cache the data. Blessings, Augustin. -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
participants (4)
-
Augustin (Beginner) -
Jeff Eaton -
Robert Douglass -
William Smith