> For the complete documentation index, see [llms.txt](https://cherrycake.tin.cat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cherrycake.tin.cat/version-1.x-beta/guide/items-guide/item-cache.md).

# Item cache

Activating cache for your [Item](/version-1.x-beta/reference/core-classes/item.md) class is as easy as setting the `loadFromIdMethod` of your class to `queryDatabaseCache`:

```php
...

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](/version-1.x-beta/reference/core-modules/cache/cache-methods.md#buildcachekey) 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](/version-1.x-beta/reference/core-classes/item/item-methods.md#clearcache) method, like this:

```php
$movie->clearCache();
```

> To give you maximum control, the cache of an Item is not automatically cleared after doing an [Item::update](/version-1.x-beta/reference/core-classes/item/item-methods.md#update) operation, so you have to remember to do also [Item::clearCache](/version-1.x-beta/reference/core-classes/item/item-methods.md#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.
