Redis serialized storage Java set List and other custom types

Source: Internet
Author: User

Redis serialized storage Java set List and other custom types
 
As the number of application scenarios increases in the project, the Java Collection List is stored. At this time, common code may report errors, such as "unable to serialize" or "serialization failed ~
After several hours of Practical Exploration, I have referred to the Code working in seconds and the recent code. There are two feasible methods.
SpringDataRedis is used in the project, but the idea of Jedis code is the same.

Redis configuration in the project

 
  
   
   
    
   
   
    
   
  
 

Directly store java. util. list will prompt "unable to serialize", "JdkSerializationRedisSerializer serialization failed" similar error, simply put java. util. list elements cannot implement the Serialiable interface. I also considered whether it is related to the serialVersionUID of the List element. The default value is 1, which is generated by the system. private static final long serialVersionUID =-2162380932844568332L; Method 1: Convert the List to JSON, store it in Redis, and convert the JSON into List when getting it. This method is also very good, but I didn't think of it at the time. Serialized Storage
List list = new ArrayList();  String json=JSONObject.toJSONString(list);  logger.info(save json=+json);  defaultCache.add(key, json, CATCHE_TIME);

Deserialization
  Object jsonInRedis = defaultCache.getValue(key);List
 
   list = null;Object listInRedis = null;if(jsonInRedis != null){logger.info(get json=+jsonInRedis);listInRedis= JSONObject.parseArray(jsonInRedis.toString(), MatchContent.class);}if (listInRedis instanceof List) {list = (List) listInRedis;logger.debug(Find fund4Project in redis~ size= + list.size());}
 

It is worth noting that JSONObject. parseArray can convert a json string to a Java List. This method is rarely used before and has never been familiar with it. The 2nd parameters are the class of the List element. One Demo written by myself.
public static void main(String[] args) {List list = new ArrayList();list.add(new User());String json=JSONObject.toJSONString(list);System.out.println(json);List newList=JSONObject.parseArray(json, User.class);System.out.println(newList.size());}

Method 2: Convert the List to a binary array byte [], store it in Redis, and convert the byte [] to a List. Serialize list-> byte []
import hprose.io.HproseFormatter;java.io.ByteArrayOutputStream baos=HproseFormatter.serialize(list);byte[] bytes=baos.toByteArray();

Binary deserialization byte []-> list listInRedis = HproseFormatter. unserialize (byte []) bytesInRedis); The project uses the source code, which is copied from the second-hand code. The above two methods are serialized and stored in JSON format, which makes it easier. However, it is said that the HproseFormatter library is awesome, according to the official website.
Hprose (High Performance Remote Object Service Engine) is an advanced lightweight, cross-language, cross-platform, non-invasive, High-Performance dynamic Remote Object calling Engine library. It is not only easy to use, but also powerful. You don't need to study it. You just need to take a few eyes and you can use it to easily build a distributed application system.
I found the hprose information on the Internet. If you don't have an accident, you can use the following. Hprose/hprose-java https://github.com/hprose/hprose-java/tree/master/src Map and other types of storage, similar to List ~ Solve the Problem of serialization of Redis. It seems that the method is very simple. In the classic sentence, "No problem, it will be difficult ". After the problem is solved, the problem becomes more difficult. When the problem persists, you will be killed. Come on, man ~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.