How to use the Memcache action class in PHP

Source: Internet
Author: User
The code is as follows Copy Code
<?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 ());
}

?>

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.