tp5 封装助手函数之redis()

1, 先在创建拓展文件extend/module/Redis.php

/**

 * Created by PhpStorm.

 * User: Mac

 * Date: 2018/8/20

 * Time: 下午1:52

 */

namespace module; 

class Redis extends \Redis { 
    public static function redis() {
        try { 
            $con = new \Redis(); 
            $con->connect(config('redis.host'),config('redis.port'),config('redis.timeping')); 
            $con->auth(config('redis.password'));  
        } catch (\Exception $e) { 
            $con = 'link_err'; 
            $con = $e->getMessage(); 
        }
        return $con;
    } 

} 

2, 修改助手函数文件thinkphp/helper.php,增加如下代码:

//助手函数 redis

if (!function_exists('redis')) {

    /**
     * 获取容器对象实例
     * @return Container
     */

    function redis()

    {

        return \module\Redis::redis();

    }

}

3, 修改配置文件config.php,增加如下代码:

// redis缓存
'redis'   =>  [
    // 驱动方式
    'type'   =>'redis',
    // 服务器地址
    'host'   => '',
    // 服务器端口
    'port'   => '',
    'password' => '',
    'timeout'=> 3600
],

4,使用示例:

redis()->LPUSH('username', '张三'); //存储list类型数据

猜你喜欢

转载自blog.csdn.net/weixin_43652106/article/details/88600379