How does memcached store multi-dimensional arrays in php? For example, if I want to cache the title of a post sent by multiple users, the array format is as follows: {code ...} assume that the UID of user1 is 1001. I want to get the post cache of user1 through UID, but I don't know how to get it. In addition, can Redis be used ?... How does memcached store multi-dimensional arrays in php?
For example, if I want to cache the title of a post sent by multiple users, the array format is as follows:
php$ User1 = array ('title' => 'Post 1', 'time' => '2017-07-08 '), array ('title' => 'Post 2', 'time' => '2017-07-09 '),);
Assume that the UID of user1 is 1001. I want to get the post cache of user1 through UID, but I don't know how to get it.
In addition, can Redis be used?
I am very grateful for your advice.
Reply content:
How does memcached store multi-dimensional arrays in php?
For example, if I want to cache the title of a post sent by multiple users, the array format is as follows:
php$ User1 = array ('title' => 'Post 1', 'time' => '2017-07-08 '), array ('title' => 'Post 2', 'time' => '2017-07-09 '),);
Assume that the UID of user1 is 1001. I want to get the post cache of user1 through UID, but I don't know how to get it.
In addition, can Redis be used?
I am very grateful for your advice.
PHP's Memcached driver will automatically perform serialize and unserialize. What you need to do is:
1. select an appropriate key, such as "POST_OF_USER _". $ userId
2. Call the set function to throw the data.
Sample Code:
// 0. preparation: $ cache = new Memcache (); $ cache-> connect ('2017. 127. 0.0.1 ', 11211); // replace it with your memcache server address and port $ userId = // you can find a way to get it... // 1. construct cache KEY $ cacheKey = "POSTS_OF_USER _". $ userId; // 2. get data from the cache $ posts = $ cache-> get ($ cacheKey); if ($ posts = false) {// 3. the cache is invalid. load data $ posts = // you can find a way to load data. // 4. save the data to the cache $ cache-> set ($ cacheKey, $ posts) ;}// OK, you can use posts
1. The memcache or redis key value can only be a string or an integer array, so the array must be serialized.
2. serialize and json_encode can all be serialized.
3. serialize is recommended for PHP.
$array = array(); $string = serialize($array); $memcache->set('key',$string); $result = $memcache->get('key'); $result = unserialize($result);
Array ('user1id' => array ('title' => 'Post 1', 'time' => '2017-07-08 '), array ('title' => 'Post 2', 'time' => '2017-07-09 '), 'user2id' => array ())
Can this happen?
Key: prefix + userId
Value: [{"title": "\ u5e16 \ u5b501", "time": "2015-07-08" },{ "title": "\ u5e16 \ u5b502", "time ": "2015-07-09"}]
Memcache only supports basic data storage, such as strings, and does not support complex structures such as map.
Therefore, structured data can be serialized and stored as values.
There are many serialization methods, such as json_encode ();
The array does not need to be converted into a string, just store the array directly.
I used json_encode and urlencode to serialize the result set, and then stored in memcache.
For memcache, it is recommended to serialize or store json_encode into strings. Redis has more data types. You can also consider hashset.
Convert to json string and throw it in
You can store the data directly, and the Mecached extension will help you deal with it.
What are multiple users? If json_encode () is recommended and saved to memcache, it is based on the three-dimensional array structure and serialize is not recommended.
Take a look at the official example. You do not need to serialize the array yourself:
AddServer ('localhost', 11211); $ m-> set ('int', 99); $ m-> set ('string', 'a simple string '); $ m-> set ('array', array (11, 12 )); /* The key of 'object' will expire in 5 minutes */$ m-> set ('object', new stdclass, time () + 300 ); var_dump ($ m-> get ('int'); var_dump ($ m-> get ('string ')); var_dump ($ m-> get ('array'); var_dump ($ m-> get ('object');?>
Memcached can store strings, arrays, objects, and integer types and automatically serialize them.