How to save multidimensional arrays in PHP memcached

Source: Internet
Author: User
Tags php memcached
I want to ask memcached how to save a multidimensional array in PHP?
For example, I want to cache posts from multiple users, in the form of an array:

Php$user1=array (' title ' = ' +    ' posts 1 ', ' time ' = ' 2015-07-08 '),    Array (' title ' = ' + ' posts 2 ', ' time ' = ' 2015-07-09 ');

Assuming that the UID of User1 is 1001, I want to get his post cache by UID, I don't know how to do it.
Can redis also be used in the inside?
Ask the prawn for advice, grateful.

Reply content:

I want to ask memcached how to save a multidimensional array in PHP?
For example, I want to cache posts from multiple users, in the form of an array:

Php$user1=array (' title ' = ' +    ' posts 1 ', ' time ' = ' 2015-07-08 '),    Array (' title ' = ' + ' posts 2 ', ' time ' = ' 2015-07-09 ');

Assuming that the UID of User1 is 1001, I want to get his post cache by UID, I don't know how to do it.
Can redis also be used in the inside?
Ask the prawn for advice, grateful.

PHP's memcached drivers are automatically serialize and unserialize, and all you need to do is:
1. Select a suitable key, such as "Post_of_user_". $userId
2. Call the Set function and throw the data in.

Example code:

0. Preparation: $cache = new Memcache (); $cache->connect (' 127.0.0.1 ', 11211); Please replace it with your Memcache server address and port $userid =//Your own way to get ...//1. Construct the cache Key$cachekey = "Posts_of_user_". $userId;//2. Get data from cache $posts = $cache->get ($cacheKey), if ($posts = = = False) {     ///3. Cache is invalid, load data    $posts =//You try to load the data yourself/    4. Save the data to the cache    $cache->set ($cacheKey, $posts);    } OK, you can use posts.

1.memcache or Redis key value can only be a string or an integer array, so the array must be serialized
2.serialize,json_encode can be serialized
3.PHP recommended with Serialize

    $array  = Array ();    $string = serialize ($array);    $memcache->set (' key ', $string);    $result = $memcache->get (' key ');    $result = Unserialize ($result);

Array (' User1id ' =>array (' title ' = ' + '                        posts 1 ', ' time ' = ' 2015-07-08 '),                        Array (' title ' = ' + ' posts 2 ', ' Time ' = ' 2015-07-09 ')                       ,       ' User2id ' =>array ())      

Can this

Key:prefix + useridvalue:[{"title": "\u5e16\u5b501", "Time": "2015-07-08"},{"title": "\u5e16\u5b502", "Time": " 2015-07-09 "}]

Memcache only supports basic data storage, such as strings, complex structures such as maps, etc.
So structured data can be serialized and stored as values.
There are many methods of serialization, such as Json_encode ();

Arrays do not have to be converted into strings, and the array is stored directly.

I use Json_encode with UrlEncode to serialize the result set, and then save it in Memcache

Memcache, it is recommended that serialization or json_encode be stored after a string. Redis has a richer data type and can also be considered, hashset.

Turn into a JSON string and throw it in.

You can directly save the data into all can, mecached extension will help you to deal with the bottom.

What is the number of users? If it is known that the recommendation or with Json_encode (), deposited into the memcache, according to the structure of three-dimensional array, is not recommended serialize

Take a look at the official example, without having to serialize the array yourself:

Addserver (' localhost ', 11211), $m->set (' int '), $m->set (' string ', ' a simple string '), $m->set (' array ', */* ' object ' This key expires in 5 minutes */$m->set (' object ', New Stdclass, Time () +) Var_dump ($m->get (' int ')) ; Var_dump ($m->get (' string ')); Var_dump ($m->get (' array ')); Var_dump ($m->get (' object '));? >

Memcached can be used to save strings, arrays, objects, multiple types, and automatic serialization.

  • 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.