接上一篇,擴充User屬性:redis
Java代碼 app
public class User implements Serializable { ide
private static final long serialVersionUID = -1267719235225203410L; post
private String uid; ui
private String address; spa
private String mobile; orm
private String postCode; 對象
} get
我指望的是: 博客
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"
幾乎就是一個對象了!
可是,接下來的代碼實現,讓我完全崩潰了!
Java代碼
@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;
}
});
}
這裏用到:
Java代碼
BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);
boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));
看着就有點腫。。。Map封裝完之後,用HMSET命令:
Java代碼
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"
這一刻,我完全崩潰了!取出來的值是個List,還得根據取得順序,逐個反序列化,獲得內容。
Java代碼
@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,獲取其值。
Java代碼
List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),
redisTemplate.getStringSerializer().serialize("mobile"),
redisTemplate.getStringSerializer().serialize("postCode"));
我絕對相信,要麼是我用的過於膚淺,低估了Spring的封裝能力。或者,我該直接Json!等等,這不是MongoDB乾的事情嗎?!
PS:這兩篇博客裏操做的數據類型,只能是String類型,還沒搞定除此之外任何類型。吾將上下而求索~~~
上述操做也許你吐了,接下來的代碼,就再吐一次吧!
封裝對象的時候,必定要記得次序。。。。這絕對不是一個優質代碼的實現風格!
Java代碼
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支持!
若是想深刻體驗LINUX系統的新手,也能夠先下載一個方德Linux軟件中心試用一下。
免費下載地址:http://www.nfs-cloud.cn:81/appCenter/open/softcente