spring boot: 本地缓存

1.     场景:初始化系统,自动加载错误码到本地缓存

1.     缓存类(tcloud-common-api/ SysErrorCodeMappingCache

package com.svw.tbox.tcloud.commons.cache;

 

import java.util.ArrayList;

import java.util.List;

import com.svw.tbox.tcloud.commons.api.entity.SysErrorCodeMapping;

 

/**

 * @Title

ClassName: SysErrorCodeMappingCache

 * @Description

Description: 错误码缓存类

 * @Author

Author: 胡锐锋

 * @Date

Date: 20171222

 */

publicclass SysErrorCodeMappingCache {

   

    private SysErrorCodeMappingCache() {

       super();

    }

 

    privatestatic List sysErrorCodeMappings = new ArrayList<>();

 

    publicstatic List getSysErrorCodeMappings() {

       returnsysErrorCodeMappings;

    }

 

    publicstaticvoid setSysErrorCodeMappings(List sysErrorCodeMappings) {

       SysErrorCodeMappingCache.sysErrorCodeMappings = sysErrorCodeMappings;

    }

}

 

2.     监听器(tcloud-mds/ SysErrorCodeLoadListener)

package com.svw.tbox.tcloud.mds.listener;

 

import java.util.List;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import javax.servlet.annotation.WebListener;

import org.springframework.beans.factory.annotation.Autowired;

import com.svw.tbox.tcloud.commons.api.entity.SysErrorCodeMapping;

import com.svw.tbox.tcloud.commons.cache.SysErrorCodeMappingCache;

import com.svw.tbox.tcloud.mds.feign.SysErrorCodeMappingFeign;

 

/**

 * @Title

ClassName: SysErrorCodeLoadListener

 * @Description

Description: 错误码缓存实现

 * @Author

Author: 胡锐锋

 * @Date

Date: 20171222

 */

@WebListener

publicclass SysErrorCodeLoadListener implements ServletContextListener {

    @Autowired

    private SysErrorCodeMappingFeign sysErrorCodeMappingFeign;

 

    @SuppressWarnings("unchecked")

    @Override

    publicvoid contextInitialized(ServletContextEvent sce) {

       // 加载redis中的缓存错误码

       List sysErrorCodeMappings = (List) sysErrorCodeMappingFeign.queryAll()

              .getData();

        SysErrorCodeMappingCache.setSysErrorCodeMappings(sysErrorCodeMappings);

    }

 

    @Override

    publicvoid contextDestroyed(ServletContextEvent sce) {

       // 监听销毁时执行

    }

}

 

3.     开启监听器扫描

@SpringBootApplication

@EnableHystrix

@EnableDiscoveryClient

@EnableFeignClients

@MapperScan(basePackages = "com.svw.tbox.tcloud.mds.dao")

@ServletComponentScan

publicclass MdsApplication {……

 

4.     刷新缓存设置

package com.svw.tbox.tcloud.mds.controller;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

 

import com.svw.tbox.tcloud.commons.api.entity.SysErrorCodeMapping;

import com.svw.tbox.tcloud.commons.cache.SysErrorCodeMappingCache;

import com.svw.tbox.tcloud.commons.web.Result;

import com.svw.tbox.tcloud.commons.web.SystemResponse;

import com.svw.tbox.tcloud.mds.feign.SysErrorCodeMappingFeign;

 

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

 

/**

 * @Title

ClassName: SysErrorCodeMappingController

 * @Description

Description: 本地缓存操作类

 * @Author

Author: 胡锐锋

 * @Date

Date: 20171222

 */

@RestController

@Api(description = "本地缓存服务")

@RequestMapping("/localCache")

publicclass LocalCacheController {

    @Autowired

    private SysErrorCodeMappingFeign sysErrorCodeMappingFeign;

 

    @SuppressWarnings("unchecked")

    @ApiOperation(value = "刷新错误码本地缓存")

    @RequestMapping(value = "/refreshSysErrorCodeMapping", method = RequestMethod.GET)

    public SystemResponse refreshSysErrorCodeMapping() {

       SysErrorCodeMappingCache.clear();

       List sysErrorCodeMappingList = (List) sysErrorCodeMappingFeign

              .queryAll().getData();

        SysErrorCodeMappingCache.setSysErrorCodeMappings(sysErrorCodeMappingList);

       return Result.success(sysErrorCodeMappingList);

    }

}

2.     场景:懒加载,小缓存,定时刷新

使用GuavaCache

1.     添加依赖

       <dependency>

           <groupId>com.google.guavagroupId>

           <artifactId>guavaartifactId>

       dependency>

2.     使用LoadingCache


//TODO 目前项目没用到


你可能感兴趣的:(spring,boot,guava)