| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\models\xhActive;
- class xhActiveService {
- public static function add($data)
- {
- $active = xhActive::add($data);
- $id = $active['id'];
- self::refreshById($id);
- return $active;
- }
- public static function refreshById($id)
- {
- $activeKey = dict::getCacheKey('xhActive').$id;
- Yii::$app->redis->executeCommand('DEL', [$activeKey]);
- self::getById($id);
- }
-
- public static function updateById($id,$data)
- {
- xhActive::updateById($id, $data);
- $activeKey = dict::getCacheKey('xhActive').$id;
- $activeParams = [$activeKey];
- foreach($data as $key => $val){
- $activeParams[] = $key;
- $activeParams[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET',$activeParams);//更新缓存信息
- }
- public static function getById($id)
- {
- $cacheKey = dict::getCacheKey('xhActive').$id;
- $active = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($active)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($active as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $activeArr = [];
- foreach($arr as $key => $val){
- $activeArr[$val['key']] = $val['value'];
- }
- unset($activeArr['info_already_get_by_sql']);
- return $activeArr;
- }
- $active = xhActive::find()->where(['id' => $id])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($active)){
- foreach($active as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $active;
- }
- }
|