springboot使用GuavaCache做简单缓存处理

使用GuavaCache可以快速建立缓存
1.需要在启动类上注解@EnableCaching
2.配置CacheManager
3.控制器上注解使用@Cacheable

pom.xml

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>4.3.9.RELEASEversion>
        dependency>
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>18.0version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>

CacheConfig.java 配置类

package application.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class CacheConfig {

    public CacheManager cacheManager(){
        GuavaCache guavaCache = new GuavaCache("GuavaCacheAll", CacheBuilder.newBuilder()
        .recordStats()
        .expireAfterWrite(10000, TimeUnit.SECONDS)
        .build());

        List list = new ArrayList();
        list.add(guavaCache);
        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(list);
        return simpleCacheManager;
    }
}

TestController.java 控制器测试类

package application.controller;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @RequestMapping("/test")
    //key是使用spEl取得参数,根据参数name作为缓存的key,value是使用的缓存list中的那个,具体看配置类
    @Cacheable(value = "GuavaCacheAll",key = "#name")
    public String tt(String name){
        System.out.println("in tt");
        return "name:"+name;
    }
}

Application.java springboot启动类

package application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {
     
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

你可能感兴趣的:(java,Cacheable,GuavaCache,EnableCaching)