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