Spring Boot缓存-缓存大小

3

我想配置我的缓存大小。我正在使用@EnableCaching。这是我的缓存存储库。

VendorRepository

public interface VendorRepository extends Repository<Vendor, Long> {

@Cacheable("vendorByUsername")
Vendor getVendorByUsername(String username);

@CacheEvict(value = {"vendorByUsername", "vendor", "vendors"}, allEntries = true)
Vendor save(Vendor vendor);

@Cacheable("vendor")
Vendor findOne(Long id);

@Cacheable("vendors")
List<Vendor> findAll();
}

目前情况良好,但我希望设置最大缓存大小。我该如何在主配置文件中进行配置?


这取决于你使用的缓存实现。Spring Cache 只是一个抽象层。 - Jaiwo99
我不使用任何实现。我将使用Ehcache。谢谢。 - fatiherdem
2个回答

3

@Jaiwo99是正确的。

Spring的缓存抽象层不涉及“管理”缓存内容的特定语义和“低级”细节(例如大小或相关的清除/过期)。这在很大程度上是因为这些低级管理细节因缓存提供商而异。

例如,一些缓存提供商/实现高度分布,具有不同的一致性、冗余和控制延迟的机制,等等。因此,基于这些特性提供一致的抽象将非常困难,因为某些提供商甚至没有实现所述功能,或者具有非常不同的“一致性”策略等。

无论如何,Spring参考指南中的此部分可能最好地总结了它...

8.7. 我如何设置TTL/TTI/驱逐策略/XXX功能?

直接通过您的缓存提供程序。缓存抽象层是一个抽象层,而不是一个缓存实现。您使用的解决方案可能支持各种数据策略和其他解决方案不支持的不同拓扑结构(例如,JDK ConcurrentHashMap - 将其暴露在缓存抽象中将是无用的,因为它没有后备支持)。此类功能应通过其本机API或直接通过其支持的缓存(在配置时)进行控制。

https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache-specific-config

干杯!


谢谢你的澄清,我会使用EhCache,因为我觉得它非常容易使用。 - fatiherdem

0
您可以使用 Caffeine 的实现,结合 spring-boot-starter-cache ,并在 application.properties 或 application.yml 文件中设置大小。
spring.cache.caffeine.spec=maximumSize=500

并添加到pom.xml文件中

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
        <version>2.5.6</version>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
        <version>3.0.4</version>
    </dependency>

这里的尺寸具体代表什么?是指物品的数量吗? - Prasannjeet Singh
1
@misteeque 是的,你说得对。 - Victor Levin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接