First, the scene needs
Application scenarios such as sign points to send, receive attendance incentives, roughly requirements are as follows:
- Send a sign such as one day integral, continuous integral sign 2 Tiansong 2, 3 Tiansong 3 points, more than 3 days were sent to the integrator 3 and the like.
- If continuous attendance is interrupted, reset the count, the count is reset at the beginning of each month.
- It displays the user check-ins a month and the first sign of the time.
- Show users monthly check-ins, you can switch the date on the calendar display and so on ...... control.
Second, the code
The following code logic no points, only sign
import datetime import redis class Redis(object): def __init__(self): self.redis_store = redis.Redis( host="127.0.0.1", port="6379", db="8" ) class RedisSignIn(Redis): @staticmethod def GetDaliySignKey(time=datetime.datetime.now()): # 拼接redis的key bitmap_key_daily_sign = "daily_sign_{date}" return bitmap_key_daily_sign.format(date=time.date()) def UserDailySign(self, session): user_id = session.get("user") if not user_id: print( "验证失败") return if self.UserDaliySignByUserId(user_id): rec = self.GetSignRecordByUserId(user_id, 7) l = len(rec.split('0')[0]) print(L) # consecutive sign IF L>. 1 : Pass IF L>. 6 : Pass return # acquired record monthly check- DEF GetUserDailySignRecordMonthly (Self, the session): user_id = Session.get ( " User " ) IF Not user_id: Print ( " verification failure " ) return RES = self.GetSignRecordByUserId (user_id) Print (int (RES, 2 )) #10 converts the binary data into binary, the decimal number to get returned to the front end, the front end and then resolved into a binary number is judged return int (RES, 2 ) DEF GetSignRecordByUserId (Self, user_id, = 30 Days ): # open pipeline simulation transaction with self.redis_store.pipeline (True) AS the p-: for i in the Range (Days): # get key corresponding to the value of someone binary representation of the value of p = p.getbit (self.GetDaliySignKey (datetime.datetime.now () - the datetime.timedelta (Days = I)), user_id) # closure line RES = p.execute () # RES is a [1,0,0,0 ...] list Print (RES) # transformed into binary string return '' .join ([str (the X-) for the X- in RES]) # do daily attendance by user user_id DEF UserDaliySignByUserId (Self, user_id): IF self.GetDailySignByUserId (user_id): Print ( " Today you have to sign " ) return key = self.GetDaliySignKey () # key is to check the date, the user_id is set to 1, it indicates the sign corresponding to the key positions on self.redis_store.setbit (key, user_id, 1 ) # set the expiration time of the key self. redis_store.expire (Key, 60 * 60 * 24 * 30 ) return True # Through daily attendance information obtaining user user_id DEF GetDailySignByUserId (Self, user_id): return self.redis_store.getbit (self.GetDaliySignKey (), user_id) IF the __name__ == ' __main__ ' : the session = { " User " : 1003 } Sign = RedisSignIn () sign.UserDailySign (the session) sign.GetUserDailySignRecordMonthly (the session)