Ehcache基于注解和API分别实现缓存

一、基于注解

二、基于API

1、导入ehcache依赖

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-cacheartifactId>
        dependency>
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcacheartifactId>
            <version>2.10.6version>
        dependency>

2、添加ehcache对应的配置信息


<ehcache>

 <diskStore path="D:\work\ehcache"/>               
 <defaultCache>
         maxEntriesLocalHeap="1000"
         eternal="false"
         timeToIdleSeconds="300"
         timeToLiveSeconds="600"
         overflowToDisk="true"
         memoryStoreEvictionPolicy="LRU">
 defaultCache>
 <cache name="whiteListCache"
        maxEntriesLocalHeap="200"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true">
 cache>
ehcache>

3、写业务方法,通过API实现缓存的增删改查。(改就是添加,相同key会被覆盖掉原来的)

!!! 抓住核心 Element对象的创建。

@RestController
@RequestMapping("/whiteList/cache")
public class WhiteListCacheController {

    private final static String top = "whiteList_";
    public final static CacheManager cacheManager = CacheManager.create("src/main/resources/ehcache.xml");
    public final static Cache whiteListCache = cacheManager.getCache("whiteListCache");

    /**
     * 白名单缓存的添加或修改
     *
     * @param ehcache
     * @return
     */
    @PostMapping("/add")
    public Result<Element> addOrUpdataCache(@RequestBody Ehcache ehcache) {
        Element element = new Element(top + ehcache.getKey(), ehcache.getValue());
        whiteListCache.put(element);
        return Result.buildResult(200, "成功");
    }

    /**
     * 白名单缓存的删除
     *
     * @param ehcache
     * @return
     */
    @PostMapping("/delete")
    public Result Demo(@RequestBody Ehcache ehcache) {
        boolean remove = whiteListCache.remove(top + ehcache.getKey());
        if (remove) {
            return Result.buildResult(200, "成功");
        }
        return Result.buildResult(400, "缓存清理失败!");
    }

    /**
     * 指定key获取对应缓存信息
     *
     * @param ehcache
     * @return
     */
    @PostMapping("/get")
    public Result<Object> get(@RequestBody Ehcache ehcache) {
        Element element = null;
        try {
            element = whiteListCache.get(top + ehcache.getKey());
            return Result.buildResult(ApiCode.OK, element.getObjectValue());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildResult(ApiCode.OK, "代理服务没有获取到当前缓存!");
        }
    }

    /**
     * 白名单走缓存验证
     *
     * @return
     */
    @PostMapping("/checking")
    public Boolean checkingList(@RequestBody TBAppNodes tbAppNodes) {
        String appid = tbAppNodes.getAPPID();
        String nodeip = tbAppNodes.getNODEIP();
        try {
            Element element = whiteListCache.get(top + appid);
            List<String> objectValue = (ArrayList<String>) element.getObjectValue();
            for (String s : objectValue) {
                if (nodeip.equals(s)) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

你可能感兴趣的:(缓存,java,开发语言)