$Cache
- is system object, that responses for system cache, instance can be obtained in such way:
<?php
$Cache = \cs\Cache::instance();
This object allow to get store/get/delete any number, string or array value in cache. Data may be lost or erased any time, so this is not permanent storage for data, it is used to store some frequently used data for fasted access.
Methods Drivers Examples \cs\Cache\_Abstract class \cs\Cache\_Abstract_with_namespace class \cs\Cache\Prefix class
$Cache
object has next public methods:
- prefix()
- cache_state()
- disable()
- get()
- set()
- del()
- clean()
Returns instance for simplified work with cache, when using common prefix
Method returns boolean value, true
if cache enabled, and false
otherwise.
Disable cache for current session.
Get value of specified item from cache. Returns stored value, or boolean false
otherwise.
Also there is simplified way to get item - to get it as property of object. (see example)
If item not found and $closure parameter specified - closure must return value for item. This value will be set for current item, and returned.
This allows to replace construction like:
<?php
function foo () {
$Cache = \cs\Cache::instance();
if (($result = $Cache->long_item) === false) {
...
$Cache->long_item = $result;
}
return $result;
}
by
<?php
function foo () {
return \cs\Cache::instance()->get('long_item', function () {
...
return $result;
}
}
Stores some data in cache, like get() also works with items as properties (see example).
Deletes specified item from cache if it exists, like get() also works with items as properties (see example)
Is used to clean all system cache.
All items names looks like paths in file system, and are processed correspondingly. This means, that you can store such items:
- Blog/posts/2
- Blog/posts/3
- Blog/posts/4
Also, if you will try to delete item, that may be considered as directory, for example:
- Blog/posts
- Blog
This will delete directory, and all subdirectories with items inside.
###[Up](#) DriversCache system is based on drivers. They may be different, but provides the same external interface.
As for now, there are 3 built-in cache drivers:
- APC
- APCu
- BlackHole
- Memcached
- FileSystem
First and second both use APC/APCu (Alternative PHP Cache) as storage for cache values, actually it uses RAM. Memcached uses memcached as backend. The last uses file system. BlackHole driver works as it is named, namely does nothing, it may be used for debugging purposes, when there are no need to save any data in cache.
FileSystem is fastest and most stable cache driver, and it is recommended for production usage as for now.
###[Up](#) Examples<?php
$Cache = \cs\Cache::instance();
//Setting
$Cache->set('dir/item1', 1);
$Cache->{'dir/item2'} = 2;
//Getting
$one = $Cache->get('dir/item1');
$two = $Cache->{'dir/item2'};
//Deleting
$Cache->del('dir/item1');
unset($Cache->dir);
This is an abstract class is used as base for cache driver classes. It is described here because it shows major methods of cache abstraction.
This class has next public methods:
- false|mixed get($item : string)
- bool set($item : string, $data : mixed)
- bool del($item : string)
- bool clean()
Get item from cache
Put or change data of cache item
Delete item from cache
Clean cache by deleting all items
###[Up](#) \cs\Cache\\_Abstract_with_namespace classThis is an abstract class that extends \cs\Cache\_Abstract
. It exists to provide simplified integration of cache drivers without native namespaces support (required by CleverStyle Framework), but which have incrementing support (currently APC/APCu and Memcached drivers are using this class).
Class implements methods get()
, set()
, del()
and clean()
from \cs\Cache\_Abstract
and requires to implement following methods instead:
- available_internal()
- get_internal()
- set_internal()
- del_internal()
- increment_internal()
- clean_internal()
Main benefit is that these methods in most cases are trivial proxy calls to respecting driver-specific methods without any additional checks, which makes driver implementation extremely simple.
Whether current cache driver is available (might be false
if necessary extension is not installed or something similar)
Get item by key
Set item by key with value that can be anything that can be encoded as JSON
Delete item by key
Increment item by key
Clean the whole cache
###[Up](#) \cs\Cache\Prefix classThis class is used for simplified work with cache, when using common prefix.
It includes methods for getting/setting/deletion of cache items just like cs\Cache
class, but automatically adds prefix specified in constructor to every item.