AtkCache
From Achievo/ATK Wiki
|
Classname: atkCache
|
When you want to cache some data, you can use atkCache for it. At this moment atkCache has support for the following:
- APC (Alternative PHP Cache)
- eAccelerator
- Memcache to store data in RAM on the localhost or on another server
- Xcache
- File (which stores data in the file system)
- Var (a variable-based cache that lasts only for the duration of a page load)
- Zend Platform (3 versions, output cache, disk cache and shared memory cache, the last two can only be used with version 3.6 and higher of Zend Platform).
Configuration
By the default the lifetime for a cached value will be 1 hour for every method. This can be altered by adding the followin config to you config.inc.php
$config_cache[<method>]['lifetime']=300;
Some cache method's also require some other settings.
File cache:
// Directory where the cache will be written $config_cache['file']['path']=atkconfig('atktempdir')."cache/"; // Context var for fopen $config_cache['file']['context']=array();
Memcache:
// Servername $config_cache['memcache']['host']='localhost'; // Port $config_cache['memcache']['port']=11211; // Connection timeout $config_cache['memcache']['timeout']=1;
xCache:
// Admin Username (needed for deleteall method) $config_cache['xcache']['user']=''; // Password $config_cache['xcache']['passwd']='';
There are 2 ways to set a default cache. Option 1 is by giving an array with available methods. atkCache will choose the first available cache method (fallback). Option 2 is just setting only 1 cache method. Available cache methods are:
- var (default)
- file
- apc
- eaccelerator
- xcache
- memcache
- zp_output (Zend Platform 3.6 and lower, it's deprecated in the 3.6)
- zp_shm (Zend Platform 3.6 and higher)
- zp_disk (Zend Platform 3.6 and higher)
// Let atkCache choose the first available cache method $config_cache_method = array('zp_shm','file'); // Or just set one cache method that should be used $config_cache_method = 'zp_disk';
When you application is hosted on a shared hosting which provides one of the cache methods, you can set a namespace so your keys won't override any other existing keys.
$config_cache_namespace = 'default';
Available Methods
- Add (string $key, mixed $data, mixed $lifetime=false)
Cache a variable in the data store (only if it's not stored)
Parameters
key
Store the variable using this name. key's are cache-unique, so attempting to use add() to store data with a key that already exists will not overwrite the existing data, and will instead return FALSE. (This is the only difference between add() and set().)
data
The variable to store
lifetime
Lifetime, store var in the cache for lifetime seconds. After the lifetime has passed, the stored variable will be expunged from the cache (on the next request). If no lifetime is supplied it will use the default lifetime.
Return values
Returns TRUE on success or FALSE on failure.
- Set ($key,$data,$lifetime=false)
Cache a variable in the data store
Parameters
key
Set the variable using this name. key's are cache-unique, so storing a second value with the same key will overwrite the original value.
data
The variable to store
lifetime
Lifetime, store var in the cache for lifetime seconds. After the lifetime has passed, the stored variable will be expunged from the cache (on the next request). If no lifetime is supplied it will use the default lifetime.
Return values
Returns TRUE on success or FALSE on failure.
- Get ($key)
Fetch a stored variable from the cache
Parameters
key
The key used to store the value (with set or add)
Return values
The stored variable on success; FALSE on failure
- Delete ($key)
Removes a stored variable from the cache
Parameters
key
The key used to store the value (with set or add)
- DeleteAll ()
Clears the cache
Note: this function isn't supported by the Zend Platform Output cache.