def store_account_data_with_verification_code(self, verification_code, account): with redis_client.pipeline() as pipe: pipe.mset({verification_code: pickle.dumps(account)}) pipe.expire(verification_code, current_app.config["EMAIL_VERIFY_DEADLINE"]) pipe.execute()
def put_links_in_set(_time, links): try: from app import redis_client with redis_client.pipeline() as pipe: for key, link in enumerate(links): pipe.sadd(f'time:{_time}:links', link) pipe.execute() status = "ok" except Exception as error: status = repr(error) return status
def mark_online(user): user_id = str(user.id).encode('utf-8') now = int(time.time()) expires = now + (5 * 60) + 10 all_users_key = "online-users/%d" % (now // 60) user_key = "user-activity/%s" % user_id p = redis_client.pipeline() p.sadd(all_users_key, user_id) p.set(user_key, now) p.expireat(all_users_key, expires) p.expireat(user_key, expires) p.execute()
def incr_report(ad_id, field, amount): # Increase ad report for given ad id and field. ad_key = KEY_SPACE_AD + str(ad_id) today = datetime.utcnow().date().isoformat() report_key = KEY_SPACE_REPORT + '%s:%s' % (ad_id, today) pipe = redis_client.pipeline(transaction=True) if type(amount) == float: pipe.hincrbyfloat(ad_key, field, amount) pipe.hincrbyfloat(report_key, field, amount) elif type(amount) == int: redis_client.hincrby(ad_key, field, amount) redis_client.hincrby(report_key, field, amount) else: raise Exception('Not support type %s' % type(amount)) pipe.execute() return
def _create_or_update_ad(ad_id, dest_url, image_src, width, height, cpm, daily_budget): ad = {} if dest_url is not None: ad['dest_url'] = dest_url if image_src is not None: ad['image_src'] = image_src if width is not None: ad['width'] = int(width) if height is not None: ad['height'] = int(height) if cpm is not None: ad['cpm'] = float(cpm) if daily_budget is not None: ad['daily_budget'] = float(daily_budget) if ad_id is None: ad_id = redis_client.incr('ad_id_seq') ad['id'] = int(ad_id) pipe = redis_client.pipeline(transaction=True) pipe.hmset(KEY_SPACE_AD + str(ad_id), ad) pipe.sadd('ad_ids', ad_id) pipe.execute() return ad_id
def index(): # path = app.config['PATH'] #get config print('app content',app.wsgi_app) redis_client.set('name','blog',60) #key value expire redis_ = redis_client.get('name').decode('utf-8') #redis # print(redis_) # session['user'] = "******" # user = {'nickname': session.get('user')} #session # # if not User.query.filter(User.name=='mini').first(): # use = User( # name = 'mini', # age = 20, # sex = 1, # email = '*****@*****.**' # ) # db.session.add(use) # db.session.commit() #flask_sqlalchemy # query = db.session.query(User.id,User.name).filter(User.name=='mini') #sqlalchemy # print(query[0]) context = { 'username': u'站长', 'age': u'18', 'gender': u'男', 'redis_':redis_, 'websites': { 'baidu': 'www.baidu.com', 'google': 'www.google.com' } # 'avatar': '' } # 列表字典混合 books = [ { 'name': u'西游记', 'author': u'吴承恩', 'price': 88 }, { 'name': u'三国演义', 'author': u'罗贯中', 'price': 98 } ] p = redis_client.pipeline() keys = redis_client.keys(pattern="top*") # n,top_data = redis_client.scan(0, match='top_*', count=5) # data = [] m, data = 0, [] while True: n, top_data = redis_client.scan(m, match='top_*', count=5) print(n, top_data) if top_data: data += top_data if n != 0: m = n else: break # dat = p.execute() print(keys, data) page = request.args.get('page', None) if page: page = eval(page) print(page, type(page)) context.update({"page":page}) return render_template('myblog/index.html',**context, user=context, books=books)
def store_register_data_temporally(verification_code, data): with redis_client.pipeline() as pipe: pipe.mset({verification_code: json.dumps(data)}) pipe.expire(verification_code, current_app.config["EMAIL_VERIFY_DEADLINE"]) pipe.execute()
def delete_ad(ad_id): pipe = redis_client.pipeline(transaction=True) pipe.delete(KEY_SPACE_AD + str(ad_id)) pipe.srem('ad_ids', ad_id) pipe.execute() return