CacheModel::get PHP Method

get() public method

获取缓存操作,支持mutex模式 mutex使用注意 1.设置缓存(set)时,需要设置有效时间 2.获取缓存(get)时,需要主动创建缓存
public get ( string $_key, boolean $mutex = false ) : mix
$_key string 缓存Key值
$mutex boolean 是否启用mutex模式,默认为不启用
return mix 缓存数据
    public function get($_key, $mutex = false)
    {
        $key = C('DATA_CACHE_PREFIX') . $_key;
        // 静态缓存
        /*      if(isset(self::$_cacheHash[$key])){
                    return self::$_cacheHash[$key];
                }*/
        $sc = static_cache('cache_' . $key);
        if (!empty($sc)) {
            return $sc;
        }
        // 获取缓存数据
        $data = $this->handler->get($key);
        // 未设置缓存
        if (!$data) {
            return false;
        }
        // mutex模式未开启
        if (!$mutex) {
            if ($data['CacheExpire'] < 0 || $data['CacheMtime'] + $data['CacheExpire'] > time()) {
                return $this->_returnData($data['CacheData'], $key);
            } else {
                // 过期,清理原始缓存
                $this->rm($_key);
                return false;
            }
        }
        // mutex模式开启
        if ($data['CacheMtime'] + $data['CacheExpire'] <= time()) {
            //正常情况,有过期时间设置的mutex模式
            if ($data['CacheExpire'] > 0) {
                $data['CacheMtime'] = time();
                $this->handler->set($key, $data);
                // 返回false,让调用程序去主动更新缓存
                static_cache('cache_' . $key, false);
                return false;
            } else {
                //异常情况,没有设置有效期的时候,永久有效的时候
                if (!$data['CacheData']) {
                    $this->rm($_key);
                    return false;
                }
                return $this->_returnData($data['CacheData'], $key);
            }
        } else {
            return $this->_returnData($data['CacheData'], $key);
        }
    }