Git Product home page Git Product logo

smf-cache's Introduction

smf-cache

spring boot 整合redis和ehcache并实现缓存过期设置

1、引入依赖

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

2、配置ehcache

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <!-- 测试 -->
    <cache name="myCache"
           eternal="false"
           timeToIdleSeconds="10"
           timeToLiveSeconds="20"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
    >
    </cache>
</ehcache>

3、application.properties配置

指定缓存类型ehcache、redis等,禁用none

spring.cache.type=ehcache

ehcache配置

spring.cache.ehcache.config=classpath:ehcache.xml

redis配置

spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
spring.redis.database=0

4、启动类添加注解:

@EnableCaching // 开始缓存功能

5、使用,@CacheConfig(cacheNames="myCache") 为全局配置,当缓存为ehcache时cacheNames=""必填且ehcache.xml中的缓存名称一致

//@CacheConfig(cacheNames="myCache")
@Service
public class CacheService {

    @Cacheable(value = "myCache",key="#p0")//#:取值方式    p:参数的意思parameter     0:下标
    public String  test1(Integer id) {
        System.out.println(id+"test1");
        return id.toString();
    }

    @Cacheable(value = "myCache",key="#id")
    public String  test2(Integer id) {
        System.out.println(id+"test2");
        return id.toString();
    }
    @Cacheable(value = "myCache",key="#id")
    public String  test3(Integer id) {
        System.out.println(id+"test3");
        return id.toString();
    }
}

smf-cache's People

Contributors

bluewindqaq avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.