|
|
|
@ -0,0 +1,86 @@
|
|
|
|
|
package com.dsideal.base.UnitTest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import com.dsideal.base.Util.RedisKit;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
|
|
|
|
|
public class TestPika {
|
|
|
|
|
public static String listKey = "answer";
|
|
|
|
|
|
|
|
|
|
public static void WritePika() {
|
|
|
|
|
//模拟多条数据写入到NOSQL数据库,使用List保存索引信息,使用Hash保存数据
|
|
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
|
String student_id = "stu_" + i;
|
|
|
|
|
//写入索引信息
|
|
|
|
|
RedisKit.lpush(listKey, student_id);
|
|
|
|
|
|
|
|
|
|
//二十道题
|
|
|
|
|
for (int j = 1; j <= 20; j++) {
|
|
|
|
|
// 生成一个1到4之间的随机整数
|
|
|
|
|
int randomValue = RandomUtil.randomInt(1, 5); // 注意:上界是5,因为生成的随机数是[1, 4]
|
|
|
|
|
String ret = "";
|
|
|
|
|
// 根据随机值输出ABCD
|
|
|
|
|
switch (randomValue) {
|
|
|
|
|
case 1:
|
|
|
|
|
ret = "A";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
ret = "B";
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
ret = "C";
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
ret = "D";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//写入数据
|
|
|
|
|
RedisKit.hSet(student_id, String.valueOf(j), ret);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ReadPika() {
|
|
|
|
|
long listLength = RedisKit.llen(listKey);
|
|
|
|
|
// 遍历并打印列表中的所有元素
|
|
|
|
|
for (int i = 0; i < listLength; i++) {
|
|
|
|
|
String element = RedisKit.lindex(listKey, i); // 获取指定索引的元素
|
|
|
|
|
System.out.println("Element at index " + i + ": " + element);
|
|
|
|
|
//再根据保存人员的id,从Hash中获取数据
|
|
|
|
|
String hashKey = element;
|
|
|
|
|
System.out.println("Hash Key: " + hashKey);
|
|
|
|
|
// 获取Hash中的所有键值对
|
|
|
|
|
for (String key : RedisKit.hGetAll(hashKey).keySet()) {
|
|
|
|
|
String value = RedisKit.hGet(hashKey, key);
|
|
|
|
|
System.out.println("Key: " + key + ", Value: " + value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void clearData() {
|
|
|
|
|
long listLength = RedisKit.llen(listKey);
|
|
|
|
|
|
|
|
|
|
// 遍历并打印列表中的所有元素
|
|
|
|
|
for (int i = 0; i < listLength; i++) {
|
|
|
|
|
String element = RedisKit.lindex(listKey, i);
|
|
|
|
|
RedisKit.del(element);
|
|
|
|
|
}
|
|
|
|
|
RedisKit.del(listKey);
|
|
|
|
|
System.out.println("清除数据成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
PropKit.use("application_dev.properties");
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("Redis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
ReadPika();
|
|
|
|
|
|
|
|
|
|
clearData();
|
|
|
|
|
|
|
|
|
|
ReadPika();
|
|
|
|
|
|
|
|
|
|
redis.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|