Chapter: Using cache
« Useful global objects | ^ Advanced development | Create scripts to install the application » |
− Table of content
jCache is a class to store any data in a cache system like memcache. It supports other kind of storage like files or a SQL database.
Note that jCache doesn't use jKVDb as a backend, but it could use it in next versions.
Configuration ¶
You should declare storages in a var/config/cache.ini.php
file. It may contain some section, corresponding to a "profile", like for jDb or jKVDb.
Each profile must declare these properties:
driver
: the driver name for the storage backendenabled
: to enable or disable the cache for this profile (1
or0
). You can then disable the cache without modifying your classes, when you develop or want to debug something.ttl
: the default time to live of data, in seconds. 0 means no timeout.
Depending of the driver, you may have additional parameters.
memcache backend ¶
It is a driver for memcache, using the memcache API of PHP (not memcached API).
The configuration parameter is server
, containing a list of host and port.
[mymemcache]
driver=memcache
ttl=360
enabled=1
servers = memcache_host1:11211,memcache_host2:11211,memcache_host3:11211
file backend ¶
It is similar to the file driver of jKVDb. Here are its configuration parameters:
driver=file
cache_dir
: the directory where to store the files. by default:temp/yourapp/cache/
.file_locking=1
to disable or enable file locking (keep to 1 is better)directory level
: Set the directory structure level. 0 means "no directory structure", 1 means "one level of directory", 2 means "two levels"...directory_umask
: umask for directory structure (default '0700')file_name_prefix
: prefix for cache files (default 'jelix_cache')cache_file_umask
: umask for cache files (default '0600')
sql backend ¶
This driver uses a sql table to store cache values. The installer of jelix creates the needed table if you use this backend. If it is already installed, during developpement, use one of the lib/jelix/core-modules/jelix/install/sql/install_jcache.schema.*
files to create the table.
Configuration parameters:
dao
: indicates the dao to use. By default, jelix~cache.dbprofile
: the jDb profile to use.
Using jCache ¶
To store and read from the cache system, call one of the static methods of jCache.
jCache::get($key)
: to retrieve the value corresponding to the given key.jCache::set($key, $value, $ttl)
: to store a new value or modify an existing value. $ttl is optionaljCache::delete($key)
: delete the given key-valuejCache::increment($key)
: increment the value. You can indicate also the value of the incrementationjCache::decrement($key)
: decrement the value. You can indicate also the value of the decrementationjCache::add($key, $value)
: to store a new value. return false if the key already existsjCache::replace($key,$value, $ttl)
: to change the value of the corresponding key. If the key doesn't exist, it returns false. $ttl is optional.jCache::garbage()
: delete all expired keysjCache::flush()
: delete all keysjCache::call($fn, $fnargs, $ttl)
: get the value corresponding to the function name/args. If it is not set, call the function $fn with arguments $fnargs, which should return a value. This value is then put into the cache.
All this static methods accept as last argument a profile. By default, the "default" profile will be used.