PHP operation memcached Various data methods

Source: Internet
Author: User

First of all, before you realize what you're saying, make sure that there are three things that matter:

1: Installation of memcached.

2:memcached supports its own version of PHP.

3: Open the Memcached service, and opened the php.ini memcached module

If the above conditions are satisfied, then I would like to say how to use PHP operation:

In order to be clear, and empty say no reason, so I unified in the following amount of code to add the test, which includes all my testing process:

Also, the main test here is the curd operation of the data.

<?php//memcached is object-oriented to manipulate $mem=new memcache;//Create Memcache object if (! $mem->connect (' 127.0.0.1 ', 11211)) {die ("Connection Failed" );//exit}//connection memcached, the IP address of the parameter, port 11211//Add a set of data: parameter: Key Name-key value-compression-save time/*//increase string if ($mem->add (' Key1 ', ' Beijing ', MEMCACHE _compressed,60) {echo "Add data Success";} *///Add digital/*if ($mem->add (' Key1 ', 123456,memcache_compressed,120) {echo "Add digital success";} *///Add Array/* $arr = Array (' Beijing ', ' Tianjin '), if ($mem->add (' Key1 ', $arr, memcache_compressed,120)) {echo "Add array succeeded". <br/> ';} *///Add Object/*class dog{public $name;p ublic $age;p ublic function __construct ($name, $age) {$this->name= $name; $this- >age= $age;}} $dog 1=new Dog (' Small white ', ' max '), if ($mem->add (' Key1 ', $dog 1,memcache_compressed,60)} {echo "Add object succeeded";} *///add null value (0,null, (), false,0.0, "0" empty array, empty object)/*if ($mem->add (' Key1 ', null,memcache_compressed,60)) {echo "added null value succeeded";} *///Add Resource type/* $con =mysql_connect (' localhost ', ' root ', ' Toor ');//var_dump ($con);//fact indicates memcached cannot save resource type data if (! $con) {echo "Add Resource Failed";} Else{if ($mem->add (' Key1 ', $con, memcache_compressed,60)) {echo "Add Resource succeeded";}} *///two cases, the key name does not exist: createdata, whereas modified/*if ($mem->set (' Key1 ', ' Shanghai ', memcache_compressed,60) {echo "modified data succeeded";} *///Query the data, the same way as the console, return the result $value= $mem->get (' Key1 ');p rint_r ($value); >
The above is the PHP operation memcached Various data methods, there are two points to say

1: When the output object data is saved, if on another page to output, there will be incomplete data errors, you can introduce class files to solve.

2: The actual development, may need to be serialized processing, this will be in my next post introduction

PHP operation memcached Various data methods

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.