Jedis就是redis支持java的第三方类库,我们可以使用Jedis类库操作redis数据库。
注意:Jedis2.7以上的版本才支持集群操作。
1.1 新建Maven quickStart项目
引入Jedis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
1.2 链接到redis服务
Jedis jedis = new Jedis("10.0.31.144",6379); //默认端口6379 可以省略
jedis.auth("redis"); //无密码此步可省略
1.3 Redis Java String(字符串)
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
jedis.set("a", "test1"); // 存数据
System.out.println(jedis.get("a")); // 取数据
1.4 Redis Java List(列表)
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
//存储数据到列表中
jedis.lpush("list1", "Java");
jedis.lpush("list1", "Html5");
jedis.lpush("list1", "Python");
// 获取存储的数据并输出
List<String> list = jedis.lrange("list1", 0 ,-1);
for(int i=0; i<list.size(); i++) {
System.out.println("列表项为: "+list.get(i));
}
1.5Redis Java Keys
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
// 获取数据并输出
Set<String> keys = jedis.keys("*");
Iterator<String> it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
2 将User表放入到Redis缓存
t_user表数据量很大,查询很频繁,鲜有更新操作,可以把t_user表放到redis缓存中,实现t_user表的快速查询。
测试过程中我们用到了object转jsonString
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
2.1准备User.java
public class User {
private String id;
private String name;
private int age;
private String sex;
public User() {
super();
}
public User(String id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
2.2 将User的数据放入到Redis
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
User u1 = new User(UUID.randomUUID().toString(),"jack1",21,"m");
User u2 = new User(UUID.randomUUID().toString(),"jack2",22,"m");
User u3 = new User(UUID.randomUUID().toString(),"jack3",23,"m");
User u4 = new User(UUID.randomUUID().toString(),"jack4",24,"m");
User u5 = new User(UUID.randomUUID().toString(),"jack5",25,"m");
Map<String,String> userMap = new HashMap<String,String>();
userMap.put("u1",JSON.toJSONString(u1));
userMap.put("u2",JSON.toJSONString(u2));
userMap.put("u3",JSON.toJSONString(u3));
userMap.put("u4",JSON.toJSONString(u4));
userMap.put("u5",JSON.toJSONString(u5));
jedis.hmset("t_user",userMap);
到Redis数据库服务器上观察:
127.0.0.1:6379> keys * 1) "t_user" 127.0.0.1:6379> hkeys t_user 1) "u1" 2) "u3" 3) "u4" 4) "u5" 5) "u2" 127.0.0.1:6379> hvals t_user 1) "{\"age\":21,\"id\":\"740a1778-de0e-4426-b231-1f97d290b13c\",\"name\":\"jack1\",\"sex\":\"m\"}" 2) "{\"age\":23,\"id\":\"5133d99f-b8a0-4062-8624-89a5dc5c8e89\",\"name\":\"jack3\",\"sex\":\"m\"}" 3) "{\"age\":24,\"id\":\"f6d1a709-5f2c-4564-bec6-b8f63eef6a86\",\"name\":\"jack4\",\"sex\":\"m\"}" 4) "{\"age\":25,\"id\":\"4c8b4122-c9f9-41b2-ba29-c0de2a257370\",\"name\":\"jack5\",\"sex\":\"m\"}" 5) "{\"age\":22,\"id\":\"e48c6616-75c2-4d04-9e90-31b69a4632a4\",\"name\":\"jack2\",\"sex\":\"m\"}" 127.0.0.1:6379>
2.3 使用Redis中的User缓存数据
t_user表数据量大,查询缓慢,如何实现t_user表数据的快速查询;
select * from t_user where age =25;
select * from t_user where sex = 'm';
select * from t_user where sex = 'w';
我们可以使用redis中的set类型预先设定查询结果集。
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
//假设现在有mybatis的三个查询操作
//userMapper.getUserListByAge(25)
//userMapper.getUserListBySex("m")
//userMapper.getUserListBySex("f")
//预设查询结果集
final String user_list_by_age_25 = "user_list_by_age_25";
final String user_list_by_sex_m = "user_list_by_sex_m";
final String user_list_by_sex_f = "user_list_by_sex_f";
//在初始化redis缓存的时候,模拟预设查询结果集
// (在真正的开发中,使用mybatis二级换从数据库查询后再缓存到redis)
//定义userMap存放t_user表的所有数据
Map<String,String> userMap = new HashMap<String,String>();
User u1 = new User(UUID.randomUUID().toString(),"jack",21,"m");
userMap.put("u1",JSON.toJSONString(u1));
//u1 满足性别为m的条件,即u1为userMapper.getUserListBySex("m")的查询结果集
jedis.sadd(user_list_by_sex_m,"u1");
User u2 = new User(UUID.randomUUID().toString(),"rose",25,"f");
userMap.put("u2",JSON.toJSONString(u2));
//u2 满足sex='f' age=25
jedis.sadd(user_list_by_sex_f,"u2");
jedis.sadd(user_list_by_age_25,"u2");
User u3 = new User(UUID.randomUUID().toString(),"jack3",23,"m");
userMap.put("u3",JSON.toJSONString(u3));
//u3 满足sex='m'
jedis.sadd(user_list_by_sex_m,"u3");
jedis.hmset("t_user",userMap);
观察redis服务器:
127.0.0.1:6379> keys * 1) "user_list_by_age_25" 2) "user_list_by_sex_m" 3) "t_user" 4) "user_list_by_sex_f" 127.0.0.1:6379> hkeys t_user 1) "u1" 2) "u2" 3) "u3" 127.0.0.1:6379> hvals t_user 1) "{\"age\":21,\"id\":\"65224362-26b7-4858-9dfd-6c8df8d0dcce\",\"name\":\"jack\",\"sex\":\"m\"}" 2) "{\"age\":25,\"id\":\"cd23782e-90a5-4c80-8c31-d0c874e623f6\",\"name\":\"rose\",\"sex\":\"f\"}" 3) "{\"age\":23,\"id\":\"e5bb2423-c821-4267-b77c-0abef4777113\",\"name\":\"jack3\",\"sex\":\"m\"}" 127.0.0.1:6379> smembers user_list_by_age_25 1) "u2" 127.0.0.1:6379> smembers user_list_by_sex_m 1) "u1" 2) "u3" 127.0.0.1:6379> smembers user_list_by_sex_f 1) "u2" 127.0.0.1:6379>
这样在程序中userMapper.getUserListByAge(25)就可以通过从redis服务器上获得user_list_by_age_25数据进行使用。
Jedis jedis = new Jedis("10.0.31.144",6379);
jedis.auth("redis");
Set<String> userlist = jedis.smembers("user_list_by_sex_m");
for(String u : userlist){
//拿到对对应ser的json字符串
System.out.println(jedis.hget("t_user",u));
//...这里可以再使用json转object方法
}