tp6/tp5.1/tp5的redis配置及常用方法

tp6配置

config/cache.php

<?php

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

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

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

tp6使用缓存时要引入  use think\facade\Cache;

tp5.1配置

config/cache.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

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

return [
   // 驱动方式
    'type'   => 'redis',
    // 缓存主机
    'host'       => '127.0.0.1',
    // 缓存端口
    'port'     => '6379',
    // 缓存密码
    'password'     => '',
];

使用时引入 use think\facade\Cache;

tp5配置

application/config.php

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

    'cache'                  => [
        // 驱动方式
        'type'   => 'redis',
        // 缓存主机
        'host'       => '127.0.0.1',
        // 缓存端口
        'port'     => '6379',
         // 缓存密码
        'password'     => '',
    ],

使用时引入 use think\Cache;

常用方法

    public function redis()
    {
        Cache::set('name', 'value', 3600);
        $sj = Cache::get('name');
        return $sj;
    }

// 设置缓存数据
cache('name', $value, 3600);
// 获取缓存数据
var_dump(cache('name'));

 Db::table('think_user')->cache(60)->find();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值