1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | | package com.lyh.redis.test;
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import org.junit.Test; import com.alibaba.fastjson.JSON; import redis.clients.jedis.Jedis;
/** * 一个缓存的小案例 * 即把经常用到的数据放入缓存中(Redis数据库中) * @author MECHREVO */ public class SingleRedisTest3 { private Jedis j = init(); ; private final String APPLES = "APPLES"; private final String SWEET = "SWEET"; //含糖量为1的代表甜的 private final String RED = "RED"; //颜色为A和B 代表红 public Jedis init(){ //一个Jedis对象对应这个一个Redis的实例 Jedis jedis = new Jedis("192.168.29.160",6379); return jedis; } public void destroy(Jedis j){ //在关闭时断开连接,测试发现在操作之前调用这个方法后Jedis实例依然可以使用 j.disconnect(); } @Test public void dataInput(){ Map<String,String> apples = new HashMap<String,String>(); //id,name,price,color,sweet String id1 = UUID.randomUUID().toString(); Apple a1 = new Apple(id1,"X",100,"A","1"); apples.put(id1, JSON.toJSONString(a1)); j.sadd(SWEET,id1); j.sadd(RED,id1); String id2 = UUID.randomUUID().toString(); Apple a2 = new Apple(id2,"Y",200,"B","1"); apples.put(id2, JSON.toJSONString(a2)); j.sadd(SWEET,id2); j.sadd(RED,id2); String id3 = UUID.randomUUID().toString(); Apple a3 = new Apple(id3,"Z",200,"C","1"); apples.put(id3, JSON.toJSONString(a3)); j.sadd(SWEET,id3); String id4 = UUID.randomUUID().toString(); Apple a4 = new Apple(id4,"Y",200,"C","4"); apples.put(id4, JSON.toJSONString(a4)); String id5 = UUID.randomUUID().toString(); Apple a5 = new Apple(id5,"K",100,"A","4"); apples.put(id5,JSON.toJSONString(a5)); j.sadd(RED,id5); j.hmset(APPLES, apples); } @Test public void getApples(){ //找出既是红色有很甜的苹果 select * from apples where (color='A' or color ='B' ) and sweet = '1'
Set<String> redAndSweet = j.sinter(RED,SWEET); String[] array = redAndSweet.toArray(new String[1]); List<String> rAndS = j.hmget(APPLES, array); for(String v : rAndS){ System.out.println(v); } } }
class Apple{ private String id; //名字 private String name; //价钱 private int price; //颜色等级 A B C D private String color; //含糖量 1 2 3 4 private String sweet; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getSweet() { return sweet; } public void setSweet(String sweet) { this.sweet = sweet; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Apple(String id, String name, int price, String color, String sweet) { super(); this.id = id; this.name = name; this.price = price; this.color = color; this.sweet = sweet; } public Apple() { super(); } } |