(二)Redis投票应用

1. 用hash存储帖子的基本信息:

[img]http://dl2.iteye.com/upload/attachment/0088/4362/99cf9b7b-888f-3671-8718-9a46830ff4dd.png[/img]

2. 使用sorted set来实现按照时间排序和按照投票排序
时间
[img]http://dl2.iteye.com/upload/attachment/0088/4364/783b9f31-2b23-3714-8d01-14187287b34a.png[/img]
投票数
[img]http://dl2.iteye.com/upload/attachment/0088/4367/3ca49bae-3c82-36a5-badb-60ecca8e53d8.png[/img]

3. 由于一个用户不能重复投票,所以需要用一个set来存放,这个article有哪些人投票了
[img]http://dl2.iteye.com/upload/attachment/0088/4373/5d1b2251-e55b-317f-8b71-5339b7d7f21f.png[/img]
如果用新用户投票了,那么这个帖子积分就会增加,并且将此用户的id放入到set中。并且我们限制一个星期之后帖子就不能再投票了。

redis命令如下:
ZADD article_id 1 time
ZSCORE article_id "one" < 1 week
SADD article_id user_id
ZINCRBY article_id 1 score
HINCRBY article_id votes 1


4. 为帖子创建兴趣组:


[img]http://dl2.iteye.com/upload/attachment/0088/4369/46f70bd1-0cbe-3681-b19c-881da73ce5df.png[/img]

[size=large][color=red]ZINTERSTORE的使用方法[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Redis 投票功能的前后端代码示例,前端使用的是 Vue.js 框架,后端使用的是 Python 的 Flask 框架,Redis 仍然使用的是 Python 客户端 redis-py。 前端代码: ``` html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vote App</title> </head> <body> <div id="app"> <h2>Vote for your favorite item:</h2> <ul> <li v-for="(item, count) in items" :key="item"> {{ item }}: {{ count }} <button @click="vote(item)">Vote</button> </li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script> new Vue({ el: '#app', data: { items: {} }, mounted() { // 获取投票项目信息 this.fetchItems() }, methods: { fetchItems() { fetch('/items') .then(response => response.json()) .then(data => { this.items = data }) }, vote(item) { // 发送投票请求 fetch('/vote', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ item }) }) .then(response => response.json()) .then(data => { alert(data.message) // 更新投票项目信息 this.fetchItems() }) } } }) </script> </body> </html> ``` 后端代码: ``` python import json from flask import Flask, request import redis # 创建 Flask 应用Redis 连接 app = Flask(__name__) redis_conn = redis.Redis(host='localhost', port=6379, db=0) # 投票项目信息 items = { 'item1': 0, 'item2': 0, 'item3': 0 } # 将投票项目信息保存到 Redis 的 hash 数据类型中 for item, count in items.items(): redis_conn.hset('vote_items', item, count) # 获取投票项目信息 API @app.route('/items', methods=['GET']) def get_items(): vote_items = redis_conn.hgetall('vote_items') items_dict = {} for item, count in vote_items.items(): items_dict[item.decode()] = int(count) return json.dumps(items_dict) # 用户投票 API @app.route('/vote', methods=['POST']) def vote(): data = request.get_json() item = data.get('item') # 检查用户是否已经投过该项目 if redis_conn.sismember('voted_users:{}'.format(item), request.remote_addr): return json.dumps({'message': 'You have already voted for this item!'}) # 将用户添加到已投票用户集合中 redis_conn.sadd('voted_users:{}'.format(item), request.remote_addr) # 将该项目的投票数量加1 redis_conn.hincrby('vote_items', item, 1) return json.dumps({'message': 'You have successfully voted for item {}!'.format(item)}) if __name__ == '__main__': app.run() ``` 以上代码实现了一个简单的 Redis 投票应用,前端页面展示了投票项目列表和投票按钮,用户点击投票按钮后,将投票项目名称作为参数发送到后端 `/vote` API 进行投票。后端检查用户是否已经投过该项目,如果已经投过,则返回错误提示,否则将该用户添加到已投票用户集合中,同时将该项目的投票数量加1,最后返回投票成功的提示。前端在接收到投票成功的提示后,更新投票项目信息,展示最新的投票结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值