<?php
namespace Biz\Db\Redis;
class Cache {
	private $redis;
	function __construct() {
		try {
			$this->redis = C()->get('redis');
			if ($this->redis) {
				$this->redis = $this->redis->get();
			}
		} catch (\Exception $ex) {

		}

	}
	function checkRedis() {
		return $this->redis ? 1 : 0;
	}
	function get($key) {
		if (!$this->checkRedis()) {
			return;
		}
		return $this->redis->get($key);
	}
	function set($key, $value, $expire = 0) {
		if (!$this->checkRedis()) {
			return;
		}
		if ($expire > 0) {
			return $this->redis->setex($key, $value, $expire);
		}
		return $this->redis->set($key, $value);
	}
	//自动序列化
	function sets($key, $value, $expire = 0) {

		$value = json_encode($value);
		return $this->set($key, $value, $expire);
	}
	//自动序列化
	function gets($key) {
		$value = $this->get($key);
		return !$value ? null : json_decode($value, 1);
	}
}
