脑洞大开,用redis存关系型数据,简直神经病。
算了,既然研究了,就写一下吧。
redis命令的中文解释:http://redis.readthedocs.org/en/latest/index.html
试题信息:
mysql中试题信息
Id | 难度 | 题型 | 年级 | 题干,答案等其他字段。。。 |
1 | 2 | 3 | 1 | 。。。 |
2 | 3 | 2 | 2 | 。。。 |
3 | 1 | 1 | 1 | 。。。 |
4 | 1 | 2 | 1 | 。。。 |
redis中存储:
Hash类型(用于存储题目的内容):
hmset question_1 hard 2 type 3 level 1 content "{Id:1,hard:2,type:3,level:1,....}"
hmset question_2 hard 3 type 2 level 2 content "{Id:2,hard:3,type:2,level:2,....}"
hmset question_3 hard 1 type 1 level 1 content "{Id:3,hard:1,type:1,level:1,....}"
hmset question_4 hard 1 type 2 level 1 content "{Id:4,hard:1,type:2,level:1,....}"
set类型(用语存储每个条件关联的试题):
sadd hard_1 3 4
sadd hard_2 1
sadd hard_3 2
sadd type_1 3
sadd type_2 2 4
sadd type_3 1
sadd level_1 1 3 4
sadd level_2 2
就此,数据源已经有了。
现在要随机抽取两个 一年级的,题型是1或者2的,难度为1的题目
首先取题型1和2的并集,放到集合temp1中
sunionstore temp1 type_1 type_2
查看temp1内容是 2,3,4
然后取temp1和一年级,难度1的交集,放到temp2中
sinterstore temp2 temp1 level_1 hard_1
查看temp2内容是 3,4
随机的取出temp2中的数据
spop temp2
spop temp2
分别取出了4,3
最后删除temp1和temp2
del temp1
del temp2
用3,4取出具体的题目。
hget question_4 content
hget question_3 content
这一种方法是使用store存到临时的集合,并用pop随机的取出题目
也可以不用,直接取出交集后输出给程序,让程序内存中去完成随机取题的操作。