With caching you can improve the performance of your plugin. So database queries or complex calculatings aren't executed each time the site is loaded.
We are offering two variants of caching:
- FileCache: Data is cached in files. You can use this kind of cache for data that doesn't change often e.g. menus or single pages.
- SessionCache: Data is cached in a session. This is usefull for things, which change often. Here every user got it's one cache, so you can store user data or something simular.
The samples of this sites are using the FileCache. Just change the name and you use another cache.
The first thing we need to do is storing some data in our cache. Look here:
FileCache::setData("page", $page->id, $page);
The first parameter is the type we want to save. This should be a unique name for each data type, so have a look to our namespaces. In the second parameter you have to give a string, that indicates your object. You need to know this parameter, when you want to read your data later. The last parameter is the object you want to save. The datatype is similar.
What's a cache without a method to read from it? So let's fetch the data from cache:
Before you read from this cache, you can check, if the cache contains your data. Here is the code for that: