zoukankan      html  css  js  c++  java
  • SpringBoot整合ehcache

    ehcache 属于jvm内置缓存

    1 添加依赖

            <!--开启 cache 缓存 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <!-- ehcache缓存 -->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.9.1</version><!--$NO-MVN-MAN-VER$ -->
            </dependency>

    2 ehcache配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
        <!-- 磁盘的缓存位置-->
        <diskStore path="java.io.tmpdir" />
        <!-- 默认缓存 -->
        <defaultCache maxElementsInMemory="1000" eternal="true"
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
            diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
            diskPersistent="true" diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        </defaultCache>
    
        <!-- demo缓存 -->
        <cache name="userCache" maxElementsInMemory="1000" eternal="false"
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
            diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
            diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        </cache>
        
    </ehcache>

    3 使用ehcache

    @MapperScan(basePackages = { "com.irish.mapper" })
    @EnableCaching
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }
    @CacheConfig(cacheNames = "userCache")
    @Service
    public class UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Cacheable
        public List<Users> getUser(Long id) {
            return userMapper.getUser(id);
        }
    
    }

    项目结构:

     github下载地址:https://github.com/jake1263/SpringBoot-ehcache

  • 相关阅读:
    SignalR的三个Demo
    SignalR的一点点东西
    如何在appconfig中配置服务的ip
    IP分片丢失重传
    以太网之物理层
    以太网数据格式与封装解封
    OSI七层模型与TCP/IP五层模型
    边沿检测方法-FPGA入门教程
    如何用ModelsimSE仿真IP核-以PLL为例
    搭建Modelsim SE仿真环境-使用do文件仿真
  • 原文地址:https://www.cnblogs.com/moris5013/p/11201044.html
Copyright © 2011-2022 走看看