Example of Memcache operations and usage in PHP

Source: Internet
Author: User
This article mainly introduces the Memcache operation class and usage in PHP, and analyzes in detail the usage of Memcache connection to databases and cache operations in the form of instances, which is of great practical value, for more information about Memcache operations and usage in PHP, see the following example. Share it with you for your reference. The specific analysis is as follows:

The code is as follows:


<? Php
/* 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 (emptyempty ($ 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 (emptyempty ($ 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 ());
}

?>

I hope this article will help you with PHP programming.

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.