PHP Memcache operation class and usage example, phpmemcache_php tutorial

Source: Internet
Author: User

PHP Memcache operation class and usage example, Phpmemcache


This paper describes the Memcache operation class and usage in PHP. Share to everyone for your reference. The specific analysis is as follows:

Copy CodeThe code is as follows:
<?php
/* 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 (Emptyempty ($this->memcache)) {
return FALSE;
}else{
return TRUE;
}
}
/**
* Store Value
*
* @param mixed $key
* @param mixed $var
* @param mixed $flag default to 0 uncompressed compression status filled in: 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 (Emptyempty ($f)) {
return FALSE;
}else{
return TRUE;
}
}
/**
* Remove the value of the corresponding key
*
* @param mixed $key
* @param mixed $flags
* $flags If this value is 1 indicates serialization,
* But not compressed, 2 indicates compression and is not serialized,
* 3 indicates compression and serialization, 0 indicates not compressed 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;
}
/**
* Refresh cache but not free 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 large-value auto-compression
*
* @param mixed $threshold Unit B
* @param mixed $min _saveings default value is 0.2 = 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 all server statistics in the server pool
*
* @param mixed $type expected crawl statistics type, values that can be used are {reset, malloc, maps, Cachedump, slabs, items, sizes}
* @param mixed $slabid cachedump command will be fully occupied server is typically 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 '

 Print_r ($mem->getextendedstats ()); 
}

?

Hopefully this article will help you with your PHP programming.

http://www.bkjia.com/phpjc/926868.html www.bkjia.com true http://www.bkjia.com/phpjc/926868.html techarticle php the Memcache operation class and usages, Phpmemcache This example describes the Memcache operation class and usage in PHP. Share to everyone for your reference. The specific analysis is as follows: Copy code code ...

  • 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.