method 1:
Controller
<?php namespace app\index\controller; use think\Controller; use think\session\driver\Redis; class Index extends Controller { public function index() { $redis = new Redis(); if(is_null($redis->has('str'))){ var_dump($redis->set('str','this is redis_str')); }else{ var_dump($redis->get('str')); } } }
Method 2:
config.php
// + ----------------------------------------------- ----------------------- // | cache settings // + ------------------- -------------------------------------------------- - 'cache' => [ // drive mode 'of the type' => 'File', // save cache directory 'path' => cache_path, // cache prefix 'prefix' => '', // cache lifetime of 0 indicates permanent cache 'The expire' => 0, ], 'Redis' => [ // driving mode 'type' => 'Redis', // server address 'host' => '127.0.0.1' ,// redis server IP 'Port' => '6379', 'timeout' => 86400 ], 'password' => "",
Controller
<?php namespace app\index\controller; use think\Cache; use think\Controller; class Index extends Controller { public function index() { if(!Cache::has('str')){ var_dump(Cache::set('str','this is redis_str')); }else{ var_dump(Cache::get('str')); } } }