Home  >  Article  >  Database  >  How to use redis cache in tp6

How to use redis cache in tp6

PHPz
PHPzforward
2023-05-27 20:25:101317browse

Install redis locally and configure phpredis extension

1. Download redis

Here you can download the redis software according to the number of bits in your system

How to use redis cache in tp6

2. Corresponding version php extension

Download the extension. Select the corresponding php version to download the corresponding one. The editor here uses php7.3.4nts
2.1 Will download the php_redis.dll and php_redis.pdb to the ext folder corresponding to the PHP version

How to use redis cache in tp6
How to use redis cache in tp6

##2.2 2. Find the php.ini file, open it, enter extension=php_redis and save it. Can

How to use redis cache in tp6
How to use redis cache in tp6

3. Configure environment variables

Right-click My Computer->Properties->Advanced System Settings Start Configuration

How to use redis cache in tp6
How to use redis cache in tp6

4. Open redis

Directly use the command Redis-server.exe to open

How to use redis cache in tp6

5.phpinfo() Check whether the phpredis extension is installed successfully

Use the method in the project to check
public function index()
{
    dump(phpinfo());
}

How to use redis cache in tp6

5.1 Open redis to create a new connection

How to use redis cache in tp6

How to use redis cache in tp6

6. Configure config/cache.php to add redis configuration

<?php

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

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

    // 缓存连接方式配置
    &#39;stores&#39;  => [
        &#39;file&#39; => [
            // 驱动方式
            &#39;type&#39;       => &#39;File&#39;,
            // 缓存保存目录
            &#39;path&#39;       => &#39;&#39;,
            // 缓存前缀
            &#39;prefix&#39;     => &#39;&#39;,
            // 缓存有效期 0表示永久缓存
            &#39;expire&#39;     => 0,
            // 缓存标签前缀
            &#39;tag_prefix&#39; => &#39;tag:&#39;,
            // 序列化机制 例如 [&#39;serialize&#39;, &#39;unserialize&#39;]
            &#39;serialize&#39;  => [],
        ],
        // 更多的缓存连接(配置成redis一样)
        &#39;redis&#39;=>[
            &#39;type&#39;       =>&#39;Redis&#39;,  // 这一句很重要
            &#39;host&#39;       => &#39;127.0.0.1&#39;,
            &#39;port&#39;       => 6379,
        ]
    ],
];

7. Use redis cache

<?php
namespace app\admin\controller;
use app\BaseController;
use think\cache\driver\Redis;
use think\facade\Cache;

class Index extends BaseController
{
    public function index()
    {
        Cache::store(&#39;redis&#39;)->set(&#39;phone&#39;, 150000266892);
        dump(Cache::store(&#39;redis&#39;)->get(&#39;phone&#39;));
    }
}

8. Check redis cache success

How to use redis cache in tp6

The above is the detailed content of How to use redis cache in tp6. 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