使用Redis Hash结构来存储对象,更改有限字段;
来源:互联网 发布:淘宝售后可以申请几次 编辑:程序博客网 时间:2024/06/05 16:12
Redis的hash结构特别适合 用来存储对象; 比如我们有吧问题的回答根据id存储到Redis中,
key为 "siye_answerId:"+answer.getId();
然后把对象转换成map直接存储进去, hashkey为对象的field,hashvalue为对象的值;
当有人对回答进行浏览,评论,点赞,踩,收藏等操作的时候直接更新这1个字段,
然后获取对象的时候可以直接转换为对象;
下面为 实例代码: 和转换的工具类(注意只能转换基本数据类型,String,Date等里面有2级对象的不能转换;)
@Testpublic void test2() throws Exception { Date date = new Date(); UserInfo user = new UserInfo(25L, "嬴政", 2000, date); String key = "test_UserInfoId:" + user.getId(); Map<String, String> stringMap = EntityUtils.objectToHash(user);//对象转换map hashOps.putAll(key, stringMap); hashOps.put(key, "name", "姜子牙"); Map<String, Object> map4 = hashOps.entries(key);// 返回map集合 UserInfo user2 = EntityUtils.hashToObject2(map4, UserInfo.class); Long age = hashOps.increment(key, "age", 500L);// 指定字段的值增加,可用来点赞等操作; Map<String, Object> map5 = hashOps.entries(key);// 返回map集合 UserInfo user3 = EntityUtils.hashToObject2(map5, UserInfo.class); System.out.println(user3);}
下面为转换器
public class EntityUtils { static { ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new ByteConverter(null), Byte.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new ShortConverter(null), Short.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new Converter() { public Object convert(Class type, Object value) { if (value == null) { return null; } return new Date(Long.valueOf((String) value)); } }, Date.class); } public static Map<String, String> objectToHash(Object obj) { try { Map<String, String> map = Maps.newHashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { if (!property.getName().equals("class")) { if (property.getReadMethod().invoke(obj) != null) { // 时间类型会错乱所以吧时间手动转换成long; if (property.getReadMethod().invoke(obj) != null) { if ("java.util.Date".equals(property.getPropertyType().getTypeName())) { Date invoke = (Date) property.getReadMethod().invoke(obj); long time = invoke.getTime(); map.put(property.getName(), String.valueOf(time)); } else { map.put(property.getName(), "" + property.getReadMethod().invoke(obj)); } } } } } return map; } catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } } public static <T> T hashToObject2(Map<?, ?> map, Class t) { try { Object o = t.newInstance(); BeanUtils.populate(o, (Map) map); return (T) o; } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } catch (InstantiationException e) { e.printStackTrace(); } return null; }}
阅读全文
0 0
- 使用Redis Hash结构来存储对象,更改有限字段;
- 使用redis存储Java对象
- 使用redis存储Java对象
- redis使用教程原理-第三章:散列类型存储对象结构
- redis 一致性hash ,分布式存储
- Redis存储数据类型四:hash
- 使用Spring-Data-Redis存储对象(redisTemplate)
- 使用Spring-Data-Redis存储对象(redisTemplate)
- 怎样使用SharedPrefences来存储对象
- redis-5大对象之---存储基础结构
- Redis 内存存储结构
- Redis之存储结构
- Redis 内存存储结构
- redis的存储结构
- Redis存储结构
- redis存储结构
- 使用 Redis 来存储 Apache Tomcat 7 的 Session
- 怎么样使用Redis来存储和查询ip数据
- [UVa 11134] 传说中的车(Fabled Rooks)
- hdu 5901 Count primes
- 【百度之星2017】1003.度度熊与邪恶大魔王程序
- 根据ICCID反查手机号码的五种终极方法
- http://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html
- 使用Redis Hash结构来存储对象,更改有限字段;
- 使用shiro+ehcache进行缓存权限数据
- vue.js学习笔记(六)--利用v-model实现父子组件间的双向通信
- Android常规问题总结
- shader新手入门——分享我的shader入门经验
- Codeforces
- Error Back Propagation in BPNeuralNetwork及手写字体识别python版
- 矩阵快速幂
- SVN版本冲突解决详解