When I cache json string like so cache()->rememberForever('globals', fn() => json_encode(['foo' => 'bar']));
.
The value stored in cache is actually "s:13:"{"foo":"bar"}";"
rather than "{"foo":"bar"}"
?
Is there a way I can store string without php serialisation?
2
Answers
You should be able to store the string using the
Cache::forever
function. From Laravel docs Storing Items Forever:Given that, I would change your code to something like the following:
You would need to use the true cache storage like Redis::put(…). The cache facade(s) have a pretty helpful way of getting complex data in and out of cache. For instance you can cache models or associative arrays thru that facade and not worry about how it gets stringified behind the scenes. However, if you don’t want that kind of helper/handling to cache and restore your variables – then use the caching storage directly.