Преглед на файлове

新增资源化字典缓存

Airhead преди 6 години
родител
ревизия
0b5b1f9c30

+ 14 - 0
src/main/java/com/yihu/ehr/resource/constants/MicroServiceApi.java

@ -0,0 +1,14 @@
package com.yihu.ehr.resource.constants;
/**
 * @author l4qiang
 */
public interface MicroServiceApi {
    interface Service {
        String RESOURCE = "svr-resource";
    }
    interface Resource {
        String DICT_ENTRY = "cache/dict-entry";
    }
}

+ 31 - 16
src/main/java/com/yihu/ehr/resource/controller/RsDictionaryEntryEndPoint.java

@ -1,9 +1,10 @@
package com.yihu.ehr.resource.controller;
import com.yihu.ehr.constants.ServiceApi;
import com.yihu.ehr.constants.ApiVersion;
import com.yihu.ehr.constants.ServiceApi;
import com.yihu.ehr.controller.EnvelopRestEndPoint;
import com.yihu.ehr.model.resource.MRsDictionaryEntry;
import com.yihu.ehr.resource.constants.MicroServiceApi;
import com.yihu.ehr.resource.model.RsDictionaryEntry;
import com.yihu.ehr.resource.service.RsDictionaryEntryService;
import io.swagger.annotations.Api;
@ -50,7 +51,6 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
    }
    @RequestMapping(value = ServiceApi.Resources.DictEntries, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建标准字典项", notes = "创建标准字典项")
    public MRsDictionaryEntry createRsDictionaryEntry(
@ -60,7 +60,7 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
        RsDictionaryEntry dictionaryEntry = toEntity(jsonData, RsDictionaryEntry.class);
        String code = dictionaryEntry.getCode();
        String dictCode = dictionaryEntry.getDictCode();
        if(isExistence(dictCode,code)){
        if (isExistence(dictCode, code)) {
            throw new Exception("字典项代码不能重复");
        }
        rsDictionaryEntryService.insert(dictionaryEntry);
@ -68,7 +68,7 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
    }
    @RequestMapping(value =ServiceApi.Resources.DictEntries, method = RequestMethod.PUT)
    @RequestMapping(value = ServiceApi.Resources.DictEntries, method = RequestMethod.PUT)
    @ApiOperation(value = "修改标准字典项", notes = "修改标准字典项")
    public MRsDictionaryEntry updateRsDictionaryEntry(
            @ApiParam(name = "json_data", value = "")
@ -78,7 +78,7 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
        RsDictionaryEntry d = rsDictionaryEntryService.findById(dictionaryEntry.getId());
        String code = dictionaryEntry.getCode();
        String dictCode = dictionaryEntry.getDictCode();
        if(!d.getCode().equals(code) && isExistence(dictCode,code)){
        if (!d.getCode().equals(code) && isExistence(dictCode, code)) {
            throw new Exception("字典代码不可修改");
        }
        rsDictionaryEntryService.save(dictionaryEntry);
@ -105,26 +105,25 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
    @RequestMapping(value = ServiceApi.Resources.DictEntriesByDictCode, method = RequestMethod.GET)
    @ApiOperation(value = "根据dict_code获取获取标准字典")
    public List<MRsDictionaryEntry>  getRsDictionaryEntryByDictCode(
    public List<MRsDictionaryEntry> getRsDictionaryEntryByDictCode(
            @ApiParam(name = "dict_code", value = "", defaultValue = "")
            @RequestParam(value = "dict_code") String dict_code)
    {
            @RequestParam(value = "dict_code") String dict_code) {
        List<RsDictionaryEntry> dictionaryEntries = rsDictionaryEntryService.findByDictCode(dict_code);
        return (List<MRsDictionaryEntry>) convertToModels(dictionaryEntries, new ArrayList<>(dictionaryEntries.size()), MRsDictionaryEntry.class,null);
        return (List<MRsDictionaryEntry>) convertToModels(dictionaryEntries, new ArrayList<>(dictionaryEntries.size()), MRsDictionaryEntry.class, null);
    }
    @RequestMapping(value = ServiceApi.Resources.DictEntriesExistence,method = RequestMethod.GET)
    @RequestMapping(value = ServiceApi.Resources.DictEntriesExistence, method = RequestMethod.GET)
    @ApiOperation("根据过滤条件判断是否存在")
    public boolean isExistence(
            @ApiParam(name="filters",value="filters",defaultValue = "")
            @RequestParam(value="filters") String filters) throws Exception {
            @ApiParam(name = "filters", value = "filters", defaultValue = "")
            @RequestParam(value = "filters") String filters) throws Exception {
        List ls = rsDictionaryEntryService.search("",filters,"", 1, 1);
        return ls!=null && ls.size()>0;
        List ls = rsDictionaryEntryService.search("", filters, "", 1, 1);
        return ls != null && ls.size() > 0;
    }
    public boolean isExistence(String dictCode,String code) {
        return rsDictionaryEntryService.findByFields(new String[]{"dictCode","code"},new String[]{dictCode,code}).size() != 0;
    public boolean isExistence(String dictCode, String code) {
        return rsDictionaryEntryService.findByFields(new String[]{"dictCode", "code"}, new String[]{dictCode, code}).size() != 0;
    }
@ -136,4 +135,20 @@ public class RsDictionaryEntryEndPoint extends EnvelopRestEndPoint {
        List<RsDictionaryEntry> dictionaryEntries = rsDictionaryEntryService.search(filters);
        return (List<MRsDictionaryEntry>) convertToModels(dictionaryEntries, new ArrayList<>(dictionaryEntries.size()), MRsDictionaryEntry.class, "");
    }
    @RequestMapping(value = MicroServiceApi.Resource.DICT_ENTRY, method = RequestMethod.GET)
    @ApiOperation(value = "根据code从缓存获取字典项值")
    public String getCacheValue(
            @ApiParam(name = "dict") @RequestParam(value = "dict") String dict,
            @ApiParam(name = "code") @RequestParam(value = "code") String code) {
        return rsDictionaryEntryService.getCacheValue(dict, code);
    }
    @RequestMapping(value = MicroServiceApi.Resource.DICT_ENTRY, method = RequestMethod.DELETE)
    @ApiOperation(value = "清空缓存字典项")
    public void deleteCacheValue(
            @ApiParam(name = "dict") @RequestParam(value = "dict") String dict) {
        rsDictionaryEntryService.deleteCacheValue(dict);
    }
}

+ 2 - 2
src/main/java/com/yihu/ehr/resource/dao/ResourceBrowseDao.java

@ -14,8 +14,8 @@ import com.yihu.ehr.query.common.sqlparser.ParserFactory;
import com.yihu.ehr.query.common.sqlparser.ParserSql;
import com.yihu.ehr.query.services.HbaseQuery;
import com.yihu.ehr.query.services.SolrQuery;
import com.yihu.ehr.resource.client.QuotaStatisticsClient;
import com.yihu.ehr.resource.client.StdTransformClient;
import com.yihu.ehr.resource.feign.QuotaStatisticsClient;
import com.yihu.ehr.resource.feign.StdTransformClient;
import com.yihu.ehr.resource.model.*;
import com.yihu.ehr.resource.service.RedisService;
import com.yihu.ehr.resource.service.RsResourceService;

+ 3 - 1
src/main/java/com/yihu/ehr/resource/dao/RsDictionaryEntryDao.java

@ -10,10 +10,12 @@ import java.util.List;
 * @author linaz
 * @created 2016.05.17 16:33
 */
public interface RsDictionaryEntryDao extends PagingAndSortingRepository<RsDictionaryEntry,Integer> {
public interface RsDictionaryEntryDao extends PagingAndSortingRepository<RsDictionaryEntry, Integer> {
    List<RsDictionaryEntry> findByDictCode(String code);
    RsDictionaryEntry findByDictcodeAndCode(String dict, String code);
    @Query("SELECT count(e) FROM RsDictionaryEntry e WHERE e.dictId=?1")
    int countByDictId(int dictId);

+ 1 - 2
src/main/java/com/yihu/ehr/resource/client/QuotaStatisticsClient.java

@ -1,4 +1,4 @@
package com.yihu.ehr.resource.client;
package com.yihu.ehr.resource.feign;
import com.yihu.ehr.constants.ApiVersion;
import com.yihu.ehr.constants.MicroServices;
@ -34,5 +34,4 @@ public interface QuotaStatisticsClient {
            @RequestParam(value = "dimension", required = false) String dimension,
            @ApiParam(name = "top", value = "获取前几条数据")
            @RequestParam(value = "top", required = false) String top);
}

+ 1 - 1
src/main/java/com/yihu/ehr/resource/client/StdTransformClient.java

@ -1,4 +1,4 @@
package com.yihu.ehr.resource.client;
package com.yihu.ehr.resource.feign;
import com.yihu.ehr.constants.ApiVersion;
import com.yihu.ehr.constants.MicroServices;

+ 1 - 1
src/main/java/com/yihu/ehr/resource/service/ResourceBrowseService.java

@ -12,7 +12,7 @@ import com.yihu.ehr.profile.family.ResourceCells;
import com.yihu.ehr.query.BaseJpaService;
import com.yihu.ehr.query.common.model.QueryCondition;
import com.yihu.ehr.query.services.SolrQuery;
import com.yihu.ehr.resource.client.StdTransformClient;
import com.yihu.ehr.resource.feign.StdTransformClient;
import com.yihu.ehr.resource.dao.ResourceBrowseDao;
import com.yihu.ehr.resource.dao.ResourceBrowseMetadataDao;
import com.yihu.ehr.resource.dao.RsResourceDao;

+ 41 - 0
src/main/java/com/yihu/ehr/resource/service/RsDictionaryEntryService.java

@ -1,6 +1,7 @@
package com.yihu.ehr.resource.service;
import com.yihu.ehr.query.BaseJpaService;
import com.yihu.ehr.redis.client.RedisClient;
import com.yihu.ehr.resource.dao.RsDictionaryEntryDao;
import com.yihu.ehr.resource.model.RsDictionaryEntry;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,6 +18,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Set;
/**
 * @author linaz
@ -29,8 +31,47 @@ public class RsDictionaryEntryService extends BaseJpaService<RsDictionaryEntry,
    @Autowired
    protected JdbcTemplate jdbcTemplate;
    @Autowired
    protected RedisClient redisClient;
    @Autowired
    private RsDictionaryEntryDao dictionaryEntryDao;
    /**
     * 简化资源字典缓存,默认缓存3600秒
     *
     * @param dict
     * @param code
     * @return
     */
    public String getCacheValue(String dict, String code) {
        String key = "rs:dict-entry:" + dict + ":" + code;
        String value = redisClient.get(key);
        if (value != null) {
            return value;
        }
        RsDictionaryEntry rsDictionaryEntry = dictionaryEntryDao.findByDictcodeAndCode(dict, code);
        if (rsDictionaryEntry == null) {
            return "";
        }
        redisClient.set(key, rsDictionaryEntry.getName(), 3600);
        return rsDictionaryEntry.getName();
    }
    /**
     * 清空资源字典缓存,资源字典更新时需要调用
     *
     * @param dict
     * @return
     */
    public void deleteCacheValue(String dict) {
        String key = "rs:dict-entry:" + dict + ":*";
        Set<String> keys = redisClient.keys(key);
        keys.forEach(k -> redisClient.delete(k));
    }
    public RsDictionaryEntry findById(int id) {
        return dictionaryEntryDao.findOne(id);
    }