php-redis - Is there a way to store PHP object in Redis without serializing it?
Solution 1
As you can see in Redis data types, Redis only supports these 5 data types:
- String
- List
- Set
- Hash
- Sorted Set
So, 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).
Is there any problem with serializing that you insist on storing your objects directly?
Update: According to what you said in the comments, you can use the approach indicated in this answer
So you can use:
$xml = $simpleXmlElem->asXML();
before serialization, and then after unserialize()
, use the following code:
$simpleXmlElem = simplexml_load_string($xml);
In this way, you don't have to serialize a PHP built-in object like SimpleXmlElement
directly and there will be no problems.
Solution 2
Serializing would be the most straightforward way.
An 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.
class MyPage implements JsonSerializable
{
protected $p1;
protected $p2;
/**
* @param mixed $p1
*/
public function setP1($p1)
{
$this->p1 = $p1;
}
/**
* @param mixed $p2
*/
public function setP2($p2)
{
$this->p2 = $p2;
}
/**
* (PHP 5 >= 5.4.0)<br/>
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return [
'p1' => $this->p1,
'p2' => $this->p2,
];
}
}
In this way you're easily able to reconstruct from JSON. You could add a helper method to do that or just call the setters.
Solution 3
Here is how I do it:
class Cache extends Predis\Client {
protected $prefix = 'myapp:';
public function take($key, $value = null, $ttl = null) {
$value = is_object($value) ? serialize($value) : $value;
$key = $this->prefix . $key;
if (!$this->exists($key)) {
if (intval($ttl)) {
$this->setEx($key, $ttl, $value);
} else {
$this->set($key, $value);
}
}
return $this->get($key);
}
}
Usage:
$cache = new Cache;
$lorem = 'Lorem ipsum dolor sit amet';
$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.';
$cachedLorem = $cache->take('lorem', $lorem);
$cachedLoremLong = $cache->take('loremLong', $loremLong);
$cachedLoremTtl = $cache->take('loremTtl', $lorem, 30);
$cachedLoremGet = $cache->take('lorem');
$cachedLoremObject = $cache->take('loremObject', new stdClass);
$cachedLoremObjectTtl = $cache->take('loremObjectTtl', new stdClass, 45);
echo $cachedLorem;
echo $cachedLoremLong;
echo $cachedLoremTtl;
echo $cachedLoremGet;
echo $cachedLoremObject;
echo $cachedLoremObjectTtl;
Output:
Lorem ipsum dolor sit amet
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.
Lorem ipsum dolor sit amet
Lorem ipsum dolor sit amet
O:8:"stdClass":0:{}
O:8:"stdClass":0:{}
Sai Wai Maung
Updated on October 12, 2020Comments
-
Sai Wai Maung over 3 years
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? -
Sai Wai Maung over 9 yearsout of the five data types that is supported by Redis, which format is the most optimal type that I should serialize my PHP object to?
-
Aliweb over 9 yearsThe most logical type is
String
in order to store it and theserialize
function is the best choice and also as the PHP manual says, the purpose of this function is to make a value storable : php.net/manual/en/function.serialize.php -
Sai Wai Maung over 9 yearswhen I used serialize($this->page); I got this error: Exception: Serialization of 'SimpleXMLElement' is not allowed
-
Aliweb over 9 yearsWell PHP built-in objects cannot be serialized. Are all the objects you want to store in Redis of this type or there are also other types of objects? You can also consider
json_encode
as another option. -
Sai Wai Maung over 9 yearswhen I used json_encode($this->page), it stores into Redis as '{}'. Could you give code example based on your answer?
-
bcmcfc over 9 yearsIt will, because you won't have changed the class that your
$this->page
is an instance of yet. You may not be able to if it's part of a framework. -
Sai Wai Maung over 9 yearsI am storing only this type of objects in Redis. I also try
json_encode($this->page)
, but it store empty{}
in Redis. -
Sai Wai Maung over 9 yearsI did and it didn't work. The issue is on the way we create PHP object, I changed that instead. Thanks!