ThinkPHP 中使用Redis

环境.env

[app]
app_debug = "1"
app_trace = ""

[database]
database = ""
hostname = "127.0.0.1"
hostport = ""
password = ""
prefix = "ls_"
username = ""

[redis] 
hostname = "127.0.0.1"
hostport = "6379" 
password = "123456"
prefix = "redis_" 

[project]
env_name = ""
file_domain = "xxx.xxx.xxx.xxx" 

配置 config

<?php 

// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
use think\facade\Env; 

return [
    // 缓存配置为复合类型
    'type'  =>  'complex', 
    'default'	=>	[
      'type'	=>	Env::get('cache.type','File'),
      // 全局缓存有效期(0为永久有效)
      'expire'=>  0, 
      // 缓存前缀
      'prefix'=>  'shop_',
       // 缓存目录
      'path'  =>  '',
    ],
    'redis'	=>	[
      'type'	=>	'redis',
      'host'	=> Env::get('redis.hostname'),
      'port'    => Env::get('redis.hostport'),
      'password' => Env::get('redis.password'),
      'expire'=>  0, 
      'prefix'=>  'redis_', // 缓存前缀
    ],    
    // 添加更多的缓存类型设置
];

Redis缓存处理类

<?php 
// +----------------------------------------------------------------------
// | Redis缓存处理类
// +----------------------------------------------------------------------
// |  
// +----------------------------------------------------------------------
// | author: 007
// +----------------------------------------------------------------------
namespace app\common\logic;
 
use think\facade\Env;
use think\facade\Cache;

class RedisLogic
{  
    private $rc = null; //连接实例 
    
    protected $module = ''; // 模块标识

    public function __construct($module = '')
    {
        $this->rc = Cache::store('redis'); 
        $this->module = $module;
    }

    
    public function getkeys($key)
    {  
        return $this->rc->keys($key); 
    }

    public function setCache($key, $val)
    { 
        if($this->module) $key = $this->module.":".$key;
        return $this->rc->set($key, $val); 
    }

    public function getCache($key)
    { 
        if($this->module) $key = $this->module.":".$key;
        return $this->rc->get($key); 
    }  

    public function delete($key)
    { 
        if($this->module) $key = $this->module.":".$key;
        $prefix = Env::get('redis.prefix','');
        $key = $prefix.$key;
        return $this->rc->del($key);  
    }

    /**
     * 删除指定key的缓存
    *
    * 若 `$key===true` 则表示删除全部
    *
    *      // 支持下面的方式
    *      $this->delete('abc');
    *      $this->delete('abc', 'abc2');
    *      $this->delete(['abc', 'abc2']);
    *
    *      // 删除全部
    *      $this->delete(true);
    *      // 也可使用
    *      $this->delete_all();
    *
    * @param string|array|true $key
    */
    public function delCache($key)
    {   
        // $this->_connect(); 
        if (true === $key) {
            return $this->rc->flushAll();
        } else {
            if (is_array($key)) {
                $keys = $key;
            } else {
                $keys = func_get_args();
            }
            return $this->rc->del($keys);
        }
    } 
    
 
    public function hGetAllCache($key)
    { 
        if($this->module) $hash= $this->module.":".$key; 
        return $this->rc->hGetAll($hash); 
    } 
 

    // 判断hash表中字段是否存在,存在返回true,失败返回false
    public function hExistsCache($key, $field)
    {
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hExists($hash, $field); 
    } 

    public function hSetCache($key, $field, $val)
    {
        if($this->module) $hash= $this->module.":".$key;    
        return $this->rc->hSet($hash, $field, $val); 
    } 


    public function hGetCache($key, $field)
    {
        if($this->module) $hash= $this->module.":".$key;     
        return $this->rc->hGet($hash, $field); 
    } 

    public function hMgetCache($key, $fields)
    {
        // $fields = ['name', 'age']
        if($this->module) $hash= $this->module.":".$key;    
        return $this->rc->hMget($hash, $fields); 
    }

    public function hMsetCache($key, $entry)
    {
        // $entry = ['name' => 'jet', 'age' => 18]
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hMset($hash, $entry); 
    }
    
    
    public function hIncrByCache($key, $field, $add)
    { 
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hIncrBy($hash, $field, $add); 
    }

    public function hIncrByFloatCache($key, $field, $add)
    { 
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hIncrByFloat($hash, $field, $add); 
    }

}
?>

控制器中使用

<?php 


namespace app\data\controller; 
 
use app\common\logic\RedisLogic;   

class Test extends Base 
{ 
    public function redis()
    { 
		if(class_exists('Redis')){ 
		    echo '<br>服务器支持Redis服务<br>';  
		    $redis = new RedisLogic();     
		    $redis->setCache('key',12365478); 
		    echo '<br>key:',$redis->getCache('key'); 
		    echo $redis->delete('key');   
		}
		else{
		    echo '服务器不支持Redis服务';
		}
    }
    
}


 

使用redis的场景和对应示例代码icon-default.png?t=N7T8https://www.cnblogs.com/liuxinyustu/articles/13504257.html

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值