Cache can be defined as memory with high access speed, which purpose is to speed up access to data that is generated at low speed but with high consumption of resources.
In Prestashop there are two types of cache:
1. Templates cache realized in Smarty
Smarty cache setup is located in the back office: Parameters > Performance > Smarty:
2. Cache of results of certain functions implementation, database queries and PHP scripts
For this type of cache you can use one of the four caching systems:
- Memcached – RAM data caching service;
- APC – framework for caching and optimizing PHP source code;
- Xcache – speed up execution of PHP scripts by interpreter through caching their bytecode;
- File System – cache is stored in file system.
This type of cache is set up in the backend: Advanced Parameters > Performance > Caching:
All caching classes, except for Smarty, are located in the directory classes/cache :
- Cache is an abstract class with all other caching system classes inherited from it.
- CacheApc – APC caching system class;
- CacheFs – file caching system class;
- CacheMemcache – Memcached caching system class;
- CacheXcache – Xcache caching system class.
Let’s take a look at the main abstract methods for caching data, which can be used for all caching systems:
- Cache::clean($key) – clear cache by key;
- Cache::delete($key) – Delete one or several data from cache (* joker can be used);
- Cache::deleteQuery($query) – Delete a query from cache;
- Cache::exists($key) – Check if a data is cached;
- Cache::get($key) – Retrieve a data from cache;
- Cache::set($key, $value, $ttl = 0) – Store a data in cache;
- Cache::setQuery($query, $result) – Store a query in cache.
As an example let’s take the Hook class method, which gets the list of alias for hooks names:
public static function getHookAliasList()
$cache_id = 'hook_alias';
$hook_alias_list = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'hook_alias`');
$hook_alias = array();
foreach ($hook_alias_list as $ha)
$hook_alias[strtolower($ha['alias'])] = $ha['name'];
As you can see above, the method Cache: :isStоred( $сасhe_id) verifies the existence of cache, and if it does not exist then data are generated and stored in cache (Cache::store($cache_id, $hook_alias)), else data are simply retrieved from cache (Cache::retrieve($cache_id)).
You can learn more about caching following this link.