当前位置: 首页 > news >正文

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,失败返回falsepublic 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

http://www.lryc.cn/news/296211.html

相关文章:

  • Go语言Gin框架安全加固:全面解析SQL注入、XSS与CSRF的解决方案
  • MySQL数据库基础与SELECT语句使用梳理
  • scikit-learn 1.3.X 版本 bug - F1 分数计算错误
  • Python面试题19-24
  • 《Django+React前后端分离项目开发实战:爱计划》 01 项目整体概述
  • 从零开始 TensorRT(4)命令行工具篇:trtexec 基本功能
  • 基于SpringBoot+Vue的校园博客管理系统
  • 基于 SpringBoot 和 Vue.js 的权限管理系统部署教程
  • Redis篇之集群
  • JUnit 5 注解总结与解析
  • CSS综合案例4
  • WifiConfigStore初始化读取-Android13
  • 【Spring源码解读!底层原理进阶】【下】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨
  • 从零开始手写mmo游戏从框架到爆炸(六)— 消息处理工厂
  • Go基础学习笔记-知识点
  • jvm几个常见面试题整理
  • ReentrantLock 和 公平锁
  • 使用Postman做API自动化测试
  • 入门指南|Chat GPT 的兴起:它如何改变数字营销格局?
  • 【C#】.net core 6.0 创建默认Web应用,以及默认结构讲解,适合初学者
  • Linux中的numactl命令指南
  • AD域国产替代方案,助力某金融企业麒麟信创电脑实现“真替真用”
  • 抽象springBoot报错
  • Linux的打包压缩与解压缩---tar、xz、zip、unzip
  • 在angular12中proxy.conf.json中配置详解
  • PyTorch 中音频信号处理库torchaudio的详细介绍
  • OpenAI研究揭示:ChatGPT对生物武器制造影响有限
  • IntelliJ IDEA 2023.3发布,AI 助手出世,新特性杀麻了!!
  • async 与 await(JavaScript)
  • GPT-1, GPT-2, GPT-3, GPT-3.5, GPT-4论文内容解读