laravel 6 redis缓存无法获取缓存项

piwo6bdm  于 2021-06-08  发布在  Redis
关注(0)|答案(0)|浏览(295)

[更新]
我发现不管我想存多少把钥匙,拉雷维尔只会兑现一把钥匙,但我总是只找到一把钥匙这一把”laravel:9a261d789b8265a6b0495ea5d87481b4589422b1"
我试过了

if (Cache::store('redis')->has('bar')) {
    echo ' - bar key cache exists - ';
}
else{
    echo  ' - bar key cache does not exist - ';
}
Cache::store('redis')->put('bar', 'baz', 600);
$value = Cache::get('bar');
echo $value; exit;

我总是 - bar key cache does not exist - baz 为什么?
我正在尝试在laravel homestead localhost中实现redis缓存

PP_NAME=mysite
APP_ENV=local
APP_KEY=base64:887AJ9PHxl7W/QS4g8VrMch6CTL4QMLneq4dHx2KhoE=
APP_DEBUG=true
APP_URL=https://mysite.test:44300
PAGINATE=10
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

和config/database.php

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ],
],

在config/cache.php中

'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],

现在在控制器中:

$posts = Cache::remember('posts_cache', 600, function () use ($where) {
        return Post::where($where)->inRandomOrder()->paginate(3);
    });

当我打印时 $posts 每次我得到不同的帖子而不是缓存的3个帖子。我跑了

redis-cli monitor

发现它正在缓存。我跑了

redis-cli

然后

keys *

我明白了

1) "laravel:9a261d789b8265a6b0495ea5d87481b4589422b1"
2) "laravel:9a261d789b8265a6b0495ea5d87481b4589422b1:timer"

如何从redis检索缓存数据?我的代码怎么了?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题