Section VII Data Dictionary: Hash Hash

One, Heart

        Redis hash field is a string type and the value of the mapping table, of course, here refers to a type of string of the data string serialization.

        Common business scenario is to cache pairs. I, for example, there is a classroom had thirty students, each student has a unique student number, then we can use the hash Redis store. Identify these classes as a student common (key), that is to say can find these students through this class. In which these students, each student (value) have a unique identifier, such as student number (field) to distinguish them.

         Data structure: key ------ field school classes ----- value specific number of students

         Therefore, immediately thought of using Hash cache Heart: There is a screening can identify a set of key data. In this group are filtered out of the data, each data themselves have a unique identifier field, in order to identify its value. Has such characteristics data, you can use Redis as a cache of hash.

        Common business scenarios

(A) cache dictionary table. A simple dictionary table, referring to the FIG. type field as a key, code field as the field, as are the corresponding record value.

(B) ordinary table cache. Table name (key) for filtering a set of data (all data in this table), the primary key ID (field) as each data record (value) unique identifier.

Second, the use Hash Example

        The page often have similar drop-down box pairs. For example, color, gender. These data will be stored in the dictionary table system.

        I consider the time line on the system, all data dictionary table of all loaded into Redis cache. And every time the operation dictionary table, update the cache.

        

        For example, there are above this dictionary tables. Suppose management page, to add a record, if successful operation dictionary table, then query all this data dictionary tables, to add to the Redis cache. As example, color type, color categories can be identified. sex as a type, capable expressed gender categories.

        Data structure diagram is as follows: 

        Key     -----------------    field  ---------------------   value

  Constant custom field type corresponding to a plurality of types of recording List <Parameter>

public Integer addParameter(Parameter parameter) {

        parameter.setId(null);
        mapper.insertSelective(parameter);

        if (parameter.getId() > 0) {
            //实时同步缓存
            cacheAllConfig();
        }

        return parameter.getId();
    }

    private void cacheAllConfig() {
        //清除上次缓存
        redisTemplate.delete(Constant.MAP_KEY);
        HashOperations<String, String, List<Parameter>> hashOperations = this.getHashOperations();
        //获取字典表的所有数据
        List<Parameter> parameters = mapper.getAll();
        //字典表的type作为唯一标识符,标识一组数据
        for (Parameter parameter : parameters) {
            //对应API命令  Hget  key field
            List<Parameter> parametersList = hashOperations.get(Constant.MAP_KEY, parameter.getType());
            if (parametersList == null || parametersList.isEmpty()) {
                //如果添加的种类参数不在缓存中,那么就创建一个List
                parametersList = new ArrayList<>();
            }
            parametersList.add(parameter);
            hashOperations.put(Constant.MAP_KEY, parameter.getType(), parametersList);
        }
    }

private HashOperations getHashOperations() {
        return redisTemplate.opsForHash();
    }

        How to get it?

public Map<String, List<Parameter>> getParameter() {
        HashOperations<String, String, List<Parameter>> hashOperations = getHashOperations();
        //Hash的entries命令,获取key对应的所有的 field - value
        //这里的field就是 type, value就是type对应的多条记录
        Map<String, List<Parameter>> result = hashOperations.entries(Constant.MAP_KEY);
        return result;
    }

    public List<Parameter> getParameterByType(final String type) {
        HashOperations<String, String, List<Parameter>> hashOperations = getHashOperations();
        //基础的get命令   hget key filed 
        List<Parameter> result = hashOperations.get(Constant.MAP_KEY, type);
        return result;
    }


private HashOperations getHashOperations() {
        return redisTemplate.opsForHash();
    }

        I would like to add a data dictionary table, above the trigger cache load operation, all data dictionary table according to type, Redis loaded into the cache.

       Then request all data dictionary tables on the page, of course, these data are from the Redis can avoid interacting with the DB.

        The last test to obtain the specified data dictionary table according to type.

Published 315 original articles · won praise 243 · views 260 000 +

Guess you like

Origin blog.csdn.net/yanluandai1985/article/details/104382513