山一程--软件开发--基础设施--缓存--spring cache

目的:系统基础设施缓存设定.


thanks refence:

1.<Spring 设计模式> chapter 9

 


缓存使用最佳实践(Web 应用场景):

1.Spring web 应用, @Cacheable, @CachePut 和 @CacheEvict 这些 Spring 缓存声明应该被用在具体类上, 而不是接口上.

  Java 声明是不会被接口继承.

2. 不要在相同类里直接调用被 @Cacheable ... 声明的任何方法, 因为对缓存方法的直接调用,是不能使用 Spring AOP 代理的.

3. 企业级应用中,Java Map 或任何键值集合永远不要作为缓存. 因为缓存提供了超过存储键值的能力.

4. Spring ,在应用中使用缓存抽象层。@Cacheable 声明可以将业务逻辑代码 和缓存关注点分离.

5.应用缓存时,像 Web Service 或昂贵的数据库 这种成本很大的才用缓存,因为每次 API 调用都有开销.

6.应用的缓存实现中,需要保证缓存中的数据是与数据库同步的,

7.当从数据库进行缓慢的数据查询时得到的数据每次都不太相同时缓存只能作为备选方案, 因为无论缓存了什么, 第一步检查

  缓存值不存在时都会调用实际方法,所以是没必要使用缓存.

8.缓存一般是工作在应用的服务层.


 

Caffeine

thanks reference: 

https://blog.csdn.net/weixin_42666623/article/details/131310750

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

使用 Window-TinyLfu 缓存淘汰策略,综合了 LRU (least Recently Used)和 LFU ( Least Frequently Used) 长处.

 

posted @ 2023-09-10 12:13  君子之行  阅读(13)  评论(0)    收藏  举报