国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Configuration
redis use
string(string)# in /thinkphp/library/think/Cache.php
Home Database Redis How to use redis in ThinkPHP5

How to use redis in ThinkPHP5

Jun 02, 2023 pm 06:25 PM
thinkphp redis

    Premise: Because this article mainly focuses on using redis in thinkPHP5, there is no special explanation about the installation of redis, but here is a little reminder, after installing redis Be sure to enable the php.ini extension later, otherwise you will still be unable to use redis.

    Configuration

    1. Students who can use ThinkPHP5 all know that TinkPHP5 encapsulates the cache class. We only need to fill in the cache configuration items in the cache in /application/congfig.php It's ready to use (shown below).

    How to use redis in ThinkPHP5

    2. From the /thinkphp/library/think/cache/driver/Redis.php file we can see that the redis cache encapsulated here can only use the string basic type of redis. If you want This will not work if you use composite data types such as hashes or queues.

    Looking at the cache class/thinkphp/library/think/cache/Driver.php, you will find that the handler method will return a handle, so we can use all data types of redis as long as we obtain this handle where we use redis. , so you can add the gethandle method getHandler

    	/**
         * 返回句柄對(duì)象,可執(zhí)行其它高級(jí)方法
         *
         * @access public
         * @return object
         */
        public function handler()
        {
            return $this->handler;
        }
    	/*
        *  獲取句柄
        * @param  
        */
        public static function getHandler()
        {
            return self::init();
        }

    How to use redis in ThinkPHP5

    redis use

    string(string)# in /thinkphp/library/think/Cache.php

    ##Basic type, one key corresponds to one value.

    A string type value can store up to 512MB

    Illustration:

    How to use redis in ThinkPHP5

    // 創(chuàng)建數(shù)據(jù)
    $redis->set('key', 'value');// 獲取數(shù)據(jù)
    $value = $redis->get('key');
    echo $value . PHP_EOL;// 修改數(shù)據(jù),與創(chuàng)建數(shù)據(jù)一致,即覆蓋數(shù)據(jù)
    $redis->set('key', 'value2');
    echo $redis->get('key') . PHP_EOL;// 追加數(shù)據(jù)
    $redis->append('key', '_value2');
    echo $redis->get('key') . PHP_EOL;// 刪除數(shù)據(jù)
    $redis->del('key');
    // $redis->delete('key');
    var_dump($redis->get('key'));// 創(chuàng)建數(shù)據(jù),帶有效期
    $redis->set('timeout_key', 'timeout_value', 5);
    $redis->setex('timeout_key', 5, 'timeout_value');
    // 獲取數(shù)據(jù)的有效期
    echo $redis->ttl('timeout_key') . PHP_EOL;// 判斷是否已經(jīng)寫入,未寫入則寫入
    $redis->set('unique_key', 'unique_value');
    if (!$redis->setnx('unique_key', 'unique_value')) {
    	echo $redis->get('unique_key') . PHP_EOL;
    }// 批量創(chuàng)建
    $multi = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];
    $redis->mset($multi);// 批量獲取
    $result = $redis->mget(array_keys($multi));
    var_dump($result);

    Hash(Hash)

    Hash is a collection of key-value (key=>value) pairs; it is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.

    Each hash can store 2^32 -1 key-value pairs (more than 4 billion)

    Illustration:

    How to use redis in ThinkPHP5

    // 創(chuàng)建 hash 表
    // 向名字叫 'hash' 的 hash表 中添加元素 ['key1' => 'val1']
    $redis->hSet('hash', 'key1', 'val1');// 獲取 hash表 中鍵名是 key1 的值
    echo $redis->hGet('hash', 'key1') . PHP_EOL;// 獲取 hash表的元素個(gè)數(shù)
    echo $redis->hLen('hash') . PHP_EOL;// 獲取 hash表 中所有的鍵
    $keys = $redis->hKeys('hash');
    var_dump($keys);// 獲取 hash表 中所有的值
    $vals = $redis->hVals('hash');
    var_dump($vals);// 獲取 hash表 中所有的鍵值對(duì)
    // 不推薦使用這種方法獲取全部數(shù)據(jù),會(huì)導(dǎo)致服務(wù)器執(zhí)行超時(shí),推薦方法后邊會(huì)詳細(xì)介紹
    // $all = $redis->hGetAll('hash');
    // var_dump($all);// 判斷 hash 表中是否存在鍵名是 key2 的元素
    $bool = $redis->hExists('hash', 'key2');
    echo $bool ? '存在' : '不存在' . PHP_EOL;// 批量添加元素
    $redis->hMset('hash', ['key2' => 'val2', 'key3' => 'val3']);// 批量獲取元素
    $hashes = $redis->hMGet('hash', ['key1', 'key2', 'key3']);
    var_dump($hashes);// 刪除 hash表
    $redis->del('hash');

    List( List)

    A list is a simple list of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list. List types are commonly used in messaging queue services to facilitate message exchange between multiple programs. Each list can store up to about 4 billion elements, or 2^32-1 elements.

    Illustration:

    How to use redis in ThinkPHP5

    // 向隊(duì)列左側(cè)加入元素
    $redis->lPush('lists', 'X');
    $redis->lPush('lists', 'X');
    // 向隊(duì)列右側(cè)加入元素
    $redis->rPush('lists', 'Z');// 將索引為1的數(shù)據(jù)修改為 Y
    $redis->lSet('lists', 1, 'Y');// 獲取 list 長度
    $length = $redis->lLen('lists');
    echo $length;// 遍歷 list
    $lists = $redis->lRange('lists', 0, $length - 1);
    dump($lists);// 從左側(cè)出隊(duì)一個(gè)元素(獲取并刪除)
    $x = $redis->lPop('lists');
    echo $x . PHP_EOL;
    // 從右側(cè)出隊(duì)一個(gè)元素(獲取并刪除)
    $z = $redis->rPop('lists');
    echo $z . PHP_EOL;// 獲取左側(cè)第一個(gè)元素
    $y = $redis->lIndex('lists', 0);
    echo $y . PHP_EOL;// 刪除隊(duì)列
    $redis->del('lists');

    Set (set)

    Redis’ Set is an unordered collection of string type.

    Like lists, the efficiency is very high when performing insertion and deletion and determining whether an element exists.

    The biggest advantage of sets is that they can perform intersection, union, and difference operations.

    The maximum number of elements that a Set can contain is 4294967295 (more than 4 billion).

    Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

    Illustration:

    How to use redis in ThinkPHP5

    // 創(chuàng)建集合
    $redis->sAdd('sets', 'value1', 'value2');
    // 以數(shù)組形式創(chuàng)建集合
    $redis->sAddArray('sets2', ['value1', 'value2', 'value3']);// 取兩個(gè)集合的并集
    $union = $redis->sUnion('sets', 'sets2');
    // 取兩個(gè)集合的差集
    $diff = $redis->sDiff('sets', 'sets2');
    // 取兩個(gè)集合的交集
    $inter = $redis->sInter('sets', 'sets2');var_dump($union, $diff, $inter);// 獲取集合數(shù)量
    $card = $redis->sCard('sets');
    echo $card . PHP_EOL;// 獲取集合中全部元素
    // 不推薦使用這種方法獲取全部數(shù)據(jù),會(huì)導(dǎo)致服務(wù)器執(zhí)行超時(shí),推薦方法后邊會(huì)詳細(xì)介紹
    $sets = $redis->sMembers('sets');
    var_dump($sets);// 判斷元素是否是集合中的成員
    $isMember = $redis->sIsMember('sets', 'value2');
    var_dump($isMember);// 刪除集合中的元素
    $redis->sRem('sets', 'value2');
    var_dump($redis->sMembers('sets'));// 隨機(jī)獲取一個(gè)元素
    echo $redis->sRandMember('sets');// 隨機(jī)獲取一個(gè)元素并從集合中刪除
    echo $redis->sPop('sets');// 刪除集合
    $redis->del('sets', 'sets2');

    zset (ordered set)

    Redis zset is also a collection of string type elements like set, and does not Duplicate members are allowed.

    The difference is that each element is associated with a double type score.

    Redis uses scores to sort the members of the set from small to large.

    // 添加成員
    $redis->zAdd('zset', 95, '小明');
    $redis->zAdd('zset', 99, '小剛');
    $redis->zAdd('zset', 100, '小紅');// 統(tǒng)計(jì)成員個(gè)數(shù)
    echo $redis->zCard('zset') . PHP_EOL;// 獲取某個(gè)成員的分?jǐn)?shù)
    $score = $redis->zScore('zset', '小明');
    echo $score . PHP_EOL;// 獲取某個(gè)成員的排名
    $rank = $redis->zRank('zset', '小明'); // 從低到高排序的名次
    $revRank = $redis->zRevRank('zset', '小明'); // 從高到低排序的名次
    echo $rank . PHP_EOL;
    echo $revRank . PHP_EOL;// 給指定成員增加分?jǐn)?shù)
    $redis->zIncrBy('zset', 1, '小明'); // 給小明加一分// 返回指定排名范圍的成員
    $range = $redis->zRange('zset', 0, 9, true); // 返回分?jǐn)?shù)從低到高排序的前10名及分?jǐn)?shù)
    $revRange = $redis-> zRevRange('zset', 0, 9, true); // 返回分?jǐn)?shù)從高到低排序的前10名及分?jǐn)?shù)
    var_dump($range);
    var_dump($revRange);// 刪除成員
    $redis->zRem('zet', '小明');// 返回指定分?jǐn)?shù)范圍的成員
    $rangeByScore = $redis->zRangeByScore('zet', 98, 100); // 返回指定分?jǐn)?shù)范圍內(nèi)從低到高排序的成員
    $revRangeByScore = $redis->zRevRangeByScore('zet', 98, 100); // 返回指定分?jǐn)?shù)范圍內(nèi)從高到低排序的成員
    var_dump($rangeByScore);
    var_dump($revRangeByScore);

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

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1502
    276
    Recommended Laravel's best expansion packs: 2024 essential tools Recommended Laravel's best expansion packs: 2024 essential tools Apr 30, 2025 pm 02:18 PM

    The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

    Laravel environment construction and basic configuration (Windows/Mac/Linux) Laravel environment construction and basic configuration (Windows/Mac/Linux) Apr 30, 2025 pm 02:27 PM

    The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

    Redis: A Comparison to Traditional Database Servers Redis: A Comparison to Traditional Database Servers May 07, 2025 am 12:09 AM

    Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

    How to limit user resources in Linux? How to configure ulimit? How to limit user resources in Linux? How to configure ulimit? May 29, 2025 pm 11:09 PM

    Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

    Is Redis Primarily a Database? Is Redis Primarily a Database? May 05, 2025 am 12:07 AM

    Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

    Redis: Beyond SQL - The NoSQL Perspective Redis: Beyond SQL - The NoSQL Perspective May 08, 2025 am 12:25 AM

    Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

    Redis: Unveiling Its Purpose and Key Applications Redis: Unveiling Its Purpose and Key Applications May 03, 2025 am 12:11 AM

    Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

    Steps and examples for building a dynamic PHP website with PhpStudy Steps and examples for building a dynamic PHP website with PhpStudy May 16, 2025 pm 07:54 PM

    The steps to build a dynamic PHP website using PhpStudy include: 1. Install PhpStudy and start the service; 2. Configure the website root directory and database connection; 3. Write PHP scripts to generate dynamic content; 4. Debug and optimize website performance. Through these steps, you can build a fully functional dynamic PHP website from scratch.

    See all articles