Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 888
Sai Wai Maung Avatar asked Nov 03 '14 16:11

Sai Wai Maung


2 Answers

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.

like image 66
Aliweb Avatar answered Sep 21 '22 18:09

Aliweb


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:{}
like image 21
Sinan Eldem Avatar answered Sep 18 '22 18:09

Sinan Eldem