Share the method of using memcache cache in php
The prerequisite for using memcache is to configure the memcahche environment first on the server side! After confirming that memcahce can be connected normally, it can be used in the program!
<?php /** * Memcache cache operation * @author hxm * @version 1.0 * @since 2015.05.04 */ class MCache extends Object implements CacheFace { private $mem = null; //Mem object private $sId = 1; //Servier service ID /** * Initialize Memcache * * @return Object */ public function __construct() { if ( !class_exists('Memcache') ) { throw new QException('PHP extension does not exist: Memcache'); } $this->mem = new Memcache(); } /** * Link to memcahce service * * @access private * @param string $key keyword * @param string $value cache content * @return array */ private function connect( $sid ) { $file = $this->CacheFile(); require $file; if(! isset($cache) ) { throw new QException('The cache configuration file does not exist'.$file); } $server = $cache[$this->cacheId]; $sid = isset($sid) == 0 ? $this->sId : $sid;//Memcache Service Selection if ( ! $server[$sid]) { throw new QException('The cache server configuration file for the current operation does not exist'); } $host = $server[$sid]['host']; $port = $server[$sid]['port']; try { $this->mem->connect( $host , $port ); } catch (Exception $e) { exit('memecache connection failed, error message:'. $e->getMessage()); } } /** * Write to cache * * @access private * @param string $key keyword * @param string $value cache content * @return array */ public function set( $key , $value , $sid , $expire = 0) { $data = $this->get($key , $sid); //If the key value already exists if( $data ) { return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire); } else { return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire); } } /** * Read cache * * @access private * @param string $key keyword * @param int $sid Select which memcache server to use * @return array */ public function get( $key , $sid) { $this->connect( $sid ); return $this->mem->get($key); } /** * Clean (delete) all stored elements * * @access private * @return array */ public function flush() { $this->connect(); return $this->mem->flush(); } /** * Delete the cache * * @access private * @param string $key keyword * @param int $sid Select which memcache server to use * @return array */ public function remove( $key , $sid) { $this->connect(); return $this->mem->delete($key); } /** * Destructor * Finally close memcache */ public function __destruct() { /*if(! $this->mem) { $this->mem->close(); }*/ } }
The above is the entire content of this article, I hope you like it.
Related Articles
Detailed explanation of PHP+jQuery registration module development
This article mainly records the entire process of developing the PHP+jQuery registration module, including filling in the column username, email, password, duplicate password, verification code, etc. It is very detailed and recommended to everyone.2014-10-10php_screw installation tutorial (another PHP code encryption implementation)
This article mainly introduces the tutorial on installing and using php_screw. php_screw is another PHP code encryption implementation, similar to Zend's encoder. Friends who need it can refer to it.2014-05-05How to disable debug toolbar on Yii2 specific page Debug Toolbar Debug Toolbar
This article mainly introduces you to the relevant information about how to disable the debug toolbar Debug Toolbar on a specific page of Yii2. The article introduces the detailed sample code in detail, which has certain reference learning value for everyone's study or work. Friends who need it, let's take a look below.2017-08-08PHP traversal and print all file instances in the specified directory
This article mainly introduces PHP traversal and printing all file instances in the specified directory. Friends who need it can refer to it.2014-02-02PHP code to generate random numbers or strings
The simplest function to generate random numbers or random strings using php. The characters in the $chars variable can be modified by yourself to achieve the purpose of numbers or strings.2008-09-09PHP uses arrays to replace matches in strings in sequence
This article mainly introduces the relevant information about PHP using arrays to replace matches in strings. Friends who need it can refer to it.2016-01-01PHP CURL multi-threaded operation code example
This article mainly introduces PHP CURL multi-threaded operation code examples. This article directly gives the implementation code. Friends who need it can refer to it.2015-05-05Implementation code to obtain the username, user group, user ID and other information of Discuz forum login under php
An article mentioned earlier that I took username, user group, user ID and other information from cookies and discuz databases, but that is not necessarily accurate, because when discuz logs in, it will use the auth value in COOKIE to log in dynamically, and the cookie information is lagging, so the information obtained is not completely accurate.2010-12-12Make theme support widgets and add plug-ins to enable functions in WordPress
This article mainly introduces the methods of making the theme support widget and adding plug-in to enable functions in WordPress, so that WP can use widget widget and add plug-in enabled functions through register_activation_hook(). Friends who need it can refer to it2015-12-12A summary of the redirected web page jump method in php
This article mainly introduces the summary case tutorial of redirecting web page jump method in PHP. This article explains the understanding and use of this technology through brief cases. The following is the detailed content. Friends who need it can refer to it.2021-08-08