Reputation: 7555
I have Drupal 7
site. I am using Memcache
for caching.
This is how I am storing the JSON into it
//creating an object of Memcache
$cache = new Memcache();
$cache ->addServer('localhost', 11211);
//adding a key
$cacheKey = 'mobile';
//delete old cache
$cache ->delete($cacheKey);
//refresh cache
$cache ->set($cacheKey, serialize($jsonData));
No issue till here. But when fetching the JSON from this cache.
returned JSON fails to validate
I am using http://jsonlint.com/
to validate my JSON.
Please note the however the JSON has correct data but issue is validation.
$Records = $cache->get($cacheKey);
echo '<pre>';
print_r(Records);
exit();
Any help highly appreciated.
JSON returned on var_dump() as mentioned in ans by Jeroen
string '{"defaults":[{"nid":"213","public_url":"http:\/\/www.mywebsite.com","current_ver'... (length=3033)
Upvotes: 0
Views: 2070
Reputation: 91762
You are using serialize()
when you store your data, so you need to use unserialize()
when you get it:
$cache->set($cacheKey, serialize($jsonData));
...
$jsonData = unserialize($cache->get($cacheKey));
Although there is not really any need to serialize the data as Memcache will take care of that:
$cache->set($cacheKey, $jsonData);
...
$jsonData = $cache->get($cacheKey);
Edit:
To see what you have exactly:
var_dump($cacheKey);
var_dump($jsonData);
$cache->set($cacheKey, $jsonData);
...
$jsonData = $cache->get($cacheKey);
var_dump($cacheKey);
var_dump($jsonData);
Upvotes: 2