| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- class baseService{
- /**
- * 通过主键Id从数据库取数据并生成缓存 (非强制更新缓存时($forceUpdate=false),此缓存键必须不存在,否则报错--键已存在!)
- * @param $id 表主键ID
- * @param $model 表对应的模型
- * @param string $cacheKey 缓存键
- * @param int $expire 过期时间(默认无过期)
- * @param bool $forceUpdate 是否强制从数据库里取,并更新缓存数据(false:默认在键不存时设置缓存; true:强制从数据库取数据更新缓)
- * @return mixed
- */
- public static function setCacheById($id, $model, $cacheKey = '', $expire = 0, $forceUpdate = false){
- $fresh = false;//是否更新缓存
- if(empty($cacheKey)){
- $preKey = dict::getCacheKey($model);
- if(empty($preKey)){
- throw new \Exception('get' . $model . 'model cache key fail from dict::getCacheKey');
- }
- $cacheKey = $preKey.$id;
- }
- $class = "\common\models\\$model";
- //是否存在此键值
- $exist = Yii::$app->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;
- }
- }
|