def mark_visited(user_ip, page_name): '''记录访问过的用户操作''' pipe = redis_data.pipeline() visited_users = 'visited_users:%s' % user_ip visited_pages = 'visited_pages:%s' % page_name '''访问过的用户''' pipe.sadd('visited:users', user_ip) '''实时查询''' page_count = redis_data.zscore(visited_users, visited_pages) '''记录访问某个页面的次数''' if page_count is None: page_count = 1 pipe.zadd(visited_users, page_count, visited_pages) redis_data.zincrby(visited_users, visited_pages, 1) '''记录访问的时间''' now_time = int(time()) query_last_time = redis_data.zscore(visited_users, 'last_time') query_now_time = redis_data.zscore(visited_users, 'time') if query_last_time is None: query_last_time = now_time pipe.zadd(visited_users, query_last_time, 'last_time') if query_now_time is None: query_now_time = now_time '''十分钟''' if now_time - query_last_time >= 600: pipe.zadd(visited_users, query_now_time, 'last_time') pipe.zadd(visited_users, now_time, 'time') else: pipe.zadd(visited_users, now_time, 'time') pipe.expire('visited:users', 604800) pipe.expire(visited_users, 259200) pipe.execute()
def mark_online(user_ip): '''记录在线用户''' pipe = redis_data.pipeline() config = current_app.config now_time = int(time()) + 28800 expires = config['ONLINE_LAST_MINUTES'] * 60 '''分钟''' online_users = 'online_users:%d' % (now_time // 60) active_users = 'active_users:%s' % user_ip '''注册用户''' if g.user is not None and g.user.is_authenticated: online_sign_users = 'online_sign_users:%d' % (now_time // 60) active_sign_users = 'active_sign_users:%s' % user_ip pipe.sadd(online_sign_users, user_ip) pipe.set(active_sign_users, now_time) pipe.expire(online_sign_users, expires) pipe.expire(active_sign_users, expires) pipe.sadd(online_users, user_ip) pipe.set(active_users, now_time) pipe.expire(online_users, expires) pipe.expire(active_users, expires) high = redis_data.hget('online_users', 'high:counts') if not high: pipe.hset('online_users', 'high:counts', 1) high_time = redis_data.hget('online_users', 'high:time') if not high_time: pipe.hset('online_users', 'high:time', now_time) pipe.execute()
def set_question(): '''使用redis记录''' pipe = redis_data.pipeline() '''用户发帖数''' user = '******' % str(current_user.id) pipe.hincrby(user, 'topic', 1) pipe.hincrby(user, 'all:topic', 1) pipe.execute()
def set_topics(): '''使用redis记录''' pipe = redis_data.pipeline() '''用户发帖数''' user = '******' % str(current_user.id) pipe.hincrby(user, 'topic', 1) pipe.hincrby(user, 'all:topic', 1) pipe.execute()
def mark_online(user_ip): '''记录在线用户''' config = current_app.config now = int(time()) expires = now + (config['ONLINE_LAST_MINUTES'] * 60) + 10 online_users_key = 'online_users:%d' % (now // 60) user_key = 'user_activity:%s' % user_ip p = redis_data.pipeline() p.sadd(online_users_key, user_ip) p.set(user_key, now) p.expireat(online_users_key, expires) p.expireat(user_key, expires) p.execute()
def daily(): '''签到''' user = '******' + ':' + 'daily' + ':' + str(current_user.id) if redis_data.exists(user): flash('你已签到,不能重复签到') return redirect(url_for('user.index', user_url=current_user.name)) else: from datetime import date, timedelta from time import mktime today = date.today() + timedelta(days=1) a = mktime(today.timetuple()) b = a + 28800 pipe = redis_data.pipeline() pipe.set(user, '1') pipe.expireat(user, int(b)) pipe.execute() current_user.infor.score += 10 db.session.commit() flash('签到成功') return redirect(url_for('user.index', user_url=current_user.name))
def set_replies(qid): pipe = redis_data.pipeline() pipe.hincrby('topics', 'replies', 1) pipe.hincrby('topic:%s' % str(qid), 'replies', 1) pipe.execute()
def set_replies(qid): pipe = redis_data.pipeline() pipe.hincrby('question:%s' % str(qid), 'replies', 1) pipe.execute()