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.