TP5框架Redis的使用

TP5.0

       config类配置redis缓存

    'cache'                  => [
        //	使用复合缓存类型
        'type'		        =>		'complex',
        //	默认使用的缓存
        'default'			=>	[
            //	驱动方式
            'type'			=>	'File',
            //	缓存保存目录
            'path'			=>	CACHE_PATH,
        ],
        'file'			    =>	[
            //	驱动方式
            'type'			=>	'File',
            //	缓存保存目录
            'path'			=>	CACHE_PATH,
            //缓存前缀
            'prefix'        =>  '',
            //缓存有效期 0表示永久缓存
            'expire'        =>  0,
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => '127.0.0.1',
            //库10
            'select'     => 10,
        ],
    ],

         创建类导入cache类并在类方法使用redis缓存

//导入Cache类
use think\Cache;

//方法内使用redis缓存
$redis = Cache::store('redis')->handler();

//存入reids缓存
$redis->hset("data",date('Y-m-d', strtotime('last Friday')),json_encode($data));

TP5.1

        config文件下cache类配置缓存驱动方法

return [
    // 驱动方式
    'type'   => 'redis',
    // 缓存保存目录
    'path'   => '',
    // 缓存前缀
    'prefix' => '',
    // 缓存有效期 0表示永久缓存
    'expire' => 0,
];

          创建类导入cache类助手函数并在类方法使用redis缓存

//导入Cache类助手函数
use think\facade\Cache;

        //类方法内使用redis缓存
        $cache=Cache::init();
        $redis=$cache->handler();

        //存入hash函数
        $redis->hset("ceshi",1,"01");
        //取hash函数
        var_dump($redis->hget("ceshi",1));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值