/* Author: mongoman (lyc) /* Email: jar-c@163.com /* Memory cache management */ Class Yc_Memcache { Private $ memcache = null; Public function _ construct (){ } /** * Connect to the 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 value is 0 and the compression status is not compressed. enter MEMCACHE_COMPRESSED. * @ Param mixed $ expire default cache time (unit: 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; } } /** * Obtain the value of the corresponding key. * * @ Param mixed $ key * @ Param mixed $ flags * $ Flags if this value is 1, it indicates serialization, * But not compressed. 2 indicates compression but not serialized, * 3 indicates compression and Serialization. 0 indicates that compression and serialization are not performed. */ Public function get ($ key, $ flags = 0 ){ $ Val = @ memcache_get ($ this-> memcache, $ key, $ flags ); Return $ val; } /** * Delete the cached key * * @ Param mixed $ key * @ Param mixed $ timeout */ Public function delete ($ key, $ timeout = 1 ){ $ Flag = @ memcache_delete ($ this-> memcache, $ key, $ timeout ); Return $ flag; } /** * Refresh the cache but do not release memory space * */ Public function flush (){ Memcache_flush ($ this-> memcache ); } /** * Disable 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; } /** * Enable automatic compaction * * @ Param mixed $ threshold Unit B * @ Param mixed $ min_saveings the default value is 0.2, indicating that the compression rate is 20%. */ Public function setCompressThreshold ($ threshold, $ min_saveings = 0.2 ){ $ F = @ memcache_set_compress_threshold ($ this-> memcache, $ threshold, $ min_saveings ); Return $ f; } /** * Used to obtain 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; } /** * Statistics on all servers in the cache server pool * * @ Param mixed $ type indicates the type of statistics to be captured. the values available include {reset, malloc, maps, cachedump, slabs, items, and sizes} * @ Param mixed $ slabid cachedump the command takes full use of the server and is usually used for strict tuning. * @ Param mixed $ limit the number of entity entries retrieved from the server */ Public function getExtendedStats ($ type = '', $ slabid = 0, $ limit = 100 ){ $ Re = memcache_get_extended_stats ($ this-> memcache, $ type, $ slabid, $ limit ); Return $ re; } } ********************/ $ Mem = new Yc_Memcache (); $ F = $ mem-> connect ('192. 64.41.138 ', 125 ); 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 ', 127 ); Echo $ mem-> get ('key '); Echo''; print_r($mem->getExtendedStats()); } ?> |