【opencart3源码分析】redis缓存类redis.php

<?php
namespace Cache;
/**
 * Redis 缓存类
 * @package Cache
 */
class Redis {
	private $expire;
	private $cache;

	// 构造方法
	public function __construct($expire) {
	    // 过期时间
		$this->expire = $expire;
        //实例化类
		$this->cache = new \Redis();
		$this->cache->pconnect(CACHE_HOSTNAME, CACHE_PORT);
	}

	// 读取缓存
	public function get($key) {
		$data = $this->cache->get(CACHE_PREFIX . $key);

		return json_decode($data, true);
	}

	//设置缓存
	public function set($key, $value) {
		$status = $this->cache->set(CACHE_PREFIX . $key, json_encode($value));

		if ($status) {
			$this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire);
		}

		return $status;
	}

	// 删除缓存
	public function delete($key) {
		$this->cache->delete(CACHE_PREFIX . $key);
	}
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/81431002
今日推荐