tp6使用redis

1. phpStudy安装redis和php开启redis扩展

本人使用phpStudy8上默认版本redis3.0.504
找到正在使用的php的配置文件 php.ini
添加 extension=php_redis.dll;要不然会报错 不支持redis;

2. tp6配置redis

config/cache.php

<?php

// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------

return [
    // 默认缓存驱动
    'default' => env('cache.driver', 'file'),

    // 缓存连接方式配置
    'stores'  => [
        'file' => [
            // 驱动方式
            'type'       => 'File',
            // 缓存保存目录
            'path'       => '',
            // 缓存前缀
            'prefix'     => '',
            // 缓存有效期 0表示永久缓存
            'expire'     => 0,
            // 缓存标签前缀
            'tag_prefix' => 'tag:',
            // 序列化机制 例如 ['serialize', 'unserialize']
            'serialize'  => [],
        ],
        // redis配置
        'redis' => [
            'type' => 'redis',  // 类型
            'host' => '127.0.0.1', // ip
            'port' => '6379',   // redis端口号
            'password' => '123456', // redis密码
            'select' => '0',
            'expire' => 0, // 全局缓存有效期 0为永久
            'prefix' => '',
            'timeout' => 3600
        ],
    ],
];

3.创建测试类

<?php
namespace app\api\controller;

use app\BaseController;
use think\App;
use think\facade\Config;
use \think\cache\driver\Redis as driverRedis;

class Redis extends BaseController {
    protected $redis = null;

    public function __construct()
    {
        $redis = new driverRedis(Config::get('cache.stores.redis'));
        $this->redis = $redis;
    }

    public function setString(){
        $this->redis->set('key','value');
        $this->redis->set('key1','value1');
    }

    public function getString(){
        echo $this->redis->get('key');
    }

    public function setList(){
        for ($i=1;$i<=5;$i++) {
            $this->redis->rpush('list',$i);
        }
    }
    public function getList(){
        $arr = [];
        for ($i=1;$i<=5;$i++) {
            $ret = $this->redis->lpop('list');
            $arr[] = $ret;
        }
        print_r($arr);
    }
}

程序员口语

愿有缘人,头发永远茂密,保持一个永恒热爱的心。
做个笔记,加深印象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值