Thinking of implementing caching for your php application , Just in 6 simple (copy and paste) steps you can install and access Memcached Server.
-
Install libevent ,libmemcached and libmemcached devel (dependency)
yum install libevent yum install libmemcached libmemcached-devel
-
Install Memcached Server
yum install memcached
-
Start Memcached server
memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody
(d = daemon, m = memory, u = user, l = IP to listen to, p = port)
-
Check your memcached server is running successfully
ps -eaf | grep memcached
-
Connect Memcached server via telnet
telnet 127.0.0.1 11211 stats quit
-
Install PHP client to access Memcached Server
pecl install memcache
-
Restart your apache server
service httpd restart
Downlaod cache.class.php
and include it to your project
You can specify a script directly inside the cache configuration.
require_once('cache.class.php');
$cache = new MicroCache('cache key');
if($cache->check())
die($cache->out());
else
$cache->start();
// your code here
$cache->end();
- cache_on - Enable/disable caching
- lifetime - The lifetime of the cache
- c_type - Cache type (
memcache
orfile
) - patch - Path of cache directory (if memcached and/or memcache not available)
- If memcached and/or memcache is not available(error when connecting or something else), script change cache mode to 'filecache'. This provides stable cache system.
- Script using buffering output(ob_start(), ob_get_contents(), etc...).
- Some parameters only work if they are assigned before function check().
- Clear current or some cache by function clear().
Anyone and everyone is welcome to contribute.