def get(cls, id_): cache_key = cls._student_cache_key % id_ if mc.get(cache_key): return pickle.loads(bytes.fromhex(mc.get(cache_key))) student = cls.query.filter_by(id=id_).first() if student: mc.set(cache_key, pickle.dumps(student).hex()) mc.expire(cache_key, ONE_HOUR) return student
def get(cls, id_): cache_key = cls._room_cache_key % id_ if mc.get(cache_key): return pickle.loads(bytes.fromhex(mc.get(cache_key))) room = cls.query.filter_by(room_id=id_).first() if room: mc.set(cache_key, pickle.dumps(room).hex()) mc.expire(cache_key, ONE_HOUR) return room
def access_token(self): access_token_cache = mc.get(self._wechat_access_token_cache_key) if not access_token_cache: access_token = self.get_access_token() if access_token: mc.set(self._wechat_access_token_cache_key, access_token, 6600) return access_token else: return None else: return access_token_cache
def get_by_open_id(open_id): cache_key = WeChatUser._wechat_user_by_open_id_cache_key % open_id if mc.get(cache_key): return pickle.loads(bytes.fromhex(mc.get(cache_key))) wechat_user = WeChatUser.query.filter_by(openid=open_id).first() if wechat_user: mc.set(cache_key, pickle.dumps(wechat_user).hex()) mc.expire(cache_key, ONE_DAY) else: raise WechatUserNotFoundError return wechat_user
def get(cls, id_): cache_key = cls._room_cache_key % id_ if mc.get(cache_key): return pickle.loads(bytes.fromhex(mc.get(cache_key))) room = cls.query.filter_by(id=id_).first() # 处理头像 row = User.query.join('wechat_user').filter( User.id.in_([data[0] for data in json.loads(room.members) ])).with_entities('chess_user.id', 'chess_wechat_user.headimg', 'chess_wechat_user.nickname').all() headimgs = [ dict(zip(['uid', 'img', 'nickname'], item)) for item in row ] setattr(room, 'headimgs', headimgs) if room: mc.set(cache_key, pickle.dumps(room).hex()) mc.expire(cache_key, ONE_HOUR) return room