PHP lumen通过cache方式调用非default redis的所有方法

1. 可直接使用的方法都在这个文件中 {project}\vendor\predis\predis\src\ClientInterface.php

2. 调用方式示例:Cache::store("my_redis_name")->connection()->sadd($key, array($value));

3. 准备工作:

{project}\config\cache.php中配置redis stores 信息;

return [
    'prefix' => 'ctrlapi',
    'default' => 'default',   
    'stores' => [
        'my_redis_name' => [
            'driver' => 'redis',
            'connection' => 'my_redis_name',
        ],
]

{project}\config\database.php中配置redis connection参数

    'redis' => [
        
        'cluster' => env('REDIS_CLUSTER', false),
        
        'default' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'port'     => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DATABASE', 0),
            'password' => env('REDIS_PASSWORD', false),
            'read_write_timeout' => 0,
        ],
        'my_redis_name' => [
            'host'     => env('MY_REDIS_HOST', '127.0.0.1'),
            'port'     => env('MY_REDIS_PORT', 6379),
            'database' => env('MY_REDIS_DATABASE', 0),
            'password' => env('MY_REDIS_PASSWORD', false),
            'read_write_timeout' => 0,
        ],
    ]

4. 了解业务细节需要关注的几个文件

{project}\vendor\illuminate\cache\CacheManager.php

{project}\vendor\illuminate\cache\RedisStore.php

{project}\vendor\illuminate\redis\RedisServiceProvider.php

{project}\vendor\illuminate\redis\Database.php

{project}\vendor\predis\predis\src\ClientInterface.php

你可能感兴趣的:(PHP基础,经验案例)