Home:ALL Converter>php-redis - Is there a way to store PHP object in Redis without serializing it?

php-redis - Is there a way to store PHP object in Redis without serializing it?

Ask Time:2014-11-04T00:12:33         Author:Sai Wai Maung

Json Formatter

I am trying to store user' request URL as the key and a PHP object corresponding to that key as the value in Redis. I tried the following:

$redisClient = new Redis();
$redisClient->connect('localhost', 6379);
$redisClient->set($_SERVER['REQUEST_URI'], $this->page);
$redisTest = $redisClient->get($_SERVER['REQUEST_URI']);
var_dump($redisTest);

However, with this code the value of the URL key that is being stored in Redis is type of string with the value equal to 'Object' instead of the actual PHP object. Is there a way to store a PHP object without serializing it?

Author:Sai Wai Maung,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26718263/php-redis-is-there-a-way-to-store-php-object-in-redis-without-serializing-it
bcmcfc :

Serializing would be the most straightforward way.\n\nAn alternative is to json_encode only the parameters required to reconstruct the object later. One way to do this is using PHP 5.4's JsonSerialize interface. You'd want to extract various properties using jsonSerialize and then provide the means to pass them back into your class when you pull the item from Redis.\n\n\n\nclass MyPage implements JsonSerializable\n{\n\n protected $p1;\n protected $p2;\n\n /**\n * @param mixed $p1\n */\n public function setP1($p1)\n {\n $this->p1 = $p1;\n }\n\n /**\n * @param mixed $p2\n */\n public function setP2($p2)\n {\n $this->p2 = $p2;\n }\n\n /**\n * (PHP 5 &gt;= 5.4.0)<br/>\n * Specify data which should be serialized to JSON\n * @link http://php.net/manual/en/jsonserializable.jsonserialize.php\n * @return mixed data which can be serialized by <b>json_encode</b>,\n * which is a value of any type other than a resource.\n */\n public function jsonSerialize()\n {\n return [\n 'p1' => $this->p1,\n 'p2' => $this->p2,\n ];\n }\n\n}\n\n\nIn this way you're easily able to reconstruct from JSON. You could add a helper method to do that or just call the setters.",
2014-11-03T16:16:00
Sinan Eldem :

Here is how I do it:\n\nclass Cache extends Predis\\Client {\n protected $prefix = 'myapp:';\n\n public function take($key, $value = null, $ttl = null) {\n $value = is_object($value) ? serialize($value) : $value;\n $key = $this->prefix . $key;\n if (!$this->exists($key)) {\n if (intval($ttl)) {\n $this->setEx($key, $ttl, $value);\n } else {\n $this->set($key, $value);\n }\n }\n return $this->get($key);\n }\n}\n\n\nUsage:\n\n$cache = new Cache;\n\n$lorem = 'Lorem ipsum dolor sit amet';\n$loremLong = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';\n\n$cachedLorem = $cache->take('lorem', $lorem);\n$cachedLoremLong = $cache->take('loremLong', $loremLong);\n$cachedLoremTtl = $cache->take('loremTtl', $lorem, 30);\n$cachedLoremGet = $cache->take('lorem');\n$cachedLoremObject = $cache->take('loremObject', new stdClass);\n$cachedLoremObjectTtl = $cache->take('loremObjectTtl', new stdClass, 45);\n\necho $cachedLorem;\necho $cachedLoremLong;\necho $cachedLoremTtl;\necho $cachedLoremGet;\necho $cachedLoremObject;\necho $cachedLoremObjectTtl;\n\n\nOutput:\n\nLorem ipsum dolor sit amet\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nLorem ipsum dolor sit amet\nLorem ipsum dolor sit amet\nO:8:\"stdClass\":0:{}\nO:8:\"stdClass\":0:{}\n",
2016-11-15T11:04:02
Aliweb :

As you can see in Redis data types, Redis only supports these 5 data types:\n\n\nString\nList\nSet\nHash\nSorted Set\n\n\nSo, there is no object data-type and therefor you are not able to store an object directly as a value. You have to serialize it first (or JSON-encode it with the json_encode function for example). \n\nIs there any problem with serializing that you insist on storing your objects directly?\n\nUpdate: According to what you said in the comments, you can use the approach indicated in this answer\n\nSo you can use: \n\n$xml = $simpleXmlElem->asXML();\n\n\nbefore serialization, and then after unserialize(), use the following code:\n\n$simpleXmlElem = simplexml_load_string($xml);\n\n\nIn this way, you don't have to serialize a PHP built-in object like SimpleXmlElement directly and there will be no problems.",
2014-11-03T16:17:24
yy