Redis实战之征服 Redis + Jedis + Spring (二)

时间:2021-05-08 20:29:53

不得不说,用哈希操作来存对象,有点自讨苦吃!Redis实战之征服 Redis + Jedis + Spring (二)

不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?!

或许,是我的理解不对,没有真正的理解哈希表。

 

相关链接:

Redis实战

Redis实战之Redis + Jedis

Redis实战之征服 Redis + Jedis + spring (一)

Redis实战之征服 Redis + Jedis + Spring (二)

Redis实战之征服 Redis + Jedis + Spring (三)

一、预期

接上一篇,扩充User属性:

 

Java代码  Redis实战之征服 Redis + Jedis + Spring (二)
  1. public class User implements Serializable {  
  2.     private static final long serialVersionUID = -1267719235225203410L;  
  3.   
  4.     private String uid;  
  5.   
  6.     private String address;  
  7.   
  8.     private String mobile;  
  9.   
  10.     private String postCode;  
  11. }  
[java] view plain copy
  1. public class User implements Serializable {  
  2.     private static final long serialVersionUID = -1267719235225203410L;  
  3.   
  4.     private String uid;  
  5.   
  6.     private String address;  
  7.   
  8.     private String mobile;  
  9.   
  10.     private String postCode;  
  11. }  

 

 我期望的是: 

 

 

redis 127.0.0.1:6379> hmget uc.user.info.uid.u123456 address mobile postCode
1) "\xe4\xb8\x8a\xe6\xb5\xb7"
2) "13800138000"
3) "100859"

 

 

几乎就是一个对象了!Redis实战之征服 Redis + Jedis + Spring (二) 

但是,接下来的代码实现,让我彻底崩溃了!Redis实战之征服 Redis + Jedis + Spring (二)

 

二、代码实现

1.保存——HMSET

 

Java代码  
  1. @Override  
  2. public void save(final User user) {  
  3.     redisTemplate.execute(new RedisCallback<Object>() {  
  4.         @Override  
  5.         public Object doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + user.getUid());  
  9.             BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate  
  10.                     .boundHashOps(key);  
  11.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  12.                     .serialize("mobile"), redisTemplate  
  13.                     .getStringSerializer().serialize(user.getMobile()));  
  14.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  15.                     .serialize("address"), redisTemplate  
  16.                     .getStringSerializer().serialize(user.getAddress()));  
  17.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  18.                     .serialize("postCode"), redisTemplate  
  19.                     .getStringSerializer().serialize(user.getPostCode()));  
  20.             connection.hMSet(key, boundHashOperations.entries());  
  21.             return null;  
  22.         }  
  23.     });  
  24. }  
[java] view plain copy
  1. @Override  
  2. public void save(final User user) {  
  3.     redisTemplate.execute(new RedisCallback<Object>() {  
  4.         @Override  
  5.         public Object doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + user.getUid());  
  9.             BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate  
  10.                     .boundHashOps(key);  
  11.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  12.                     .serialize("mobile"), redisTemplate  
  13.                     .getStringSerializer().serialize(user.getMobile()));  
  14.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  15.                     .serialize("address"), redisTemplate  
  16.                     .getStringSerializer().serialize(user.getAddress()));  
  17.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  18.                     .serialize("postCode"), redisTemplate  
  19.                     .getStringSerializer().serialize(user.getPostCode()));  
  20.             connection.hMSet(key, boundHashOperations.entries());  
  21.             return null;  
  22.         }  
  23.     });  
  24. }  

 

 

 

这里用到:

 

Java代码  
  1. BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);  
  2. boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));  
[java] view plain copy
  1. BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);  
  2. boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));  

 

 看着就有点肿。。。Map封装完以后,用HMSET命令:

 

Java代码  
  1. connection.hMSet(key, boundHashOperations.entries());  
[java] view plain copy
  1. connection.hMSet(key, boundHashOperations.entries());  

 

 这时候就完成了哈希表的保存操作,可以在控制台看到相应的数据了。

 

redis 127.0.0.1:6379> hmget uc.user.info.uid.u123456 address mobile postCode
1) "\xe4\xb8\x8a\xe6\xb5\xb7"
2) "13800138000"
3) "100859"

 

2.获取——HMGET

这一刻,我彻底崩溃了!取出来的值是个List,还得根据取得顺序,逐个反序列化,得到内容。

 

Java代码  
  1. @Override  
  2. public User read(final String uid) {  
  3.     return redisTemplate.execute(new RedisCallback<User>() {  
  4.         @Override  
  5.         public User doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + uid);  
  9.             if (connection.exists(key)) {  
  10.                 List<byte[]> value = connection.hMGet(  
  11.                         key,  
  12.                         redisTemplate.getStringSerializer().serialize(  
  13.                                 "address"),  
  14.                         redisTemplate.getStringSerializer().serialize(  
  15.                                 "mobile"), redisTemplate  
  16.                                 .getStringSerializer()  
  17.                                 .serialize("postCode"));  
  18.                 User user = new User();  
  19.                 String address = redisTemplate.getStringSerializer()  
  20.                         .deserialize(value.get(0));  
  21.                 user.setAddress(address);  
  22.                 String mobile = redisTemplate.getStringSerializer()  
  23.                         .deserialize(value.get(1));  
  24.                 user.setMobile(mobile);  
  25.                 String postCode = redisTemplate.getStringSerializer()  
  26.                         .deserialize(value.get(2));  
  27.                 user.setPostCode(postCode);  
  28.                 user.setUid(uid);  
  29.   
  30.                 return user;  
  31.             }  
  32.             return null;  
  33.         }  
  34.     });  
  35. }  
[java] view plain copy
  1. @Override  
  2. public User read(final String uid) {  
  3.     return redisTemplate.execute(new RedisCallback<User>() {  
  4.         @Override  
  5.         public User doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + uid);  
  9.             if (connection.exists(key)) {  
  10.                 List<byte[]> value = connection.hMGet(  
  11.                         key,  
  12.                         redisTemplate.getStringSerializer().serialize(  
  13.                                 "address"),  
  14.                         redisTemplate.getStringSerializer().serialize(  
  15.                                 "mobile"), redisTemplate  
  16.                                 .getStringSerializer()  
  17.                                 .serialize("postCode"));  
  18.                 User user = new User();  
  19.                 String address = redisTemplate.getStringSerializer()  
  20.                         .deserialize(value.get(0));  
  21.                 user.setAddress(address);  
  22.                 String mobile = redisTemplate.getStringSerializer()  
  23.                         .deserialize(value.get(1));  
  24.                 user.setMobile(mobile);  
  25.                 String postCode = redisTemplate.getStringSerializer()  
  26.                         .deserialize(value.get(2));  
  27.                 user.setPostCode(postCode);  
  28.                 user.setUid(uid);  
  29.   
  30.                 return user;  
  31.             }  
  32.             return null;  
  33.         }  
  34.     });  
  35. }  

 

 这个实现,跟Redis的命令几乎一模一样,指定Key,指定field,获取其值。

 

Java代码  
  1. List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),  
  2. redisTemplate.getStringSerializer().serialize("mobile"),   
  3. redisTemplate.getStringSerializer().serialize("postCode"));  
[java] view plain copy
  1. List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),  
  2. redisTemplate.getStringSerializer().serialize("mobile"),   
  3. redisTemplate.getStringSerializer().serialize("postCode"));  

 

 我绝对相信,要么是我用的过于肤浅,低估了Spring的封装能力。或者,我该直接Json!Redis实战之征服 Redis + Jedis + Spring (二)等等,这不是MongoDB干的事情吗?!

PS:这两篇博客里操作的数据类型,只能是String类型,还没搞定除此以外任何类型。吾将上下而求索~~~

上述操作也许你吐了,接下来的代码,就再吐一次吧!

封装对象的时候,一定要记得次序。。。。这绝对不是一个优质代码的实现风格!

 

Java代码  
  1. User user = new User();  
  2. String address = redisTemplate.getStringSerializer().deserialize(value.get(0));  
  3. user.setAddress(address);  
  4. String mobile = redisTemplate.getStringSerializer().deserialize(value.get(1));  
  5. user.setMobile(mobile);  
  6. String postCode = redisTemplate.getStringSerializer().deserialize(value.get(2));  
  7. user.setPostCode(postCode);  
[java] view plain copy
  1. User user = new User();  
  2. String address = redisTemplate.getStringSerializer().deserialize(value.get(0));  
  3. user.setAddress(address);  
  4. String mobile = redisTemplate.getStringSerializer().deserialize(value.get(1));  
  5. user.setMobile(mobile);  
  6. String postCode = redisTemplate.getStringSerializer().deserialize(value.get(2));  
  7. user.setPostCode(postCode);  

 

 好吧!苦逼的事情,就此结束。目标Json支持!