PHP Connection memcache Program code

Source: Internet
Author: User

Example

The code is as follows Copy Code

<?php
$mem = new Memcache;
$mem->connect (' 127.0.0.1 ', 11211) or die ("could not Connect");
$mem->set (' key ', ' This is a test! ', 0, 60);
$val = $mem->get (' key ');
Echo $val;
?>

The application will do a class

The code is as follows Copy Code

Class Mcache
{

private static $server = Array (' 127.0.0.1 ', 11211);
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); # SEC
Print_r (Mcache::get (' Test_one '));


PHP memcache Client Summary of all methods

The list of all methods for the Memcache function is as follows:
Memcache::add– adds a value and returns False if it already exists
memcache::addserver– add a server address to use
memcache::close– closes a Memcache object
memcache::connect– Create a Memcache object
memcache_debug– Control Debugging function
Memcache::d ecrement– to subtract values from a key that is saved
Memcache: Delete a key value:d elete–
memcache::flush– clears all cached data
memcache::get– gets a key value
memcache::getextendedstats– get run System statistics for all processes in the process pool
memcache::getserverstatus– get the parameters to run the server
memcache::getstats– returns some running statistics for the server
memcache::getversion– returns the version information of the running Memcache
memcache::increment– an addition to the value in a key that is saved
Memcache: Create a Memcache Persistent connection object:p connect–
Memcache::replace-Overwrite an existing key
memcache::set– Add a value, if it already exists, overwrite
memcache::setcompressthreshold– compression of data larger than a certain size
memcache::setserverparams– Modify server parameters at run time

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.