laravel: 指定redis快取項的字首

刘宏缔的架构森林發表於2024-07-23

一,laravel預設會為快取項新增字首:

config/database.php中:

    'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

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

    ],

例子:

127.0.0.1:6379> keys *
1) "gs_database_ac_13866887788"
2) "a"

二,指定字首:

在配置檔案.env中指定REDIS_PREFIX即可,如圖

laravel: 指定redis快取項的字首

檢視效果:已生效

127.0.0.1:6379> keys *
1) "lhdac_13866887788"
2) "a"

相關文章