Redis存儲對象

      redis已經應用至關普遍了,但redis自己並無直接存儲對象的方法,咱們能夠經過轉換對象的方式來存儲對象。java

大體總結了如下幾種方案來存儲對象:redis

方案一:序列化對象爲二進制code

使用redis的接口:對象

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

至於序列化方式,咱們有不少種選擇,好比:Java serialize,Protobuf,或者本身手動序列化都行接口

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

方案二:序列化對象爲字符串ip

使用redis的接口:字符串

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

序列化爲字符串,咱們也有不少選擇:Json(Jackson,FastJson),Xml等方式get

方案三:轉換對象爲Map博客

使用redis的接口:it

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

將對象轉成一個map:

/**
	 * 將指定的對象數據封裝成map
	 * 
	 * @param bean
	 *            對象數據
	 * @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;
	}

將map轉成java對象:

/**
	 * 將map轉成指定的對象
	 * 
	 * @param beanMap
	 *            map數據
	 * @param clazz
	 *            指定的類對象
	 * @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;
	}

	/**
	 * 將String類型數值轉換成指定的類型
	 * 
	 * @param value
	 *            數值
	 * @param 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;
	}

這種方式有一個優點:在更新對象的時候不須要更新整個對象,只須要更新須要的字段。

總結:大體總結了三種方案,每種方式都有本身的優點和劣勢,根據不一樣的需求進行選擇。

我的博客:http://codingo.xyz

相關文章
相關標籤/搜索