]> git.mxchange.org Git - friendica.git/commitdiff
Redis serialize instead of json because of objects
authorPhilipp Holzer <admin@philipp.info>
Sun, 8 Jul 2018 11:35:28 +0000 (13:35 +0200)
committerPhilipp Holzer <admin@philipp.info>
Sun, 8 Jul 2018 11:35:28 +0000 (13:35 +0200)
src/Core/Cache/RedisCacheDriver.php
tests/src/Core/Cache/CacheTest.php

index 223c2b8a943493b146ef2983af7bace20dd0dbcf..20bdb75d29b03dce2bea1c3c03862d1dbcce5acb 100644 (file)
@@ -40,7 +40,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
                        return null;
                }
 
-               $value = json_decode($cached);
+               $value = unserialize($cached);
 
                // Only return a value if the serialized value is valid.
                // We also check if the db entry is a serialized
@@ -56,7 +56,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        {
                $cachekey = $this->getCacheKey($key);
 
-               $cached = json_encode($value);
+               $cached = serialize($value);
 
                if ($ttl > 0) {
                        return $this->redis->setex(
@@ -93,7 +93,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
-               $cached = json_encode($value);
+               $cached = serialize($value);
 
                return $this->redis->setnx($cachekey, $cached);
        }
@@ -105,7 +105,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        {
                $cachekey = $this->getCacheKey($key);
 
-               $newCached = json_encode($newValue);
+               $newCached = serialize($newValue);
 
                $this->redis->watch($cachekey);
                // If the old value isn't what we expected, somebody else changed the key meanwhile
index 4f3e4d351c844970137cb34ee2efcf3a9cde221b..5be0e4b893dd7cba8d1c129fee858ed32d826902 100644 (file)
@@ -5,6 +5,7 @@ namespace Friendica\Test\src\Core\Cache;
 use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Test\DatabaseTest;
+use Friendica\Util\DateTimeFormat;
 
 abstract class CacheTest extends DatabaseTest
 {
@@ -104,4 +105,53 @@ abstract class CacheTest extends DatabaseTest
 
                $this->assertNull($this->instance->get('value1'));
        }
+
+       function testDifferentTypesInCache() {
+               // String test
+               $value = "foobar";
+               $this->instance->set('stringVal', $value);
+               $received = $this->instance->get('stringVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // Integer test
+               $value = 1;
+               $this->instance->set('intVal', $value);
+               $received = $this->instance->get('intVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // Boolean test
+               $value = true;
+               $this->instance->set('boolValTrue', $value);
+               $received = $this->instance->get('boolValTrue');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               $value = false;
+               $this->instance->set('boolValFalse', $value);
+               $received = $this->instance->get('boolValFalse');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // float
+               $value = 4.6634234;
+               $this->instance->set('decVal', $value);
+               $received = $this->instance->get('decVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // array
+               $value = array('1', '2', '3', '4', '5');
+               $this->instance->set('arrayVal', $value);
+               $received = $this->instance->get('arrayVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // object
+               $value = new DateTimeFormat();
+               $this->instance->set('objVal', $value);
+               $received = $this->instance->get('objVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+
+               // null
+               $value = null;
+               $this->instance->set('objVal', $value);
+               $received = $this->instance->get('objVal');
+               $this->assertEquals($value, $received, 'Value type changed from ' . gettype($value) . ' to ' . gettype($received));
+       }
 }