<?php
/* Author: Bump Mann (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;
}
}
/**
* Store Value
*
* @param mixed $key
* @param mixed $var
* @param mixed $flag defaults to 0 uncompressed compression state fill: 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;
}
}
/**
* Take out the value of the corresponding key
*
* @param mixed $key
* @param mixed $flags
* $flags If the value of 1 indicates serialization,
* But not compressed, 2 indicates compression without serialization,
* 3 indicates compression and serialization, 0 indicates uncompressed and serialized
*/
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;
}
/**
* Flush Cache but not free memory space
*
*/
Public Function flush () {
Memcache_flush ($this->memcache);
}
/**
* Turn off memory connections
*
*/
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;
}
/**
* Open Large value automatic compression
*
* @param mixed $threshold Unit B
* @param mixed $min _saveings default value is 0.2 for 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;
}
/**
* Caching all server statistics in the server pool
*
* @param mixed $type The type of statistical information you want to crawl, the values you can use are {reset, malloc, maps, Cachedump, slabs, items, sizes}
* @param mixed $slabid cachedump command will completely occupy the server is usually used for more stringent tuning
* @param mixed $limit The number of entity bars fetched 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 ' <pre> ';
Print_r ($mem->getextendedstats ());
}
?> |