Item cache

All the performance benefits of a cache system when working with Items.

Activating cache for your Item class is as easy as setting the loadFromIdMethod of your class to queryDatabaseCache:


class Movie extends \Cherrycake\Item {
    protected $tableName = "movies";
    protected $loadFromIdMethod = "queryDatabaseCache";
    protected $fields = [
        "id" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_INTEGER
        "title" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_STRING
        "summary" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_TEXT
        "year" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_YEAR
        "imdbRating" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_FLOAT

You can now add some other optional properties for caching if you want to change the defaults:

  • cacheProviderName The name of the cache provider to use. Default: engine

  • cacheTtl: The TTL to use when caching data for this Item. Default: CACHE_TTL_NORMAL

  • cacheSpecificPrefix: The key prefix to use when caching data for this item. Default: none

Just like this, whenever you're loading a Movie, it will be loaded extremely fast from the cache without any actual request to the database, as long as it has been loaded before at least once, and the TTL expiration time hasn't yet arrived.

If you need to remove an item from cache, use the Item::clearCache method, like this:


To give you maximum control, the cache of an Item is not automatically cleared after doing an Item::update operation, so you have to remember to do also Item::clearCache if you want the changes to be effective immediately if someone requests the same Item, so they don't have to wait for the TTL expiration.

Last updated