Redis Storage Object

original
2016/08/04 18:00
Reading amount 1.1W

Redis has been widely used. However, Redis does not directly store objects. We can store objects by transforming objects.

The following schemes are summarized to store objects:

Scheme 1: The serialized object is binary

Use the Redis interface:

 jedis.get(byte[] key) jedis.set(byte[] key, byte[] value)

As for the serialization method, we have many options, such as Java serialize, Protobuf, or manually serialize

 public byte[] serialize(Object obj); public Object unSerialize(byte[] bytes);

Scheme 2: Serializing objects as strings

Use the Redis interface:

 jedis.get(String key); jedis.set(String key, String value);

Serializing as a string, we also have many options: Json (Jackson, FastJson), Xml, etc

Scheme 3: Convert the object to Map

Use the Redis interface:

 jedis.hgetAll(String key); jedis.hmset(String key,  Map<String,String> values);

Convert the object to a map:

 /** *Encapsulates the specified object data into a map *  * @param bean *Object Data * @return */ @SuppressWarnings("all") public static Map<String, String> warp(AbstractRedisBean bean) { Map<String, String> propertyMap = new HashMap<String, String>(); try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()) .getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : ps) { String propertyName = propertyDescriptor.getName(); if (propertyName !=  null && !propertyName.equals(CLASS)) { Method getter = propertyDescriptor.getReadMethod(); if (getter !=  null) { RedisBeanField mannota = getter .getAnnotation(RedisBeanField.class); if (mannota !=  null && mannota.serialize() == false) { continue; } propertyMap.put(propertyName, String.valueOf(getter.invoke(bean, null))); } } } } catch (Exception e) { logger.error(e); } return propertyMap; }

Convert map to java object:

 /** *Convert the map to the specified object *  * @param beanMap *Map data * @param clazz *Specified class object * @return */ public static <T extends AbstractRedisBean> T reverse( Map<String, String> beanMap, Class<T> clazz) { T bean = getRedisBean(clazz); try { PropertyDescriptor[] ps = Introspector.getBeanInfo(clazz) .getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : ps) { String propertyName = propertyDescriptor.getName(); if (propertyName !=  null && !propertyName.equals(CLASS)) { Method setter = propertyDescriptor.getWriteMethod(); String value = beanMap.get(propertyName); String type = propertyDescriptor.getPropertyType() .getName(); if (setter !=  null && value != null && ! value.equalsIgnoreCase("null")) { Object obj = value(value, type); if (obj !=  null) { setter.invoke(bean, obj); } } } } } catch (Exception e) { logger.error(e); e.printStackTrace(); } bean.clearChangeMap(); return bean; } /** *Converts a String type numeric value to the specified type *  * @param value *Value * @param type *Specified type * @return */ private static Object value(String value, String type) { if (type.equals("boolean")) { return Boolean.valueOf(value); } else if (type.equals("byte")) { return Byte.valueOf(value); } else if (type.equals("short")) { return Short.valueOf(value); } else if (type.equals("float")) { return Float.valueOf(value); } else if (type.equals("int")) { return Integer.valueOf(value); } else if (type.equals("java.lang.String")) { return value; } else if (type.equals("long")) { return Long.valueOf(value); } return null; }

This method has one advantage: when updating an object, it does not need to update the entire object, only the required fields need to be updated.

Summary: Three schemes are roughly summarized, each of which has its own advantages and disadvantages and is selected according to different needs.

Personal blog: http://codingo.xyz

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
six Collection
one fabulous
 Back to top
Top