def random_private_library_offset(library_type, user_id, refresh=False): if not user_id: return 1 cache_key = private_library_cache_key + str(library_type) + ":" + str( user_id) # 从集合中随机取一个值 rand_id = redis.srandmember(cache_key) # Redis随机取值失败或者需要刷新 if not rand_id or refresh: # 读取数据库数据 if library_type == 11: model_list = ImageModel.query.filter_by(user_id=user_id, type=11, status=1).all() cache_id_list = array_column(model_list, "image_id") else: model_list = VideoModel.query.filter_by(user_id=user_id, type=31, status=1).all() cache_id_list = array_column(model_list, "video_id") # 更新Redis并返回随机值 redis.delete(cache_key) if cache_id_list: redis.sadd(cache_key, *cache_id_list) return random.choice(cache_id_list) else: return 1 else: return rand_id
def azz_net_job(username): latest_pic_url = get_first_page_urls(username)[0] # 判断最新一张是否在历史中,如果不在说明为新的作品 if not redis.sismember(key(username), latest_pic_url): # 发现新的作品,推送: logger.info('检测到 {} 有新的作品发布'.format(username)) for user_id in models.get_all_user_id(): content = '{} 发布了新的作品,图片链接为: \n{}'.format(username, latest_pic_url) bot.send_message(chat_id=user_id, text=content) redis.sadd(key(username), latest_pic_url)
def addIPAddress(self, ip_address): """ Add an IP address to the IP address list """ self._data['ip_addresses'].add(ip_address) keyExists = True if redis.exists(f'session::{self.id}::ip_addresses') == 0: keyExists = False redis.sadd(f'session::{self.id}::ip_addresses', ip_address) if not keyExists: redis.expire(f'session::{self.id}::ip_addresses', self.timeToEnd().seconds + (500))
def save(self): """ Save the current alert to database. """ email_sha = sha1(self.email).hexdigest() saved = bool( redis.sadd("sl:alert:ids", self.sha) and redis.sadd("sl:account:{}:alerts".format(email_sha), self.sha) and redis.hmset("sl:alert:{}".format(self.sha), self.to_dict()) ) if not saved: raise SleekException("Could not save current alert.")
def join(message): room_id = message['room_id'] if 'manager_id' in session: if room_id not in session: session[room_id] = u"管理员" if 'uid' not in session: session['uid'] = str(session['manager_id']) uid = session['uid'] join_room(room_id) redis.sadd(room_id, room_id + ':' + uid) emit('user update', {'flag': 'join', 'uid': uid, 'nick_name': session[room_id]}, room=room_id) emit('system message', {'content': session[room_id] + u'加入了房间' + message['room_id']}, room=room_id)
def add_karma(self, sha): if redis.sadd("karma:{}".format(self.sha), sha): self.karma += 1 redis.zincrby("trending", self.sha, self.score / self.karma) redis.zremrangebyrank("trending", 0, -100) return self.update() return False
def register(self): """ Register and save the current user to database. """ try: sha = sha1(self.email).hexdigest() except TypeError: raise SleekException("Could not register user.", 401) if not redis.sadd("sl:account:ids", sha): raise SleekException("Could not register new user.", 401) self.save(register=True)
def join(message): room_id = message['room_id'] if 'manager_id' in session: if room_id not in session: session[room_id] = u"管理员" if 'uid' not in session: session['uid'] = str(session['manager_id']) uid = session['uid'] join_room(room_id) redis.sadd(room_id, room_id + ':' + uid) emit('user update', { 'flag': 'join', 'uid': uid, 'nick_name': session[room_id] }, room=room_id) emit('system message', {'content': session[room_id] + u'加入了房间' + message['room_id']}, room=room_id)
def cache_user_rsvp_response(response, event_id, previous_event=False): """ 1. Get the response dictionary. 2. JSONIFY (serialize it so that it can be saved in redis cache) 3. Insert the response into the list """ # in case of previous_event we dont need to convert tag name # prvious event means if manually cache the response to run job if not previous_event: tag_ques_response = modify_reponse_of_user(response) response[tag_ques_response[0]] = tag_ques_response[1] # concate reponse for key, value in response.iteritems(): if isinstance(value, list): response[key] = ','.join(value) else: response[key] = value from utils_helper import Util_Helpers key = Util_Helpers.cache_key_creation(["event", event_id, "response"]) response = json.dumps(response) redis.sadd(key, response)
def addEmail(self, email): """ Add email to email list, emails on this list will be sent a message with the final results """ # Don't allow modifications to data after the session has ended if self.isEnded(): raise Exception('This session has ended.') # Force emails to lowercase email = email.lower() # Add email address to this session's email set self._data['emails'].add(email) # Save to redis keyExists = True if redis.exists(f'session::{self.id}::emails') == 0: keyExists = False redis.sadd(f'session::{self.id}::emails', email) ## set ttl on the key if it was just created if not keyExists: redis.expire(f'session::{self.id}::emails', self.timeToEnd().seconds + (500))
def scrap(data): scraping = get_scraping(data['type']) key = data['email'] json_data = json.dumps(data) r.sadd(key, json_data) return scraping.scrap(data)
def add_online(user_id): if not user_id: return when = int(time.time()) / PERIOD * PERIOD key = KEY.format(when) redis.sadd(key, user_id)
def save(self): if redis.sadd("messages", self.sha) and redis.set("message:{}".format(self.sha), self.to_dict()): redis.zincrby("new", self.sha, dt2ts(self.date)) redis.zremrangebyrank("new", 0, -100) return True return False
def init_azz_net(username): for url in get_first_page_urls(username): redis.sadd(key(username), url)
def push_list(key, value): redis.sadd(key, value)
def save(self): return (redis.sadd("tokens", self.sha) and redis.set("token:{}".format(self.sha), self.to_dict()))