Redis storage object, handling of null pointers to new fields in the object class, redis pointer

Source: Internet
Author: User

Redis storage object, handling of null pointers to new fields in the object class, redis pointer


Redis is a key-value storage system. Similar to Memcached, Memcached supports more storage value types, including string, list, set, and zset) and hash (hash type ). These data types support push/pop, add/remove, Intersection Set and difference set, and more abundant operations, and these operations are atomic.

When cache objects are used, they are stored through serialization. When a new field is added to the object class, deserialization is like a newly added field will be empty, which is easily ignored, if you haven't done well, there will be a lot of NULL pointer exceptions going online. To solve this problem, we have done some processing on redis keys.

Eg. We added cache for an activity configuration.

Class Activity {

String name; // name

Boolean limit1; // the maximum number of participants is 1.

Boolean limit2; // the maximum number of participants is 1.



If (limit1 ){

// Meet the conditions ..


Redis key; ACTIVITY_KEY expire time: 1 hour

If we have been running well in the official environment for a period of time, we have now added a limit of limit3;

The limit3 of the retrieved Activity in the cache is null,

If (limit3) {// throw NullPointExecption

// Meet the conditions ..


There may be many similar items in the project. My solution is key + version (Activity. class );

Private static int version (Class clazz ){
Return clazz. getDeclaredFields (). length;

By marking the number of fields in the class to be added to the cache, The redis key is automatically replaced after the attribute in the object class is updated to avoid null pointers.

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: 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.