哈希Hash实战之系统数据字典实时触发与层级存储

内容:典型的数据字典:"支付状态",其数据选项的取值包括:(1=未支付;2=支付成功;3=支付失败...),

对应的前端筛选组件就是"下拉框"

创建字典配置表s_config

create table s_config(
	id int(11) not null auto_increment,
    type varchar(100) character set utf8mb4 not null comment "字典类型",
    name varchar(100) character set utf8mb4 not null comment "字典名称",
    code varchar(100) character set utf8mb4 not null comment "选项编码",
    value varchar(100) character set utf8mb4 not null comment "选项取值",
    order_by int(11) default 1 comment "排序",
    is_active tinyint(4) default 1 comment "是否有效(1=是;0=否)",
    create_time datetime default current_timestamp comment "创建时间",
    primary key(id),
    unique key idx_type_Code (type, code) using btree
)engine=InnoDB auto_increment=5 default charset=utf8 comment="字典配置表";

逆向工程

HashController.java

@RestController
@RequestMapping("hash")
public class HashController extends AbstractController {

    @Autowired
    private HashService hashService;

    //添加数据字典
    @RequestMapping(value = "put",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResponse put(@RequestBody @Validated SysConfig config, BindingResult result){
        String checkRes= ValidatorUtil.checkResult(result);
        if (StrUtil.isNotBlank(checkRes)){
            return new BaseResponse(StatusCode.Fail.getCode(),checkRes);
        }
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            hashService.addSysConfig(config);

        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    //获取所有数据字典
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public BaseResponse get(){
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            response.setData(hashService.getAll());

        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    //获取特定的数据字典及其选项
    @RequestMapping(value = "get/type",method = RequestMethod.GET)
    public BaseResponse getType(@RequestParam String type){
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            response.setData(hashService.getByType(type));

        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }
}

HashService.java

@Service
public class HashService {

    private static final Logger log= LoggerFactory.getLogger(HashService.class);

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private HashRedisService redisService;

    //TODO:添加数据字典及其对应的选项(code-value)
    @Transactional(rollbackFor = Exception.class)
    public Integer addSysConfig(SysConfig config) throws Exception{
        config.setId(null);
        int res=sysConfigMapper.insertSelective(config);
        if (res>0){
            //插入db成功之后,实时触发数据字典的hash存储
            redisService.cacheSysConfigs();
        }
        return config.getId();
    }

    //TODO:取出缓存中所有的数据字典列表
    public Map<String,List<SysConfig>> getAll() throws Exception{
        return redisService.getAllSysConfigs();
    }

    //TODO:取出缓存中特定的数据字典列表
    public List<SysConfig> getByType(final String type) throws Exception{
        return redisService.getSysConfigsByType(type);
    }
}

HashRedisService.java

@Service
public class HashRedisService {

    private static final Logger log= LoggerFactory.getLogger(HashRedisService.class);

    @Autowired
    private SysConfigMapper configMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    //实时触发db中数据字典的缓存存储
    public void cacheSysConfigs(){
        try {

            //从db获取出所有
            List<SysConfig> list=configMapper.selectActiveConfigs();
            if (list!=null && !list.isEmpty()){
                //大类:type;小类: field-value (code-value) 对
                Map<String,List<SysConfig>> typeMap= Maps.newHashMap();

                list.forEach(config -> {
                    List<SysConfig> configs=typeMap.get(config.getType());
                    if (configs==null || configs.isEmpty()){
                        configs= Lists.newLinkedList();
                    }
                    configs.add(config);

                    typeMap.put(config.getType(),configs);
                });

                //TODO:存储至hash中
                HashOperations<String,String,List<SysConfig>> hashOperations=redisTemplate.opsForHash();
                hashOperations.putAll(Constant.RedisHashKeyConfig,typeMap);
            }
        }catch (Exception e){
            log.error("实时触发db中数据字典的缓存存储-发生异常:",e);
        }
    }

    //获取所有的数据类型
    public Map<String,List<SysConfig>> getAllSysConfigs(){
        Map<String,List<SysConfig>> resMap=Maps.newHashMap();
        try {
            HashOperations<String,String,List<SysConfig>> hashOperations=redisTemplate.opsForHash();
            resMap=hashOperations.entries(Constant.RedisHashKeyConfig);

        }catch (Exception e){
            log.error("获取所有的数据类型-发生异常:",e);
        }
        return resMap;
    }

    //根据数据类型编码获取特定的选项编码列表
    public List<SysConfig> getSysConfigsByType(final String type){
        List<SysConfig> list=Lists.newLinkedList();
        try {
            HashOperations<String,String,List<SysConfig>> hashOperations=redisTemplate.opsForHash();
            list=hashOperations.get(Constant.RedisHashKeyConfig,type);

        }catch (Exception e){
            log.error("根据数据类型编码获取特定的选项编码列表-发生异常:",e);
        }
        return list;
    }
}


总结一:由于hash数据结构key-value(key~field-value)的特性,因此它特别适合缓存那种具有多种值选择的数据,比如下拉框就是典型的案例

总结二:比如"支付状态"有"未支付-Unpay", "支付成功-Payed", "支付失败...”

猜你喜欢

转载自blog.csdn.net/weixin_37841366/article/details/109149349