当前位置:Gxlcms > PHP教程 > tp5缓存驱动Predisphp没有Redis驱动模块的时候使用Predis连接使用redis

tp5缓存驱动Predisphp没有Redis驱动模块的时候使用Predis连接使用redis

时间:2021-07-01 10:21:17 帮助过:10人阅读

这篇文章主要介绍了关于tp5缓存驱动Predis php没有Redis驱动模块的时候使用Predis连接使用redis ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

没有Redis驱动模块的时候使用Predis连接使用redis

安装

  1. composer require predis/predis

tp5 缓存驱动增加Predis.php

  1. <?phpnamespace think\cache\driver;use think\cache\Driver;/**
  2. * Predis缓存驱动,就是php没有redis扩展的时候使用,适合单机部署、有前端代理实现高可用的场景,性能最好
  3. * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
  4. *
  5. * 要求安装Predis扩展:https://github.com/nrk/predis
  6. *
  7. */class Predis extends Driver{
  8. protected $options = [ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', 'database' => 15,
  9. ];
  10. /**
  11. * 构造函数
  12. * @param array $options 缓存参数
  13. * @access public
  14. */
  15. public function __construct($options = [])
  16. {
  17. if (!empty($options)) {
  18. $this->options = array_merge($this->options, $options);
  19. } $func = $this->options['persistent'] ? 'pconnect' : 'connect';
  20. $this->handler = new \Predis\Client;
  21. $this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
  22. if ('' != $this->options['password']) {
  23. $this->handler->auth($this->options['password']);
  24. }
  25. if (0 != $this->options['select']) {
  26. $this->handler->select($this->options['select']);
  27. }
  28. } /**
  29. * 判断缓存
  30. * @access public
  31. * @param string $name 缓存变量名
  32. * @return bool
  33. */
  34. public function has($name)
  35. {
  36. return $this->handler->get($this->getCacheKey($name)) ? true : false;
  37. } /**
  38. * 读取缓存
  39. * @access public
  40. * @param string $name 缓存变量名
  41. * @param mixed $default 默认值
  42. * @return mixed
  43. */
  44. public function get($name, $default = false)
  45. {
  46. $value = $this->handler->get($this->getCacheKey($name));
  47. if (is_null($value)) {
  48. return $default;
  49. }
  50. $jsonData = json_decode($value, true); // 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
  51. return (null === $jsonData) ? $value : $jsonData;
  52. } /**
  53. * 写入缓存
  54. * @access public
  55. * @param string $name 缓存变量名
  56. * @param mixed $value 存储数据
  57. * @param integer $expire 有效时间(秒)
  58. * @return boolean
  59. */
  60. public function set($name, $value, $expire = null)
  61. {
  62. if (is_null($expire)) {
  63. $expire = $this->options['expire'];
  64. }
  65. if ($this->tag && !$this->has($name)) {
  66. $first = true;
  67. }
  68. $key = $this->getCacheKey($name); //对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
  69. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  70. if (is_int($expire) && $expire) {
  71. $result = $this->handler->setex($key, $expire, $value);
  72. } else {
  73. $result = $this->handler->set($key, $value);
  74. }
  75. isset($first) && $this->setTagItem($key);
  76. return $result;
  77. } /**
  78. * 自增缓存(针对数值缓存)
  79. * @access public
  80. * @param string $name 缓存变量名
  81. * @param int $step 步长
  82. * @return false|int
  83. */
  84. public function inc($name, $step = 1)
  85. {
  86. $key = $this->getCacheKey($name);
  87. return $this->handler->incrby($key, $step);
  88. } /**
  89. * 自减缓存(针对数值缓存)
  90. * @access public
  91. * @param string $name 缓存变量名
  92. * @param int $step 步长
  93. * @return false|int
  94. */
  95. public function dec($name, $step = 1)
  96. {
  97. $key = $this->getCacheKey($name);
  98. return $this->handler->decrby($key, $step);
  99. } /**
  100. * 删除缓存
  101. * @access public
  102. * @param string $name 缓存变量名
  103. * @return boolean
  104. */
  105. public function rm($name)
  106. {
  107. return $this->handler->delete($this->getCacheKey($name));
  108. } /**
  109. * 清除缓存
  110. * @access public
  111. * @param string $tag 标签名
  112. * @return boolean
  113. */
  114. public function clear($tag = null)
  115. {
  116. if ($tag) { // 指定标签清除
  117. $keys = $this->getTagItem($tag);
  118. foreach ($keys as $key) {
  119. $this->handler->delete($key);
  120. }
  121. $this->rm('tag_' . md5($tag));
  122. return true;
  123. }
  124. return $this->handler->flushDB();
  125. }
  126. }

相关推荐:

PHP 中TP5 Request 请求对象的方法

以上就是tp5缓存驱动Predis php没有Redis驱动模块的时候使用Predis连接使用redis 的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行