PHP's more comprehensive cache class

Source: Internet
Author: User
Tags apc delete key
<?php

/*

* Name:wrappercache

* Notes:wrapper cache for Filecache, memcache/memcached, APC, Xcache and Eaccelerator

$CACHEOBJ =wrappercache::getinstance (' Memcache ', 30,array (Array (' host ' = ' localhost '));

echo $cacheObj->cache (' key ', ' value ');

*/

Class Wrappercache {

Const DEFAULT_MEMCACHE_PORT = 11211;


Const Cache_type_auto = ' AUTO ';

Const Cache_type_eaccelerator = ' eaccelerator ';

Const CACHE_TYPE_APC = ' APC ';

Const Cache_type_memcache = ' MEMCACHE ';

Const cache_type_memcached = ' MEMCACHED ';

Const Cache_type_file = ' Filecache ';

Const Cache_type_xcache = ' XCACHE ';


Private $cache _params; Extra params for external caches like path or connection option memcached

Public $cache _expire; Seconds that the cache expires

Private $cache _type; Type of cache to use

Private $cache _external; External instance of cache, can be Filecache or memcache

private static $instance;//instance of this class


Always returns only one instance

public static function getinstance ($type =self::cache_type_auto, $exp _time=3600, $params = ' cache/') {

if (!isset (self:: $instance)) {//doesn ' t exists the isntance

Self:: $instance = new self ($type, $exp _time, $params); Goes to the constructor

}

Return self:: $instance;

}


Cache constructor, optional expiring time and cache path

Private Function __construct ($type, $exp _time, $params) {

$this->cache_expire = $exp _time;

$this->cache_params = $params;

$this->setcachetype ($type);

}


Public Function __destruct () {

unset ($this->cache_external);

}


Prevent users to clone the instance

Public Function __clone () {

$this->cacheerror (' Clone is not allowed. ');

}


Deletes cache from folder

Public Function ClearCache () {

Switch ($this->cache_type) {

Case Self::cache_type_eaccelerator:

Eaccelerator_clean ();

Eaccelerator_clear ();

Break


Case SELF::CACHE_TYPE_APC:

Apc_clear_cache (' user ');

Break


Case Self::cache_type_xcache:

Xcache_clear_cache (Xc_type_var, 0);

Break


Case Self::cache_type_memcache:

$this->cache_external->flush ();

Break


Case self::cache_type_memcached:

$this->cache_external->flush ();

Break


Case Self::cache_type_file:

$this->cache_external->deletecache ();

Break

}

}


Writes or reads the cache

Public Function cache ($key, $value = ', $ttl = ') {

if ($value! = ") {//wants to write

if ($ttl = = ") $ttl = $this->cache_expire;

$this->put ($key, $value, $ttl);

} else return $this->get ($key);

Reading value

}


Creates new cache files with the given data, $key = = Name of the cache, data the info/values to store

Private function put ($key, $data, $ttl = ") {

if ($ttl = = ") $ttl = $this->cache_expire;


Switch ($this->cache_type) {

Case Self::cache_type_eaccelerator:

Eaccelerator_put ($key, Serialize ($data), $ttl);

Break


Case SELF::CACHE_TYPE_APC:

Apc_store ($key, $data, $ttl);

Break


Case Self::cache_type_xcache:

Xcache_set ($key, Serialize ($data), $ttl);

Break


Case Self::cache_type_memcache:

$data =serialize ($data);

$this->cache_external->set ($key, $data, False, $ttl);

Break


Case self::cache_type_memcached:

$data =serialize ($data);

$this->cache_external->set ($key, $data, $ttl);

Break


Case Self::cache_type_file:

$this->cache_external->cache ($key, $data);

Break

}

}


Returns cache for the given key

Private function Get ($key) {

Switch ($this->cache_type) {

Case Self::cache_type_eaccelerator:

$data = Unserialize (Eaccelerator_get ($key));

Break


Case SELF::CACHE_TYPE_APC:

$data = Apc_fetch ($key);

Break


Case Self::cache_type_xcache:

$data = Unserialize (Xcache_get ($key));

Break


Case Self::cache_type_memcache:

$data = Unserialize ($this->cache_external->get ($key));

Break


Case self::cache_type_memcached:

$data = Unserialize ($this->cache_external->get ($key));

Break


Case Self::cache_type_file:

$data = $this->cache_external->cache ($key);

Break

}

return $data;

}


Delete key from cache

Public Function Delete ($key) {

Switch ($this->cache_type) {

Case Self::cache_type_eaccelerator:

EACCELERATOR_RM ($key);

Break


Case SELF::CACHE_TYPE_APC:

Apc_delete ($key);

Break


Case Self::cache_type_xcache:

Xcache_unset ($key);

Break


Case Self::cache_type_memcache:

$this->cache_external->delete ($key);

Break


Case self::cache_type_memcached:

$this->cache_external->delete ($key);

Break


Case Self::cache_type_file:

$this->cache_external->delete ($key);

Break

}


}

Overloading for the application variables and automatically cached

Public Function __set ($name, $value) {

$this->put ($name, $value, $this->cache_expire);

}


Public Function __get ($name) {

return $this->get ($name);

}


Public Function __isset ($key) {//echo "is ' $name ' set?\n"

if ($this->get ($key)!== false) return true;

else return false;

}


Public Function __unset ($name) {//echo ' unsetting ' $name ' \ n ';

$this->delete ($name);

}

End Overloads


Public Function Getcachetype () {

return $this->cache_type;

}


Sets the cache if it installed if not triggers error

Public Function Setcachetype ($type) {

$this->cache_type=strtolower ($type);


Switch ($this->cache_type) {

Case Self::cache_type_eaccelerator:

if (!function_exists (' Eaccelerator_get ')) $this->cacheerror (' eaccelerator not found ');

Break


Case SELF::CACHE_TYPE_APC:

if (!function_exists (' Apc_fetch ')) $this->cacheerror (' APC not found ');

Break


Case Self::cache_type_xcache:

if (function_exists (' Xcache_get ')) $this->cacheerror (' XCache not found ');

Break


Case Self::cache_type_memcache:

if (class_exists (' Memcache ')) $this->init_mem ();

else $this->cacheerror (' memcache not found ');

Break


Case self::cache_type_memcached:

if (class_exists (' Memcached ')) $this->init_mem (true);

else $this->cacheerror (' memcached not found ');

Break


Case Self::cache_type_file:

if (class_exists (' Filecache ')) $this->init_filecache ();

else $this->cacheerror (' Filecache not found ');

Break


Case Self::cache_type_auto://try to AUTO Select a CACHE system

if (function_exists (' Eaccelerator_get ')) $this->cache_type = Self::cache_type_eaccelerator;

ElseIf (function_exists (' Apc_fetch ')) $this->cache_type = SELF::CACHE_TYPE_APC;

ElseIf (function_exists (' Xcache_get ')) $this->cache_type = Self::cache_type_xcache;

ElseIf (class_exists (' Memcache ')) $this->init_mem ();

ElseIf (class_exists (' Memcached ')) $this->init_mem (true);

ElseIf (class_exists (' Filecache ')) $this->init_filecache ();

else $this->cacheerror (' Not any compatible cache is found ');

Break


Default://not any cache selected or wrong one selected

$msg = ' Not any cache type selected ';

if (Isset ($type)) $msg = ' Unrecognized cache type selected <b> '. $type. ' </b> ';

$this->cacheerror ($msg);

Break

}

}


Private Function Init_mem ($useMemecached = False) {//get instance of the Memcache/memcached class

if (Is_array ($this->cache_params)) {

if ($useMemecached) {

$this->cache_type = self::cache_type_memcached;

$this->cache_external = new Memcached ();

} else {

$this->cache_type = Self::cache_type_memcache;

$this->cache_external = new Memcache;

}

foreach ($this->cache_params as $server) {

$server [' port '] = isset ($server [' Port '])? (int) $server [' Port ']: self::D efault_memcache_port;

if ($useMemecached) {

$this->cache_external->addserver ($server [' Host '], $server [' Port '];

} else {

$server [' persistent '] = Isset ($server [' persistent '])? (bool) $server [' Persistent ']: true;

$this->cache_external->addserver ($server [' Host '], $server [' Port '], $server [' persistent '];

}

}

} else $this->cacheerror ($this->cache_type. ' needs an array, example:wrappercache::getinstance ("'. $this->cache_type. ' ", 30,array (Array (" host "=" localhost "));

}


Private Function Init_filecache () {//get instance of the Filecache class

$this->cache_type = self::cache_type_file;

$this->cache_external = filecache::getinstance ($this->cache_expire, $this->cache_params);

}


Public Function Getavailablecache ($return _format= ' html ') {//returns the available cache

$avCaches = Array ();

$avCaches [] = Array (self::cache_type_eaccelerator,function_exists (' eaccelerator_get '));

$avCaches [] = Array (SELF::CACHE_TYPE_APC, function_exists (' Apc_fetch '));

$avCaches [] = Array (Self::cache_type_xcache, function_exists (' Xcache_get '));

$avCaches [] = Array (Self::cache_type_memcache, class_exists (' MEMCACHE '));

$avCaches [] = Array (self::cache_type_memcached, class_exists (' MEMCACHED '));

$avCaches [] = Array (self::cache_type_file, class_exists (' Filecache '));


if ($return _format = = = ' html ') {

$ret = ' <ul> ';

foreach ($avCaches as $c) {

$ret. = ' <li> '. $c [0]. ' - ';

if ($c [1]) $ret. = ' found/compatible ';

else $ret. = ' Not found/incompatible ';

$ret. = ' </ll> ';

}

Return $ret. ' </ul> ';

} else return $avCaches;

}


Private Function Cacheerror ($msg) {//triggers error

Trigger_error (' <br/><b>wrappercache error</b>: '. $msg.

' <br/>if you want the can try with \ ' auto\ ' for Auto Select a compatible cache.

<BR/>or Choose a supported cache from list: '. $this->getavailablecache (), e_user_error);

}

}

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.