(用注解)springboot 整合缓存(Ehcache或者reids)

2019-09-28 07:57:34

这里介绍Spring Boot结合JPA,MySQL和Ehcache实现缓存功能,提高程序访问效率。

一、Maven依赖

  

<!-- caching -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

二、程序的启动类加上 @EnableCaching

3、application.yml和ehcache.xml配置文件 

#使用ehcache缓存配置
spring:
    cache:
        type: ehcache
        ehcache:
          config: classpath:ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <cache name="sysCache"
           maxElementsInMemory="100000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="true"
           maxElementsOnDisk="10000000"
           diskPersistent="true"
           memoryStoreEvictionPolicy="LRU"/>
    <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="600"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>

注意:
1)@CacheConfig(cacheNames = {“lemonCache”})设置了ehcache的名称,这个名称就是ehcache.xml内的名称; (可以不指定,应为在yml 中已经制定了)
2)@Cacheable:应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调 用方法获取数据,然后把数据添加到缓存中,适用于查找;
3)@CachePut:主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。适用于更新和插入;
4)@CacheEvict:主要针对方法配置,能够根据一定的条件对缓存进行清空。适用于删除。

整合reids   https://blog.csdn.net/plei_yue/article/details/79362372


  • 2023-09-21 16:49:06

    用webpack.ProvidePlugin来解决Photo-Sphere-Viewer旧版本浏览器兼容问题

    上篇文章已经搭建了最基本的项目,我用手机下载了一个版本62.点开头的chrome apk来测试。 果真白屏,啥也没有,也看不到报错信息。 赶紧安装 vconsole ,重启,刷新,依然白屏,连vconsole也没有。这可咋弄。 想了下,这浏览器老的是可以呀,还得修改编译参数,加大es5的处理。 根目录有一个配置文件 .browserslistrc 里面的配置如下