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

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

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

相关链接:

Redis实战

Redis实战之Redis + Jedis

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

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

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

一、预期

接上一篇,扩充User属性:

  1. public class User implements Serializable {
  2. private static final long serialVersionUID = -1267719235225203410L;
  3. private String uid;
  4. private String address;
  5. private String mobile;
  6. private String postCode;
  7. }
public class User implements Serializable {
private static final long serialVersionUID = -1267719235225203410L; private String uid; private String address; private String mobile; private String postCode;
}

我期望的是:

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 (二)-LMLPHP

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

二、代码实现

1.保存——HMSET

  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. }
	@Override
public void save(final User user) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(
"uc.user.info.uid." + user.getUid());
BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate
.boundHashOps(key);
boundHashOperations.put(redisTemplate.getStringSerializer()
.serialize("mobile"), redisTemplate
.getStringSerializer().serialize(user.getMobile()));
boundHashOperations.put(redisTemplate.getStringSerializer()
.serialize("address"), redisTemplate
.getStringSerializer().serialize(user.getAddress()));
boundHashOperations.put(redisTemplate.getStringSerializer()
.serialize("postCode"), redisTemplate
.getStringSerializer().serialize(user.getPostCode()));
connection.hMSet(key, boundHashOperations.entries());
return null;
}
});
}

这里用到:

  1. BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);
  2. boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));
BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);
boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));

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

  1. connection.hMSet(key, boundHashOperations.entries());
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,还得根据取得顺序,逐个反序列化,得到内容。

  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. return user;
  30. }
  31. return null;
  32. }
  33. });
  34. }
	@Override
public User read(final String uid) {
return redisTemplate.execute(new RedisCallback<User>() {
@Override
public User doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(
"uc.user.info.uid." + uid);
if (connection.exists(key)) {
List<byte[]> value = connection.hMGet(
key,
redisTemplate.getStringSerializer().serialize(
"address"),
redisTemplate.getStringSerializer().serialize(
"mobile"), redisTemplate
.getStringSerializer()
.serialize("postCode"));
User user = new User();
String address = redisTemplate.getStringSerializer()
.deserialize(value.get(0));
user.setAddress(address);
String mobile = redisTemplate.getStringSerializer()
.deserialize(value.get(1));
user.setMobile(mobile);
String postCode = redisTemplate.getStringSerializer()
.deserialize(value.get(2));
user.setPostCode(postCode);
user.setUid(uid); return user;
}
return null;
}
});
}

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

  1. List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),
  2. redisTemplate.getStringSerializer().serialize("mobile"),
  3. redisTemplate.getStringSerializer().serialize("postCode"));
List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),
redisTemplate.getStringSerializer().serialize("mobile"),
redisTemplate.getStringSerializer().serialize("postCode"));

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

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

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

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

  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);
User user = new User();
String address = redisTemplate.getStringSerializer().deserialize(value.get(0));
user.setAddress(address);
String mobile = redisTemplate.getStringSerializer().deserialize(value.get(1));
user.setMobile(mobile);
String postCode = redisTemplate.getStringSerializer().deserialize(value.get(2));
user.setPostCode(postCode);

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

相关链接:

Redis实战

Redis实战之Redis + Jedis

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

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

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

04-21 06:42