redis->executeCommand('EXISTS', [$cacheKey]); if(!$exist){//不存在 此键值 $data = $class::find()->where(['id' => $id])->asArray()->one(); $fresh = empty($data) ? false : true; }else{//存在 此键值(是否强制从数据库里取,并更新缓存数据) if($forceUpdate){ $data = $class::find()->where(['id' => $id])->asArray()->one(); $fresh = empty($data) ? false : true; }else{ throw new \Exception('Cache Key: '. $cacheKey . ' is existed'); } } if($fresh){ Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//先删除 $params = [$cacheKey]; if(!empty($data)){ foreach($data as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET', $params); if($expire > 0){ Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]); } } return $data; } /** * 传数组数据更新缓存 * @param $data 数据 * @param string $cacheKey 缓存键 * @param int $expire 过期时间(默认无过期) * @return bool * @throws \Exception */ public static function setCacheByData(array $data, $model, $cacheKey = '', $expire = 0){ $fresh = false;//是否更新缓存 if(empty($cacheKey)){ $preKey = dict::getCacheKey($model); if(empty($preKey)){ throw new \Exception('get ' . $model . ' model cache key fail from dict::getCacheKey'); } if(!isset($data['id'])){ throw new \Exception('parameter data array not exist key id'); } $cacheKey = $preKey.$data['id']; } //是否存在 此键值 $exist = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]); if(!$exist){//不存在 此键值 } $fresh = (!empty($data)) ? true : false;//是数组且不为空 if($fresh){ $params = [$cacheKey]; foreach($data as $key => $val){ $params[] = $key; $params[] = $val; } Yii::$app->redis->executeCommand('HMSET', $params); if($expire > 0){ Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]); } return true; } return false; } /** * 传数组数据更新缓存(此缓存键必须存在,否则报错--键不存在! ///- 安全起见,只能函数setCacheById、setCacheByData生成新键 -///) * @param $data 数据 * @param string $cacheKey 缓存键 * @param int $expire 过期时间(默认无过期) * @return bool * @throws \Exception */ public static function updateCacheByData(array $data, $model, $cacheKey = '', $expire = 0){ $fresh = false;//是否更新缓存 if(empty($cacheKey)){ $preKey = dict::getCacheKey($model); if(empty($preKey)){ throw new \Exception('get ' . $model . ' model cache key fail from dict::getCacheKey'); } if(!isset($data['id'])){ throw new \Exception('parameter data array not exist key id'); } $cacheKey = $preKey.$data['id']; } //是否存在 此键值 $exist = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]); if(!$exist){//不存在 此键值 throw new \Exception('Cache Key: '. $cacheKey . ' is not existed'); } $fresh = (!empty($data)) ? true : false;//是数组且不为空 if($fresh){ $params = [$cacheKey]; foreach($data as $key => $val){ $params[] = $key; $params[] = $val; } Yii::$app->redis->executeCommand('HMSET', $params); if($expire > 0){ Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]); } return true; } return false; } /** * 获取Hgetall(哈希表)缓存 * @param $cacheKey * @return array */ public static function getCache($cacheKey){ $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]); //$cacheData = Yii::$app->redis->hGetAll($key); if(!empty($cacheData)){ $arr = []; $i = 0; $x = 0; foreach($cacheData as $key => $val){ $i++; if($i%2 == 0){ $arr[$x]['value'] = $val; $x++; }else{ $arr[$x]['key'] = $val; } } $cacheDataArr = []; foreach($arr as $key => $val){ $cacheDataArr[$val['key']] = $val['value']; } return $cacheDataArr; } return false; } }