Exemple #1
0
 def cache_avatar(student_id, avatar_url):
     cache_key = Student._avatar_cache_key % student_id
     response = requests.get(avatar_url)
     image = response.content
     mc.set(cache_key, pickle.dumps(image).hex())
     mc.expire(cache_key, ONE_WEEK)
     return image
Exemple #2
0
 def get(cls, id_):
     cache_key = cls._course_post_by_id_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     post = CoursePost.query.get(id_)
     if post:
         mc.set(cache_key, pickle.dumps(post).hex())
         mc.expire(cache_key, ONE_HOUR)
     return post
Exemple #3
0
 def get_by_open_id(cls, open_id):
     cache_key = cls._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 = cls.query.filter_by(open_id=open_id).first()
     if wechat_user:
         mc.set(cache_key, pickle.dumps(wechat_user).hex())
         mc.expire(cache_key, ONE_DAY)
     return wechat_user
Exemple #4
0
 def get_all(cls):
     cache_key = cls._all_course_cache_key
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     else:
         courses = Course.query.all()
         mc.set(cache_key, pickle.dumps(courses).hex())
         mc.expire(cache_key, ONE_DAY)
         return courses
Exemple #5
0
 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
Exemple #6
0
 def remaining_viewcount(self):
     cache_key = self._remaining_viewcount_by_student_cache_key % self.id
     if mc.get(cache_key):
         viewcount = int(mc.get(cache_key))
         return viewcount
     else:
         mc.set(cache_key, self.MAX_VIEWCOUNT)
         mc.expire(cache_key, ONE_DAY)
         return self.MAX_VIEWCOUNT
 def get(cls, id_):
     cache_key = cls._course_post_demand_by_id_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_demand = CourseDemand.query.get(id_)
     if course_demand:
         mc.set(cache_key, pickle.dumps(course_demand).hex())
         mc.expire(cache_key, ONE_DAY)
     return course_demand
Exemple #8
0
 def demand(self):
     cache_key = self._course_post_demand_by_post_id_cache_key % self.id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_demand = CourseDemand.get_by_post(self.id)
     if course_demand and course_demand.course_id:
         mc.set(cache_key, pickle.dumps(course_demand).hex())
         mc.expire(cache_key, ONE_DAY)
         return course_demand
     return None
Exemple #9
0
 def supply(self):
     cache_key = self._course_post_supply_by_post_id_cache_key % self.id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_supply = CourseSupply.get_by_post(self.id)
     if course_supply and course_supply.course_id:
         mc.set(cache_key, pickle.dumps(course_supply).hex())
         mc.expire(cache_key, ONE_DAY)
         return course_supply
     return None
Exemple #10
0
 def get(cls, id_):
     cache_key = cls._course_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     else:
         course = Course.query.get(id_)
         if course:
             mc.set(cache_key, pickle.dumps(course).hex())
             mc.expire(cache_key, ONE_DAY)
         return course
Exemple #11
0
    def get_by_third_session_key(cls, third_session_key):
        id_ = mc.get(cls._id_by_open_id_cache_key % third_session_key)

        wechat_session = cls.get(id_) if id_ else cls.query.filter_by(
            third_session_key=third_session_key).first()

        if wechat_session and not wechat_session.expired:
            mc.set(cls._id_by_open_id_cache_key % wechat_session.open_id, wechat_session.id)
            mc.expire(cls._id_by_open_id_cache_key % wechat_session.open_id, HALF_DAY)
            return wechat_session
        return None
Exemple #12
0
 def get(cls, id_):
     cache_key = cls._wechat_session_by_id_cache_key % id_
     if mc.get(cache_key):
         wechat_session = pickle.loads(bytes.fromhex(mc.get(cache_key)))
         mc.expire(cache_key, HALF_DAY)
         return wechat_session
     wechat_session = cls.query.get(id_)
     if wechat_session:
         mc.set(cache_key, pickle.dumps(wechat_session).hex())
         mc.expire(cache_key, HALF_DAY)
     return wechat_session
Exemple #13
0
 def gets(cls, student_id, post_id, post_type=PostType.course_post):
     cache_key = cls._records_by_student_and_post_and_type_cache_key % (
         student_id, post_id, post_type.value)
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     records = cls.query.filter_by(student_id=student_id,
                                   post_id=post_id,
                                   post_type_=post_type.value).all()
     if records:
         mc.set(cache_key, pickle.dumps(records).hex())
         mc.expire(cache_key, ONE_DAY)
     return records
Exemple #14
0
    def add(cls, open_id, session_key, expires_in=ONE_DAY):
        third_session_key = uuid.uuid4().hex
        instance = cls.get_by_open_id(open_id)
        if instance:
            instance.update(session_key, third_session_key, expires_in)
            return third_session_key

        expire_time = datetime.now() + timedelta(seconds=expires_in)
        wechat_session = WechatSession(
            open_id, session_key, third_session_key, expire_time)

        db.session.add(wechat_session)
        db.session.commit()
        mc.set(cls._id_by_open_id_cache_key % open_id, wechat_session.id)
        mc.expire(cls._id_by_open_id_cache_key % open_id, HALF_DAY)
        mc.set(cls._wechat_session_by_id_cache_key % wechat_session.id, wechat_session)
        mc.expire(cls._wechat_session_by_id_cache_key % wechat_session.id, HALF_DAY)
        return third_session_key