问题
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?
回答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.
回答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.
回答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:{}
回答4:
An addition to Aliweb's answer!
Redis supports integers as well as actions like INCR, INCRBY, DECR and DECRBY.
As for the question:
Serialize only if is not a String or Int. Serialization is a costly operation!
on GET and HGET try to see what if it is serialized:
'
private function string_unserialize($str){
$data = @unserialize($str);
if ($str === 'b:0;'){
return 0;
}elseif($data !== false){
return $data;
}else {
return $str;
}
}
'
来源:https://stackoverflow.com/questions/26718263/php-redis-is-there-a-way-to-store-php-object-in-redis-without-serializing-it