Home > Article > Backend Development > How to use Memcache operation class in PHP_PHP tutorial
This article introduces an example program for using Memcache in PHP. Friends in need can refer to it.
The code is as follows | Copy code |
/* author:Auguman(lyc) /* email: jar-c@163.com /* Memory cache management */ class Yc_Memcache{ private $memcache=null; public function __construct(){ } /** * Connect to database * * @param mixed $host * @param mixed $port * @param mixed $timeout */ public function connect($host,$port=11211,$timeout=1){ if(!function_exists(memcache_connect)){ return FALSE;} $this->memcache=@memcache_connect($host,$port,$timeout); if(empty($this->memcache)){ Return FALSE; }else{ Return TRUE; } } /** *Storage value * * @param mixed $key * @param mixed $var * @param mixed $flag The default is 0, no compression. Fill in the compression status: MEMCACHE_COMPRESSED * @param mixed $expire Default cache time (in seconds) */ public function set($key,$var,$flag=0,$expire=10){ $f=@memcache_set($this->memcache,$key,$var,$flag,$expire); if(empty($f)){ Return FALSE; }else{ Return TRUE; } } /** * Get the value of the corresponding key * * @param mixed $key * @param mixed $flags * $flags If this value is 1, it means serialization, * but not compressed, 2 indicates compression without serialization, * 3 indicates compression and serialization, 0 indicates no compression and serialization */ public function get($key,$flags=0){ $val=@memcache_get($this->memcache,$key,$flags); return $val; } /** * Delete cached key * * @param mixed $key * @param mixed $timeout */ public function delete($key,$timeout=1){ $flag=@memcache_delete($this->memcache,$key,$timeout); return $flag; } /** * Refresh cache without releasing memory space * */ public function flush(){ memcache_flush($this->memcache); } /** * Close memory connection * */ public function close(){ memcache_close($this->memcache); } /** * Replace the value of the corresponding key * * @param mixed $key * @param mixed $var * @param mixed $flag * @param mixed $expire */ public function replace($key,$var,$flag=0,$expire=1){ $f=memcache_replace($this->memcache,$key,$var,$flag,$expire); return $f; } /** * Turn on automatic compression for large values * * @param mixed $threshold unit b * @param mixed $min_saveings The default value is 0.2, which means 20% compression rate */ public function setCompressThreshold($threshold,$min_saveings=0.2){ $f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings); return $f; } /** * Used to get the online/offline status of a server * * @param mixed $host * @param mixed $port */ public function getServerStatus($host,$port=11211){ $re=memcache_get_server_status($this->memcache,$host,$port); return $re; } /** * * Cache statistics of all servers in the server pool * * @param mixed $type The type of statistical information expected to be captured. The values that can be used are {reset, malloc, maps, cachedump, slabs, items, sizes} * @param mixed $slabid The cachedump command will completely occupy the server and is usually used for stricter tuning * @param mixed $limit The number of entities obtained from the server */ public function getExtendedStats($type='',$slabid=0,$limit=100){ $re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit); return $re; } } /***********TEST AREA*************************/ $mem=new Yc_Memcache(); $f=$mem->connect('125.64.41.138',12000); var_dump($f); if($f){ // $mem->setCompressThreshold(2000,0.2); $mem->set('key','hello',0,30); // var_dump($mem->delete('key1')); // $mem->flush(); // var_dump($mem->replace('hao','d')); // echo $mem->get('key'); echo $mem->getServerStatus('127.0.0.1',12000); echo $mem->get('key'); echo ' '; |