For redis to store key/value pairs, after many stomps, we have a set of rules; This article describes the rules and precautions for defining key/value pairs.


The previous article explained how to define Redis client and Dubbo integrated storage; When we start developing, we suddenly find that we don’t know how to define Redis Key and Value formats. Don’t worry, we will understand how to define and use Redis Key and Value formats soon.

Spring+Dubbo integrates both Redis solutions

1. Redis value format

In regular Java development, we need to have an object-oriented mindset. JSON is a common and fast conversion format compared to objects. There are three popular Java libraries for JSON data processing: FastJSON, Gson and Jackson. Their advantages and disadvantages are not introduced here. This article uses Ali’s FastJSON.

JSON is mentioned above because in the Redis store, we use it to store values. Why do we do this? The main reason is that the JSON format has the following benefits:

· 1. Standard, mainstream data exchange format · 2. Simple, clear structure, more lightweight and easy to parse compared to XML · 3. · 4. Type-safe, values are typed, such as integers, strings, booleans, etcCopy the code

Let’s take a look at how to store values using JSON as follows:

/** * Insert key and value into redis database and set expiration time ** @param key k * @param value v * @param exp expiration time s * @return boolean
 */
@Override
public boolean set(String key, V value, int exp) { Jedis jedis = null; // String jKey = json.tojsonString (key); String jValue = JSON.toJSONString(value); Boolean isSucess = if the operation succeedstrue;
    if (StringUtils.isEmpty(key)) {
        LOG.info("key is empty");
        return false; } try {// get the client object jedis = rediscache.getResource (); // insert jedis.setex(key, exp, jValue); } catch (Exception e) { LOG.info("client can't connect server");
        isSucess = false;
        if(null ! = jedis) {/ / release jedis object redisCache brokenResource (jedis); }return false;
    } finally {
        if(isSucess) {/ / return the connection pool redisCache returnResource (jedis); }}return true;
}
Copy the code

In the code, we converted the Redis value before storing it, and converted the object V into A JSON object before storing it. Let’s look at the format of the value in Redis:

Above we can see in redis visualization tool RDM (Redis Desktop Manager), the key corresponding value with JSON is very clear display, very convenient for us to refer to the data stored in Redis.

I don’t know if you’ve noticed, but I commented out one line of the code above, which looks like this:

// String jKey = JSON.toJSONString(key);
Copy the code

The key key is json-friendly. So why comment this line? Here’s why.

First of all, json data will be a josn-formatted String, such as “zhangsan”. When this String is stored as a key, it will have json features by default, that is, double quotes “” will also be inserted into redis key Settings. Therefore, in RDM, we see the key value with “” by default, which is really not very beautiful, especially for our complex business, later we will explain how to use the key value rule in RDM to define the line of business folder.

2, redis key format

The above mentioned simple key storage, such as zhangsan storage, ordinary needs can be met at this time; However, in actual business, the storage of key keys is often very complicated. For example, we now have a requirement:

Requirement: Query the corresponding dictionary set according to the data dictionary type in the underlying data systemCopy the code

At this point, the business we need to pay attention to becomes complicated, and the conventional key storage method cannot be used. The above requirements can be roughly divided into:

1. System: basic data system 2. Module: data dictionary 3. Parameter: dictionary typeCopy the code

Why break it up like that? For readability; Also to abstract the key storage rules; In the case of complex business, if we define too many key keys, it is not easy to manage or search. With the rule definition of system-module-method-parameter, we can clearly understand what the values stored by Redis key do, and RDM can also be grouped according to this, which will be discussed later.

Here are the redis utility classes abstracted from this rule definition:

package com.yclimb.mdm.redis; /** * Redis ** @author yclimb * @date 2018/4/19 */ public class RedisUtils {/** * public static final */ String KEY_PREFIX ="mdm"; Private static final String KEY_SPLIT_CHAR = private static final String KEY_SPLIT_CHAR =":";

    /**
     * redis的key键规则定义
     * @param module 模块名称
     * @param func 方法名称
     * @param args 参数..
     * @return key
     */
    public static String keyBuilder(String module, String func, String... args) {
        returnkeyBuilder(null, module, func, args); } /** * Redis key rule definition * @param module module name * @param func method name * @param objStr object.toString () * @return key
     */
    public static String keyBuilder(String module, String func, String objStr) {
        returnkeyBuilder(null, module, func, new String[]{objStr}); } /** * Redis key rule definition * @param prefix item prefix * @param Module module name * @param func method name * @param objStr object.toString () * @return key
     */
    public static String keyBuilder(String prefix, String module, String func, String objStr) {
        returnkeyBuilder(prefix, module, func, new String[]{objStr}); } /** * Redis key rule definition * @param prefix item prefix * @param module module name * @param func method name * @param args parameter.. * @returnkey */ public static String keyBuilder(String prefix, String module, String func, String... Args) {// Project prefixif(prefix == null) { prefix = KEY_PREFIX; } StringBuilder key = new StringBuilder(prefix); // KEY_SPLIT_CHAR is the split character key.append(KEY_SPLIT_CHAR).append(module).append(KEY_SPLIT_CHAR).append(func);for (String arg : args) {
            key.append(KEY_SPLIT_CHAR).append(arg);
        }
        returnkey.toString(); } /** * Redis key rule definition * @param redisEnum Enum object * @param objStr object. ToString () * @return key
     */
    public static String keyBuilder(RedisEnum redisEnum, String objStr) {
        returnkeyBuilder(redisEnum.getKeyPrefix(), redisEnum.getModule(), redisEnum.getFunc(), objStr); }}Copy the code

This text describes the segmentation character in the above code, default [:], use: can be used to RDM group view; Redis keys are separated by colons by default. The advantage is that they can be grouped into folders in RDM, as shown in the figure below:

3. Use enumerated classes to define rules

The utility class above has the following code that uses an enumeration to assign values:

* @param redisEnum Enum object * @param objStr object. ToString () * @return key
 */
public static String keyBuilder(RedisEnum redisEnum, String objStr) {
    return keyBuilder(redisEnum.getKeyPrefix(), redisEnum.getModule(), redisEnum.getFunc(), objStr);
}
Copy the code

Here is the enumeration class code:

package com.yclimb.mdm.redis; /** * Redis enumeration class ** @author yclimb * @date 2018/4/19 */ public enum RedisEnum {/** * MDM_MSTDATADICTIONARYSERVICE_QUERYLISTBYENTITYREDIS( RedisUtils.KEY_PREFIX,"MstDataDictionaryService"."queryListByEntityRedis"."Data dictionary Redis cache"); /** * system id */ private String keyPrefix; /** * module name */ private String module; /** * private String func; /** * private String remark; RedisEnum(String keyPrefix, String module, String func, String remark) { this.keyPrefix = keyPrefix; this.module = module; this.func = func; this.remark = remark; } getter and setter.... }Copy the code

Using the above enumeration class, you can define the required enumeration type according to the module and method, easy to manage and maintain, it is also very convenient to use, using the code as follows:

@override public List<MstDataDictionary> queryListByEntityRedis(MstDataDictionary MstDataDictionary) {// redis key is obtained String redisKey = RedisUtils.keyBuilder(RedisEnum.MDM_MSTDATADICTIONARYSERVICE_QUERYLISTBYENTITYREDIS, (null == mstDataDictionary ?"": mstDataDictionary.toString())); List<MstDataDictionary> mstDataDictionaryList = (List<MstDataDictionary>) redisCacheService. Get (redisKey); // If there is no cache, query the database and assign the valueif (mstDataDictionaryList == null || mstDataDictionaryList.size() <= 0) {
		mstDataDictionaryList = mstDataDictionaryMapper.queryListByEntity(mstDataDictionary);
		redisCacheService.set(redisKey, mstDataDictionaryList);
	}
	return mstDataDictionaryList;
}
Copy the code

OK, here is almost finished, according to the above mentioned module mode, customize redis key name, value value format to use JSON to store;

Rule definitions for key keys can also be implemented using the Constants constant class, depending on your preferences and needs.

conclusion

Specific definition rules tool class code are above, but also source code.

To this article is over, pay attention to the public account to view more push!!