Memcache is a high-performance distributed memory object cache system commonly used in php. we can use it to improve website performance and reduce server load, the following is my learning notebook. let's take a look at the simplest connection method.
Memcache is a high-performance distributed memory object cache system commonly used in php. we can use it to improve website performance and reduce server load, the following is my learning notebook. let's take a look at the simplest connection method.
The sample code is as follows:
-
- $ Mem = new Memcache;
- $ Mem-> connect ('127. 0.0.1 ', 127) or die ("cocould not connect ");
- $ Mem-> set ('key', 'This is a test! ', 0, 60 );
- $ Val = $ mem-> get ('key ');
- Echo $ val;
- ?>
The application will create a class, the code is as follows:
- Class MCache
- {
-
- Private static $ server = array ('2017. 0.0.1 ', 127 );
- Private static $ client = 'memcached'; // memcache or memcached
-
- Private static function get_memcache ()
- {
- Static $ memcache;
- If (! Is_object ($ memcache ))
- {Www.111cn.net
- $ Memcache = self: $ client = 'memcached '? New Memcached (): new Memcache;
- $ Memcache-> addServer (self: $ server [0], self: $ server [1]);
- }
- Return $ memcache;
- }
-
- Public static function set ($ key, $ value, $ expire = 600)
- {
- Self: $ client === 'memcached '? Self: get_memcache ()-> set ($ key, $ value, $ expire)
- : Self: get_memcache ()-> set ($ key, $ value, MEMCACHE_COMPRESSED, $ expire );
- }
-
- Public static function get ($ key)
- {
- Return self: get_memcache ()-> get ($ key );
- }
-
- }
- MCache: set ('test _ one', 'value _ one', 60); #60 Sec
- Print_r (MCache: get ('test _ one '));
The list of all memcache functions is as follows:
Memcache: add-add a value. if it already exists, false is returned.
Memcache: addServer-add an available server address
Memcache: close-close a Memcache object
Memcache: connect-create a Memcache object
Memcache_debug-control debugging function
Memcache: decrement-deletes the value of a saved key.
Memcache: delete-delete a key value
Memcache: flush-clear all cached data
Memcache: get-get a key value
Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.
Memcache: getServerStatus-get the parameters of the running server
Memcache: getStats-return some running statistics of the server
Memcache: getVersion-returns the version information of the running Memcache.
Memcache: increment-adds the value of a saved key.
Memcache: pconnect-creates a persistent connection object for Memcache.
Memcache: replace-overwrite an existing key
Memcache: set-add a value. if it already exists, overwrite it.
Memcache: setCompressThreshold-compresses data larger than a certain size
Memcache: setServerParams-modify server parameters at runtime