1

Spring Boot中启用和使用缓存

 7 months ago
source link: https://www.jdon.com/72361.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Spring Boot中启用和使用缓存

由于Spring端只提供了缓存处理的接口,因此在使用缓存时需要准备单独的实现。
Caffeine是一个提供缓存实现的模块

dependencies {
  // Spring Boot Starter Cache
  implementation 'org.springframework.boot:spring-boot-starter-cache'
  // Caffeine
  implementation "com.github.ben-manes.caffeine:caffeine:3.0.5"
}

启用缓存
在 Main 类中添加"@EnableCaching "注解,在应用程序中启用缓存功能。

除 Main 类外,还可将"@EnableCaching "注解授予 JavaConfig 类。
当您想仅在应用程序使用特定功能时启用缓存功能时,请使用此方法。

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class WebappApplication {

/**
     * 启动网络应用程序
     *

*/

    public static void main(String[] args) {
        SpringApplication.run(WebappApplication.class, args);
    }
}

定义使用缓存的方法
为希望缓存方法执行结果的方法赋予"@Cacheable "注解。
此外,在项目中使用包含要缓存的方法的类时,请在 DI 容器中注册该类。
(因为 SpringBoot 中的缓存是使用 AOP 功能实现的)。

import org.springframework.cache.annotation.Cacheable;

@Component
public class CacheSample {
    @Cacheable(cacheNames = "sampleCache", sync = true)
    public int cache(int num) {
        return num * 2;
    }
}

参数解释:

1、cacheNames
存储方法执行结果的缓存的名称。

2、key
用于动态计算键的 SpEL 表达式。默认值为空字符串,这意味着除非配置了自定义 keyGenerator,否则所有方法参数都被视为键。
SpEL 表达式根据提供以下元数据的专用上下文进行评估:

  • root.method、root.target、root.caches:分别对方法、目标对象和受影响的缓存的引用
  • root.methodName, root.targetClass: 方法名,分别是对目标类的引用
  • 方法参数的索引访问:例如,第二个参数可以用root.args[1],p1a1

3、keyGenerator
您自己的密钥生成器的 Bean 名称(org.springframework.cache.interceptor.KeyGenerator)。

4、cacheManager缓存管理器
如果未设置管理器 默认

5、cacheResolver缓存解析器
您自己的缓存解析器的 Bean 名称(org.springframework.cache.interceptor.CacheResolver)。

6、condition
定义方法缓存条件的 SpEL 表达式。默认值为空字符串,这意味着该方法的结果始终被缓存。
SpEL 表达式根据提供以下元数据的专用上下文进行评估:

  • root.method、root.target、root.caches:分别对方法、目标对象和受影响的缓存的引用
  • root.methodName, root.targetClass: 方法名,分别是对目标类的引用
  • 方法参数的索引访问:例如,第二个参数可以用root.args[1],p1a1

7、unless
SpEL 表达式,用于定义拒绝方法缓存的条件。
默认为空字符串,表示没有拒绝缓存的特定条件。

与条件属性不同,该表达式在方法调用后才进行评估,因此可以引用方法的执行结果。

SpEL 表达式根据提供以下元数据的专用上下文进行评估:

  • result:对方法调用结果的引用。在诸如Optional之类的包装器中,result指的是实际对象,而不是包装器。
  • root.method、root.target、root.caches:分别对方法、目标对象和受影响的缓存的引用
  • root.methodName, root.targetClass: 方法名,分别是对目标类的引用
  • 方法参数的索引访问:例如,第二个参数可以用root.args[1],p1a1

8、sync
当多个线程尝试加载相同的键值时是否同步方法调用。同步有以下限制:

  • unless属性不可用
  • 只能指定一个缓存
  • 不能与其他缓存相关操作结合使用

缓存设置
在使用缓存的项目的application.yml中配置缓存过期时间等设置。

# Spring 缓存设置
spring : 
  cache : 
   # 缓存名称(可以多选,用逗号分隔)
    cache-names : SampleCache
    # 缓存过期日期(缓存创建到删除的时间)
    caffeine : 
      spec : expireAfterWrite=30m

调用使用缓存的方法
使用缓存的方法可以像其他方法一样被调用。

@Service
public class SampleServiceImpl implements SampleService {

private CacheSample cacheSample;

public SampleServiceImpl(CacheSample cacheSample) {
        this.cacheSample = cacheSample;
    }

@Override
    public void execute(int num) {
        int result = cacheSample.cache(num);
    }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK