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 get(self, blogId): '''记录用户浏览次数''' redis_data.zincrby('visited:article', 'article:%s' % str(blogId), 1) blog = Blog.get(blogId) tags = Blog.tags data = {'blog': blog, 'tags': tags} return render_template('blog/blog.html', **data)
def view(id): '''记录用户浏览次数''' redis_data.zincrby('visited:article', 'article:%s' % str(id), 1) comment_form = CommentForm() article = Articles.load_by_id(id) tags = article.tags return render_template('blog/blog_page.html', article=article, tags=tags, comment_form=comment_form)
def view(id): '''记录用户浏览次数''' redis_data.zincrby('visited:article', 'article:%s' % str(id), 1) comment_form = CommentForm() article = Articles.load_by_id(id) all_tags = Tags.query.distinct(Tags.name).all() tags = article.tags return render_template('blog/blog_page.html', article=article, all_tags=all_tags, tags=tags, comment_form=comment_form)
def view(id): '''记录用户浏览次数''' redis_data.zincrby('visited:article', 'article:%s' % str(id), 1) comment_form = CommentForm() reply_form = ReplyForm() article = Articles.load_by_id(id) all_tags = Tags.query.distinct(Tags.name).all() tags = article.tags return render_template('blog/blog_page.html', article=article, all_tags=all_tags, tags=tags, comment_form=comment_form, reply_form=reply_form)