Without further ado, let’s start with the code

use Illuminate\Console\Command; use Illuminate\Support\Facades\Redis; class TestCommand extends Command { protected $signature = 'test'; public function __construct() { parent::__construct(); } public function handle() { $redis = Redis::connection('WeChatApp'); $redis->select(15); $key = 'test'; If ($redis->get($key)) {echo 'use cache '.PHP_EOL; return 1; } $this->doSomething(); $redis->set($key,1); $redis->expire($key,10); Echo 'set cache '.PHP_EOL; } public function doSomething() { $redis = Redis::connection('WeChatApp'); $redis->select(12); echo 'doSomething'.PHP_EOL; }}Copy the code

Result: The cache has failed to be set

As I’m sure you can see, the problem is that I use Laravel Illuminate\Support\Facades\Redis;

Db (12); db(15)

As those of you who know Laravel well know, The Laravel framework runs as a container, loading many components through service providers, including Redis (singleton), but repackaging through facade mode leads many people to think that redis :: Connection (‘WeChatApp’) is used; “, will be like we use the usual static method is not associated with the relationship, unexpectedly this small oversight, led to my debugging for several hours, record, let us not repeat the same mistake