class RedisForCourseStatus: """ 封装有关课程的各种状态的数据库操作 key format: STATUS:COURSE_STATUS:COURSE_ID:xxx 存储的是一个值CourseStatus(参考edu.py) """ __PRIMARY_KEY = 'COURSE_ID' __TABLE_NAME = 'COURSE_STATUS' __PREFIX = REDIS_DB_NAME + ':' + __TABLE_NAME + ':' + __PRIMARY_KEY def __init__(self): self.__conn = Redis().conn def createCourse(self, course_id): """ 插入一条课堂状态记录 :param course_id: 课程唯一标识 :return: """ # format: STATUS:COURSE_STATUS:COURSE_ID:xxx key = self.__PREFIX + ':' + str(course_id) self.__conn.set(key, CourseStatus.OffLine) def getCourseStatus(self, course_id): """ 获取当前课程状态 :param course_id: 课程唯一标识 :return: """ # format: STATUS:COURSE_STATUS:COURSE_ID:xxx key = self.__PREFIX + ':' + str(course_id) if not self.__conn.exists(key): self.createCourse(course_id=course_id) return int(self.__conn.get(key)) def offLine(self, course_id): """ 课堂结束 :return: """ # format: STATUS:LESSON_STATUS:ID:xxx key = self.__PREFIX + ':' + str(course_id) self.__conn.set(key, CourseStatus.OffLine) def onLine(self, course_id): """ 正在上课 :return: """ # format: STATUS:LESSON_STATUS:ID:xxx key = self.__PREFIX + ':' + str(course_id) self.__conn.set(key, CourseStatus.OnLine) def cantJoinIn(self, course_id): """ 课堂不可中途加入 :return: """ # format: STATUS:LESSON_STATUS:ID:xxx key = self.__PREFIX + ':' + str(course_id) self.__conn.set(key, CourseStatus.CantJoinIn) def waiting(self, course_id): """ 在房间等待中,未开始上课 :return: """ # format: STATUS:LESSON_STATUS:ID:xxx key = self.__PREFIX + ':' + str(course_id) self.__conn.set(key, CourseStatus.Waiting)
class RedisForUserStatus: """ 封装有关用户的各种状态的数据库操作 key format: STATUS:USER_STATUS:UID:xxx 存储的是一个值UserStatus(参考edu.py) """ __PRIMARY_KEY = 'UID' __TABLE_NAME = 'USER_STATUS' __PREFIX = REDIS_DB_NAME + ':' + __TABLE_NAME + ':' + __PRIMARY_KEY def __init__(self): self.__conn = Redis().conn def signUp(self, uid): """ 用户注册 :param uid: 用户唯一标识 :return: """ # format: STATUS:USER_STATUS:UID:xxx key = self.__PREFIX + ':' + str(uid) self.__conn.set(key, UserStatus.Free) def getUserStatus(self, uid): """ 获取当前课程状态 :param uid: 用户唯一标识 :return: """ # format: STATUS:USER_STATUS:UID:xxx key = self.__PREFIX + ':' + str(uid) if not self.__conn.exists(key): self.signUp(uid=uid) return int(self.__conn.get(key)) def free(self, uid): """ 空闲 :param uid: 用户唯一标识 :return: """ # format: STATUS:User_STATUS:UID:xxx key = self.__PREFIX + ':' + str(uid) self.__conn.set(key, UserStatus.Free) def inRoom(self, uid): """ 在房间中 :param uid: 用户唯一标识 :return: """ # format: STATUS:LESSON_STATUS:UID:xxx key = self.__PREFIX + ':' + str(uid) self.__conn.set(key, UserStatus.InRoom) def inClass(self, uid): """ 在课堂中 :param uid: 用户唯一标识 :return: """ # format: STATUS:LESSON_STATUS:UID:xxx key = self.__PREFIX + ':' + str(uid) self.__conn.set(key, UserStatus.InClass)