Home  >  Article  >  Database  >  What is the method for using Redis in ThinkPHP framework in Pagoda?

What is the method for using Redis in ThinkPHP framework in Pagoda?

王林
王林forward
2023-06-02 20:31:01992browse

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.

Regarding the installation of redis, install the pagoda panel on the server or virtual machine to install redis, so that you can use redis very easily. Remember when installing redis, you must not only install the redis software, but also enter the php used in the project Install the redis extension in the version, and then open the redis software

1. First, find redis in the installation panel of the pagoda and click to install.

What is the method for using Redis in ThinkPHP framework in Pagoda?

2. After installing redis, click Settings and set a password

3. When installing the redis extension in the PHP environment

must be installed in In the PHP version used by the website, install the redis extension.

Create plug-in

Create the file RedisPackage.php in the extend folder of the ThinkPHP root directory. The content is as follows:

<?php
 
class RedisPackage
{
    protected static $handler = null;
    protected $options = [
        &#39;host&#39; => &#39;127.0.0.1&#39;,
        &#39;port&#39; => 6379,
        &#39;password&#39; => &#39;这是你是之前设置的redis密码&#39;,
        &#39;select&#39; => 0,
        &#39;timeout&#39; => 20,//关闭时间 0:代表不关闭
        &#39;expire&#39; => 0,
        &#39;persistent&#39; => false,
        &#39;prefix&#39; => &#39;&#39;,
    ];
 
    public function __construct($options = [])
    {
        if (!extension_loaded(&#39;redis&#39;)) {   //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常)
            throw new \BadFunctionCallException(&#39;not support: redis&#39;);
        }
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $func = $this->options[&#39;persistent&#39;] ? &#39;pconnect&#39; : &#39;connect&#39;;     //判断是否长连接
        self::$handler = new \Redis;
        self::$handler->$func($this->options[&#39;host&#39;], $this->options[&#39;port&#39;], $this->options[&#39;timeout&#39;]);
 
        if (&#39;&#39; != $this->options[&#39;password&#39;]) {
            self::$handler->auth($this->options[&#39;password&#39;]);
        }
 
        if (0 != $this->options[&#39;select&#39;]) {
            self::$handler->select($this->options[&#39;select&#39;]);
        }
    }
 
    /**
     * 写入缓存
     * @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) ? &#39;Mget&#39; : &#39;get&#39;;
        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);
    }
 
 
}

The definition array $options in the class RedisPackage has a key The name is password, fill in the redis password set above here

Introduce the file

import(&#39;RedisPackage&#39;, EXTEND_PATH);

Simple use of Redis

#设置
\RedisPackage::set(&#39;要设置的key&#39;,&#39;这是value&#39;);
 
#获取
$key = \RedisPackage::get(&#39;已设置的key&#39;));

Redis extension

in the Controller to use Redis

Connect redis

$redis = new \Redis(); 
//创建一个redis对象,下面可以直接使用$redis访问到redis对象

$redis->connect(&#39;127.0.0.1&#39;, 6379);
//连接redis数据库,127.0.0.1表示本地(如果线上redis和php目录在同一个IP,则一样使用127.0.0.1),6379为redis端口号,若线上没有修改则默认是这个

Verify whether the connection is successful (can be written or not, only for verification)

$redis ->set( "test" , "redis 连接成功");
echo $redis ->get( "test");

exists() determines whether the key exists, the parameter is the key name

$redis->exists(&#39;active_worker_list&#39;)

set()

set() stores key values. The first parameter is the key name defined by yourself, and the second parameter is the data to be stored. Through this method, the data can be named and stored in the cache.

$result = $redis->set(&#39;active_worker_list&#39;,$r)

Many times we store array type data, but redis does not support reading and writing arrays, so we need to convert the array into json format

$result = $redis->set(&#39;active_worker_list&#39;,json_encode($r,true))

get()

get() gets the key value, the parameter is the key name, through this method you can get the value stored in the corresponding key

$result = $redis->get(&#39;active_worker_list&#39;)

Same as set, many times we need array type data, so we need Convert data in json format into an array

$result = json_decode($redis->get(&#39;active_worker_list&#39;),true);

del()

Sometimes for some reasons (it may be that the assignment is wrong when simply assigning a value...) we need to delete Key value, so we need to use del(), the parameter is the key name

$redis->del(&#39;active_worker_list&#39;);

The above is the detailed content of What is the method for using Redis in ThinkPHP framework in Pagoda?. 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