コード例 #1
0
class RedisForCourse:
    """ 封装有关课程的数据库操作

        key format: STATUS:COURSE:COURSE_ID:xxx
        存储的是一个lesson_id
        表示当前课程最近一堂课的lesson_id
    """
    __PRIMARY_KEY = 'COURSE_ID'
    __TABLE_NAME = 'COURSE'
    __PREFIX = REDIS_DB_NAME + ':' + __TABLE_NAME + ':' + __PRIMARY_KEY

    def __init__(self):
        self.__conn = Redis().conn

    def updateLesson(self, course_id, lesson_id):
        """ 插入一条课程状态记录

        :param course_id: 课程唯一标识
        :param lesson_id: 课程下课堂唯一标识
        :return:
        """
        # format: STATUS:COURSE:COURSE_ID:xxx
        key = self.__PREFIX + ':' + str(course_id)
        self.__conn.set(key, lesson_id)

    def getLastlyLessonID(self, course_id):
        """ 获取课程最近一堂课的lesson_id

        :param course_id: 课程唯一标识
        :return:
        """
        # format: STATUS:COURSE:COURSE_ID:xxx
        key = self.__PREFIX + ':' + str(course_id)

        return self.__conn.get(key)
コード例 #2
0
class RedisForOnlineList:
    """ 封装有关课堂在线列表的数据库操作

        key format: STATUS:ONLINE_LIST:ID:(lesson_id)_(uid)
        存储的是一个uid
        这个key的用法实际上并不是很好,但是能实现需求
    """
    # 主键是由lesson_id与uid组成,中间通过下划线连接
    __PRIMARY_KEY = 'ID'
    __TABLE_NAME = 'ONLINE_LIST'
    __PREFIX = REDIS_DB_NAME + ':' + __TABLE_NAME + ':' + __PRIMARY_KEY

    def __init__(self):
        self.__conn = Redis().conn

    def refresh(self, lesson_id, uid):
        """ 刷新用户状态,即重置key的存活时间

        :param lesson_id: 课程下课堂唯一标识
        :param uid: 用户唯一标识
        :return:
        """
        # format: STATUS:ONLINE_LIST:ID:(lesson_id)_(uid)
        key = self.__PREFIX + ':' + str(lesson_id) + '_' + str(uid)

        self.__conn.setex(name=key, value=uid, time=5)

    def getOnlineList(self, lesson_id):
        """ 获取该课堂在线的uid列表

        :param lesson_id: 课程下课堂唯一标识
        :return:
        """
        online_list = []
        key_list = self.__conn.keys(self.__PREFIX + ':' + str(lesson_id) +
                                    '_*')
        for key in key_list:
            online_list.append(self.__conn.get(key))

        return online_list
コード例 #3
0
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)
コード例 #4
0
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)