tp5框架中使用redis的示例:
打開tp5框架的編輯器,輸入以下代碼使用redis:
/**
* @Author: Davax<23136891@qq.com>
* @Date: 2020-04-22 09:28:01
* @Last Modified by: Davax
* @Last Modified time: 2020-04-22 11:21:49
*/
namespace app\admin\Controller;
use think\Controller;
Class Test extends Controller{
public $redis;
public function __construct(){
$this->redis = new \Redis();
$this->redis->connect('127.0.0.1',6379);
}
public function index()
{
$redis = $this->redis;
echo $redis->ping();
$a = $redis->set('kl11',22);
$v = $redis->get('kl11');
print_r($v);
}
public function test(){
$redis = $this->redis;
$redis->incr('name');
$redis->expire('name','60');//設置有效時間為1分鐘
print_r($redis->get('name'));
}
}