Home >Database >Redis >How to use and encapsulate Redis in ThinkPHP5 framework

How to use and encapsulate Redis in ThinkPHP5 framework

WBOY
WBOYforward
2023-05-29 20:40:101175browse

Redis is a commonly used non-relational database, mainly used for data caching. The data is saved in the form of key-value, and the key values ​​map to each other. Its data storage is different from MySQL. Its data is stored in memory, so Data reading is relatively fast, which is very good for high concurrency.

Before using the Redis extension that comes with ThinkPHP5.0, you need to download php_redis.dll first. Choose the corresponding version according to your own windows operating system. My system is 64-bit and I installed VC2012, so I downloaded php_redis-2.2.7-5.6-ts-vc11-x64.zip

Download the compressed package After that, unzip the php_redis.dll inside to D:\wamp\bin\php\php5.6.25\ext (select according to the disk where your wamp is located), then add extension=php_redis.dll to php.ini and restart Apache is enough;

The following is the code I tested myself. It can be used. There is not much packaging. You can package it according to your own needs.

extend is thinkPHP5.0 Extend the class library directory, you can define it yourself

namespace My;  //目录我放在thinkphp5.0/extend/My  

class RedisPackage
{
    protected static $handler = null;
    protected $options = [
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => '',
        'select' => 0,
        'timeout' => 0,    //关闭时间 0:代表不关闭
        'expire' => 0,
        'persistent' => false,
        'prefix' => '',
    ];

    public function __construct($options = [])
    {
        if (!extension_loaded('redis')) {   //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常)
            throw new \BadFunctionCallException('not support: redis');      
        }
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $func = $this->options['persistent'] ? 'pconnect' : 'connect';     //判断是否长连接
        self::$handler = new \Redis;
        self::$handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);

        if ('' != $this->options['password']) {
            self::$handler->auth($this->options['password']);
        }

        if (0 != $this->options['select']) {
            self::$handler->select($this->options['select']);
        }
    }

    /**
     * 写入缓存
     * @param string $key 键名
     * @param string $value 键值
     * @param int $exprie 过期时间 0:永不过期
     * @return bool
     */
    public static function set($key, $value, $exprie = 0)
    {
        if ($exprie == 0) {
            $set = self::$handler->set($key, $value);
        } else {
            $set = self::$handler->setex($key, $exprie, $value);
        }
        return $set;
    }

    /**
     * 读取缓存
     * @param string $key 键值
     * @return mixed
     */
    public static function get($key)
    {
        $fun = is_array($key) ? 'Mget' : 'get';
        return self::$handler->{$fun}($key);
    }

    /**
     * 获取值长度
     * @param string $key
     * @return int
     */
    public static function lLen($key)
    {
        return self::$handler->lLen($key);
    }

    /**
     * 将一个或多个值插入到列表头部
     * @param $key
     * @param $value
     * @return int
     */
    public static function LPush($key, $value, $value2 = null, $valueN = null)
    {
        return self::$handler->lPush($key, $value, $value2, $valueN);
    }

    /**
     * 移出并获取列表的第一个元素
     * @param string $key
     * @return string
     */
    public static function lPop($key)
    {
        return self::$handler->lPop($key);
    }
}
namespace app\index\controller;
use think\Controller;
use My\RedisPackage;

class Redis extends Controller
{
    function redis()
    {
        $redis=new RedisPackage();
        $redis::set('dede','我就笑笑');
        echo $redis::get('dede');
    }
}


The above is the detailed content of How to use and encapsulate Redis in ThinkPHP5 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete