baig772
baig772

Reputation: 3488

Redis CLI not showing recently stored key via Laravel

I am trying to cache my results using redis in Laravel by doing this

$result =   Cache::remember('orders_cache', 10, function () use ($orders) {
           return $orders;
        });
        return $result;

When I go in my Redis-Cli and do KEYS *, I don't see orders_cache key there. I have set the cache_driver to redis in my .env and I also ran the command php artisan config:cache. I have also installed predis package using composer as well.

My Dev environment is:

Any help on this would be appreciated. TIA

Upvotes: 11

Views: 7784

Answers (6)

Ahmed Ibrahim
Ahmed Ibrahim

Reputation: 1

I was stuck with the same problem and here is my solution.

  1. I make a route targetting this function
public function index()
{
    Cache::put('contact', '03238201322');
    $users = Cache::remember('active_users', 60, function () {
        return User::where('status', 1)->get();
    });
    dd($users);
}
  1. Secondly I run this command in my redis-cli terminal command: monitor
  2. Then I hit the route which is targetting this function.
  3. And all the activity is showing in my terminal which database is selected to store the data and vairables names use as keys then I notice redis select database 1 to store variables and it always use laravel_database_ as prefix of every variable like if my key name is name then it stores laravel_database_name
  4. Then I run command in redis-cli command: SELECT 1. which select database 1 for me.
  5. Then I run command: KEYS * then it shows my all variables I stored through laravel cache.

Upvotes: 0

Primo
Primo

Reputation: 77

I have solved this issue using the help of @m.nikzad answer, but I didn't know what to do after, so I managed to do it this way:

  1. Monitor redis to determine data, database, etc..
  2. Connect to the database number.
  3. Retrieve the keys you want or all the keys, but be careful to retrieve all keys on a huge database!

So, practical:

  1. Monitoring: redis-cli -h <redis_host> monitor, where <redis_host> is your redis host, you will get something like SELECT n in the second line (ig), where n is the database number.
  2. Now you know the database number, let's connect to it: redis-cli -h <redis_host> -n <database_number>, where <redis_host> is your redis host & <database_number> is your database number that we just got in the previous step. If the redis port is different than the default port, please make sure to include -p <redis_port> to the command.
  3. Retrieve: either use KEYS *, KEYS <pattern>, or GET <key>, depending on your needs.

Resources:

  1. monitor
  2. keys
  3. get

Upvotes: 0

shmuels
shmuels

Reputation: 1393

I ran into a similar issue in where my key saved via Laravel was being returned, when queried via Laravel, using Redis::keys('*'), however a key saved via Python was not. I'm still not sure why (and separate databases as noted here was not the cause).

Interestingly, I was able to see the key, saved via Python, using Redis::scan('*') (docs). Even though scan returned the key, I am still not able to get it via Redis::get('key_name'). I need to do further research as to why this is the case, but this may be useful in finding your missing key.

Upvotes: 0

behnam shateri
behnam shateri

Reputation: 1403

Redis instance supports 16 logical databases and numbered from 0 to 15, by default by runnig redis-cli you connect to database 0. you can take advantage of the Redis INFO command to show databases that contain keys and also another more info like:

db0:keys=1,expires=0,avg_ttl=0
db1:keys=2,expires=2,avg_ttl=2462100

That in may case, I have 2 database. now you can change the database you’re using with the select command after you connect. for example:

select 1

and it will return OK if the switch is successful. now you can see your laravel keys.

Upvotes: 19

Ali Raza
Ali Raza

Reputation: 932

In recent versions of Laravel (8.^), it prepends the laravel_database with Redis keys.

E.g: laravel_database_{$YOUR_KEY}.

In order to see what keys are available RUN: KEYS *

Upvotes: 6

m.nikzad
m.nikzad

Reputation: 181

run redis-cli monitorand check the database number and written key.

Upvotes: 7

Related Questions