redis缓存处理机制

 

1.redis缓存处理机制:先从缓存里面取,取不到去数据库里面取,然后丢入缓存中

例如:系统参数处理工具类

package com.ztesoft.iotcmp.utils;

import com.esotericsoftware.minlog.Log;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.iotcmp.service.systemparammgr.service.DcSystemParamService;
import com.ztesoft.iotcmp.util.PrimaryKeyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SystemParamUtils {

    public static String getValue(String paramCode,String pkey) throws Exception
    {
        // 先从缓存里面获取,缓存里面没有再查询
        StringRedisTemplate redisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
        String paramValue = redisTemplate.opsForValue().get("Value_"+paramCode+"_"+pkey);
        if (StringUtils.isBlank(paramValue)) {
            // 使用springUtil获取Service,去数据库里取值
            DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
            paramValue = dcSystemParamService.queryValueByCodeAndPKey(paramCode,pkey);

            if (StringUtils.isBlank(paramValue))
            {
                Log.error("数据库参数Value获取失败,paramCode="+paramCode+"pkey="+pkey);
                return  null;
            }

            // 再丢缓存里面去
            redisTemplate.opsForValue().set("Value_"+paramCode+"_"+pkey,paramValue);
        }
        return  paramValue;
    }

    public static List<String> getValueList(String paramCode) throws Exception
    {
        // 先从缓存里面获取,缓存里面没有再查询
        StringRedisTemplate redisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
        List<String> paramValueList = redisTemplate.opsForList().range("List_"+paramCode,0,-1);
        if (paramValueList == null || paramValueList.isEmpty()) {
            // 使用springUtil获取Service,去数据库里取值
            DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
            paramValueList = dcSystemParamService.queryValueListByCode(paramCode);

            if (paramValueList == null)
            {
                Log.error("数据库参数List获取失败,paramCode="+paramCode);
                return  null;
            }

            // 再丢缓存里面去
            redisTemplate.opsForList().leftPushAll("List_"+paramCode,paramValueList);
        }
        return  paramValueList;
    }

    public static Map<String,String> getMap(String paramCode) throws Exception
    {
        // 先从缓存里面获取,缓存里面没有再查询
        StringRedisTemplate stringRedisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
        Map resultValueMap = stringRedisTemplate.opsForHash().entries("Map_"+paramCode);
        if (resultValueMap == null || resultValueMap.isEmpty()) {
            // 使用springUtil获取Service,去数据库里取值
            DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
            List<Map> paramValueMap= dcSystemParamService.queryValueAndPKeyByCode(paramCode);

            if (paramValueMap == null || paramValueMap.isEmpty())
            {
                Log.error("数据库参数Map获取失败,paramCode="+paramCode);
                return  null;
            }

           resultValueMap = new HashMap<String, String>();
            for ( Map<String,String> map : paramValueMap) {
                String key = map.get("pkey");
                String value = map.get("param_value");
                resultValueMap.put(key, value);
            }
            // 再丢缓存里面去
            stringRedisTemplate.opsForHash().putAll("Map_"+paramCode,resultValueMap);
        }
        return  resultValueMap;
    }
}

 

public class PrimaryKeyUtils {    
    public static StringRedisTemplate getStringRedisTemplate() {
        RedisCache redisCache = (RedisCache) CacheFactory.getCacheClient(DataDictCache.getCacheNamespace());
        StringRedisTemplate stringRedisTemplate = redisCache.getRedisTemplate();
        return stringRedisTemplate;
    }
}

 

2.系统参数表设计结构

 

posted @ 2018-10-29 19:46  子渝渝渝  阅读(1195)  评论(0编辑  收藏  举报