I use Redis for my Cache in laravel.
In my codebase, I have a piece of code which sets a cache value like this:
$key = 'track:' . $user->id ...; // this value comes out as 'track:2697:08169'
Cache::put($key, $value, $expiry);
So at this point, the key is 'track:2697:08169'
and the value is 10
, and if I were to run Cache::get('track:2697:08169')
I would naturally get 10 out.
I've been trying to dig into the internals for reasons I'll not go into, but I was trying to find all the keys in my cache, Redis::connection('cache')->keys('*');
, and this particular key showed up in the list as myapi_database_myapi_cache:track:2697:08169
. Did a little research to discover that the Cache
facade uses a prefix for these scenarios, makes sense to me.
So naturally, if that's the key according to Redis, then I should be able to do this: Redis::connection('cache')->get('myapi_database_myapi_cache:track:2697:08169');
But that doesn't give me anything. It's just blank.
Why can I access the value via the Cache:: facade, but not by Redis directly using the prefixed key? How can I access the value for this key? Also, is there any way to access the expiry of this key?
I figured out the answer by messing around a bit. I left out some vital information in my question, I didn't think it would be relevant, sorry for that.
I said that my key Cache::get('track:2697:08169')
got turned into 'myapi_cache:track:2697:08169'
, but that's not true -- it got turned into 'myapi_database_myapi_cache:track:2697:08169'
, and therein was the source of my issues:
When I was doing Redis::connection('cache')->get('myapi_database_myapi_cache:track:2697:08169');
it wasn't working, and when I was doing Redis::connection('cache')->get('track:2697:08169');
it wasn't working either, but I played around and discovered that Redis::connection('cache')->get('myapi_cache:track:2697:08169');
actually does work!
Once I changed the key by removing that part of the prefix, but not the myapi_cache
prefix, this worked too: $ttl = Redis::connection('cache')->ttl($rediskey);