PHP uses memcached simple example to share

Source: Internet
Author: User
Tags memcached resource

In many occasions, we will hear the name memcached, but many students just heard, and did not use or actually understand, only know it is a very good stuff. Here is a brief introduction, memcached is an efficient and fast distributed memory object caching system, mainly used to speed up web dynamic applications. Today we'll start with a simple discussion of the use of memcached.


1. Add Expansion pack

The code is as follows:

2. Add in PHP.ini

The code is as follows:

3. The procedure

The code is as follows:
Create a Mem object instance
$mem =new Memcache;
if (! $mem->connect ("", 11211)) {
Die (' Connection failed! ');
1. Add a String
/* IF ($mem->set (' Key1 ', "Beijing", memcache_compressed,60)) {
echo ' Add OK ';
2. Add Value
/* IF ($mem->set (' Key1 ', 100,memcache_compressed,60)) {
echo ' Add OK ';
3. Adding an array
In addition to the array is, as required. Want the serial number into,
Serialize<=>unserialize, if necessary, can also json_encode <=> Json_decode
$arr =array ("BJ", ' TJ ');
if ($mem->set (' Key1 ', $arr, Memcache_compressed,time () +31*3600*24)) {
echo ' Add array ok99111 ';
4. Adding objects
/* Class dog{
Public $name;
Public $age;
Public function __construct ($name, $age) {
$this->name= $name;
$this->age= $age;
$dog 1=new Dog (' Puppy ', 50);
if ($mem->set (' Key1 ', $dog 1,memcache_compressed,60)) {
Echo ' Add object OK ';
5. Add a null Boolean value
/* IF ($mem->set (' Key1 ', false,memcache_compressed,60)) {
Echo ' Add boolean ok ';
6. The resource type is put in.
/* $con =mysql_connect ("", "root", "root");
if (! $con) {
Die (' Connection database failed ');
Var_dump ($con);
echo "<br/>";
if ($mem->set (' Key1 ', $con, memcache_compressed,60)) {
Echo ' Add resource ok ';
$val = $mem->get (' Key1 ');
You can use the Replace
if ($mem->replace ("Key11", ' Hello ', memcache_compressed,60)) {
Echo ' replace OK ';
Echo ' Replace no OK ';
echo "<br/>";
if ($mem->delete (' key14 ')) {
Echo ' key14 delete ';
Echo ' KEY14 does not exist ';

Above is the introduction of the use of memcache in PHP, I hope you can help.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.