| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use common\components\wxUtil;
- use common\models\xhTrainStudent;
- class xhTrainStudentService {
- public static function getByUserId($userId)
- {
- $preKey = dict::getCacheKey('xhTrainStudent');
- $cacheKey = $preKey.$userId;
- $userAsset = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($userAsset)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($userAsset as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $userArr = [];
- foreach($arr as $key => $val){
- $userArr[$val['key']] = $val['value'];
- }
- unset($userArr['info_already_get_by_sql']);
- return $userArr;
- }
- $userAsset = xhTrainStudent::find()->where(['userId' => $userId])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($userAsset)){
- foreach($userAsset as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $userAsset;
- }
- public static function refreshByUserId($userId)
- {
- $cacheKey = dict::getCacheKey('xhTrainStudent').$userId;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getByUserId($userId);
- }
- public static function updateByUserId($userId,$data)
- {
- xhTrainStudent::updateByCondition(['userId' => $userId], $data);
- self::refreshByUserId($userId);
- }
- public static function add($data)
- {
- $student = xhTrainStudent::add($data);
- $userId = $student['userId'];
- self::refreshByUserId($userId);
- }
- }
|