🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <?php /** * Created by PhpStorm. * User: jc * Date: 2018/7/5 * Time: 13:33 */ namespace Common\Logic; class RedisLogic extends BaseLogic { public $handler; public $options; public function __construct($options = array()) { if (!extension_loaded('redis')) { E(L('_NOT_SUPPORT_') . ':redis'); } $redis_host = D("Common/Config")->where(['cfg_name'=>'redis_host'])->find(); $redis_host = $redis_host['cfg_data'] ? $redis_host['cfg_data'] : '127.0.0.1'; $redis_port = D("Common/Config")->where(['cfg_name'=>'redis_port'])->find(); $redis_port = $redis_port['cfg_data'] ? $redis_port['cfg_data'] : '6379'; $redis_passwd = D("Common/Config")->where(['cfg_name'=>'redis_passwd'])->find(); $redis_passwd = $redis_passwd['cfg_data'] ? $redis_passwd['cfg_data'] : ''; $options = array( 'host' => $redis_host ? $redis_host : C('REDIS_HOST') , 'port' => $redis_port ? $redis_port : C('REDIS_PORT'), 'password' => $redis_passwd ? $redis_passwd :C('REDIS_PASSWORD') , 'timeout' => C('DATA_CACHE_TIMEOUT') ?: false, 'persistent' => false, ); $this->options = $options; $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length']) ? $options['length'] : 0; $func = $options['persistent'] ? 'pconnect' : 'connect'; $this->handler = new \Redis; false === $options['timeout'] ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']); if ('' != $options['password']) { $this->handler->auth($options['password']); } } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function getVa($name) { //N('cache_read', 1); $value = $this->handler->get($this->options['prefix'] . $name); //$jsonData = json_decode($value, true); return (null === $value) ? '' : $value; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function setVa($name, $value, $expire = null) { N('cache_write', 1); if (is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'] . $name; //对数组/对象数据进行缓存处理,保证数据完整性 $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value; if (is_int($expire) && $expire) { $result = $this->handler->setex($name, $expire, $value); } else { $result = $this->handler->set($name, $value); } if ($result && $this->options['length'] > 0) { // 记录缓存队列 //$this->queue($name); } return $result; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return $this->handler->delete($this->options['prefix'] . $name); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flushDB(); } } ````