简体   繁体   English

解决Redis缓存中偏移处的php反序列化错误

[英]Resolving php unserialize error at offset in Redis caching

I'm currently using Redis to cache my database results. 我目前正在使用Redis缓存我的数据库结果。 What I did was to place the db results into an array and serializing the array before adding them as values to my keys in the Redis cache. 我要做的是将数据库结果放入数组中并进行序列化,然后再将它们作为值添加到Redis缓存中的键中。

$res = $this->db->query($qry);
foreach($res->result() as $row) {
  $curArr[] = $row;
}
$this->redis->set($key, serialize($curArr));

But, whenever I unserialize the cache, it returns an error in the offset. 但是,每当我反序列化缓存时,它都会在偏移量中返回错误。

$cache_result = $this->redis->get($key);
$curArr = unserialize($cache_result);

Message:  unserialize(): Error at offset 8193 of 8701 bytes

When I checked the $cache_result, the length of the string is 8702 and the character at 8194 is ';'. 当我检查$ cache_result时,字符串的长度为8702,而8194处的字符为“;”。 The string snippet for that part is 'Test 3\\";s:7'. There are also various occurrences of that string snippet in $cache_result but the error just appears in 8193. 该部分的字符串片段为'Test 3 \\“; s:7'。$ cache_result中也出现了该字符串片段,但错误仅出现在8193中。

Your help is much appreciated! 非常感谢您的帮助! Thanks! 谢谢!

Why not do something like this instead : $res = $this->db->query($qry); foreach($res->result() as $row) { $curArr[] = $row;
} $this->redis->set($key, json_encode($curArr));
为什么不这样做: $res = $this->db->query($qry); foreach($res->result() as $row) { $curArr[] = $row;
} $this->redis->set($key, json_encode($curArr));
$res = $this->db->query($qry); foreach($res->result() as $row) { $curArr[] = $row;
} $this->redis->set($key, json_encode($curArr));

While retrieving: 检索时:

$cache_result = $this->redis->get($key); $ cache_result = $ this-> redis-> get($ key);

$curArr = json_decode($cache_result); $ curArr = json_decode($ cache_result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM