当前路径:vendor/topthink/framework/src/think/cache/driver/Redis.php <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- declare (strict_types = 1); namespace think\cache\driver; use think\cache\Driver; /** * Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好 * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动 * * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis * @author 尘缘 <130775@qq.com> */ class Redis extends Driver { /** @var \Predis\Client|\Redis */ protected $handler; /** * 配置参数 * @var array */ protected $options = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', 'tag_prefix' => 'tag:', 'serialize' => [], ]; /** * 架构函数 * @access public * @param array $options 缓存参数 */ public function __construct(array $options = []) { if (!empty($options)) { $this->options = array_merge($this->options, $options); } if (extension_loaded('redis')) { $this->handler = new \Redis; if ($this->options['persistent']) { $this->handler->pconnect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']); } else { $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']); } if ('' != $this->options['password']) { $this->handler->auth($this->options['password']); } } elseif (class_exists('\Predis\Client')) { $params = []; foreach ($this->options as $key => $val) { if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) { $params[$key] = $val; unset($this->options[$key]); } } if ('' == $this->options['password']) { unset($this->options['password']); } $this->handler = new \Predis\Client($this->options, $params); $this->options['prefix'] = ''; } else { throw new \BadFunctionCallException('not support: redis'); } if (0 != $this->options['select']) { $this->handler->select( (int) $this->options['select']); } } /** * 判断缓存 * @access public * @param string $name 缓存变量名 * @return bool */ public function has($name): bool { return $this->handler->exists($this->getCacheKey($name)) ? true : false; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @param mixed $default 默认值 * @return mixed */ public function get($name, $default = null) { $this->readTimes++; $value = $this->handler->get($this->getCacheKey($name)); if (false === $value || is_null($value)) { return $default; } return $this->unserialize($value); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer|\DateTime $expire 有效时间(秒) * @return bool */ public function set($name, $value, $expire = null): bool { $this->writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } $key = $this->getCacheKey($name); $expire = $this->getExpireTime($expire); $value = $this->serialize($value); if ($expire) { $this->handler->setex($key, $expire, $value); } else { $this->handler->set($key, $value); } return true; } /** * 自增缓存(针对数值缓存) * @access public * @param string $name 缓存变量名 * @param int $step 步长 * @return false|int */ public function inc(string $name, int $step = 1) { $this->writeTimes++; $key = $this->getCacheKey($name); return $this->handler->incrby($key, $step); } /** * 自减缓存(针对数值缓存) * @access public * @param string $name 缓存变量名 * @param int $step 步长 * @return false|int */ public function dec(string $name, int $step = 1) { $this->writeTimes++; $key = $this->getCacheKey($name); return $this->handler->decrby($key, $step); } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return bool */ public function delete($name): bool { $this->writeTimes++; $result = $this->handler->del($this->getCacheKey($name)); return $result > 0; } /** * 清除缓存 * @access public * @return bool */ public function clear(): bool { $this->writeTimes++; $this->handler->flushDB(); return true; } /** * 删除缓存标签 * @access public * @param array $keys 缓存标识列表 * @return void */ public function clearTag(array $keys): void { // 指定标签清除 $this->handler->del($keys); } /** * 追加(数组)缓存数据 * @access public * @param string $name 缓存标识 * @param mixed $value 数据 * @return void */ public function push(string $name, $value): void { $this->handler->sAdd($name, $value); } /** * 获取标签包含的缓存标识 * @access public * @param string $tag 缓存标签 * @return array */ public function getTagItems(string $tag): array { return $this->handler->sMembers($tag); } }
相关源码
- 可旋转的彩色立方体C#源代码2021-10-29
- 在线考试系统2021-10-15
- EduSoho开源网校系统源码2019-06-27
- 仿拼多多小程序商城源码2019-06-06
- PHP5网站运行监测系统源码2017-04-14
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途)
地址:北京市海淀区大恒科技大厦五层 邮编:100080
Floor 5th,Daheng Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2022. 京ICP备09089570号 | 京公网安备11010702000869号
联系电话(Tel):4008-010-151(免长途)
地址:北京市海淀区大恒科技大厦五层 邮编:100080
Floor 5th,Daheng Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2022. 京ICP备09089570号 | 京公网安备11010702000869号