1,redis配置

宝塔redis 宝塔redis数据库_php


2,修改配置文件91行

保护模式关闭

#protected-mode Yes
#protected-mode no

宝塔redis 宝塔redis数据库_宝塔redis_02


3,php安装redis扩展,重启apache打印phpinfo();

宝塔redis 宝塔redis数据库_php_03


index.php

<?php
echo phpinfo();

宝塔redis 宝塔redis数据库_宝塔redis_04


redis扩展安装完成。

4.打开tp5.1 配置文件config/cache.php

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

return [
    'type'  =>  'complex', 
    'default'  =>  [
        // 驱动方式
        'type'   => 'File',
        // 缓存保存目录
        'path'   => '',
        // 缓存前缀
        'prefix' => '',
        // 缓存有效期 0表示永久缓存
        'expire' => 0,
    ],
    // Redis缓存
    'redis' => [
        // 驱动方式
        'type' => 'Redis',
        // 服务器IP地址
        'host' => '127.0.0.1',
        // redis 端口地址,端口需要放行
        'port' => '6379',
        // 服务器redis密码
        'password' => '111111',
        'timeout' => 3600,
        // 全局缓存有效期(0为永久有效)
        'expire'=>  3000, 
        // 缓存前缀
        'prefix'=>  'think',
    ]
];

5,新建测试demo

// 使用Redis缓存
Cache::store('redis')->set('name','value',3600);
Cache::store('redis')->get('name');

6,测试缓存是否成功
本地服务设置

<?php

namespace app\task\Controller;

use Common;
use think\facade\Cache;

class Test 
{
    public function RedisCache() {
        // 本地
        Cache::store('redis')->set('name','xin.he',3600);
        var_dump(Cache::store('redis')->get('name'));
    }
}

打印結果

宝塔redis 宝塔redis数据库_redis_05


服务器获取redis缓存

<?php

namespace app\task\Controller;

use Common;
use think\facade\Cache;

class Test 
{
    public function getRedisCache() {
        // 服务器
        var_dump(Cache::store('redis')->get('name'));
    }
}

打印結果

宝塔redis 宝塔redis数据库_宝塔redis_06

这样redis缓存可以实现缓存共享,负载均衡服务器完美解决缓存问题。