• JetCache设计原理浅析


    1、目录

    2、JetCache介绍

    image.png

    3、如何设计一个缓存组件?

    image.png

    4、SpringCache VS JetCache

    image.png

    5、JetCache基本使用

    5.1 JetCache配置信息

    1. jetcache:
    2. statIntervalMinutes: 60
    3. areaInCacheName: false
    4. penetrationProtect: true
    5. enableMethodCache: true
    6. hiddenPackages: com.xxx.xxx,com.xxx.xxx
    7. local:
    8. default:
    9. type: caffeine # 支持的类型:linkedhashmap、caffeine
    10. limit: 100
    11. keyConvertor: fastjson # 支持的类型:fastjson,可自定义转换器函数
    12. expireAfterWriteInMillis: 600000
    13. expireAfterAccessInMillis: 300000
    14. remote:
    15. default:
    16. type: redis.lettuce # 支持的类型:redis、redis.lettuce
    17. keyPrefix: '系统简称:所属名字:'
    18. keyConvertor: fastjson
    19. valueEncoder: java # 支持的类型:kryo、java,可自定义编码器
    20. valueDecoder: java # 支持的类型:kryo、java,可自定义解码器
    21. expireAfterWriteInMillis: 3600000
    22. #readFrom: slavePreferred # 优先从Slave节点中读取
    23. uri: redis-sentinel://host1:26379,host2:26379,host3:26379/?sentinelMasterId=mymaster # 哨兵模式
    24. #uri: redis://127.0.0.1:6379/ # 单节点模式
    25. #mode: masterslave # 设置为主从模式
    26. #uri: # 集群模式
    27. #- redis://127.0.0.1:7000
    28. #- redis://127.0.0.1:7001
    29. #- redis://127.0.0.1:7002

    image.pngimage.pngimage.png

    5.2、JetCache使用示例

    image.pngimage.pngimage.pngimage.pngimage.png

    6、JetCache原理

    image.png

    • Cache:缓存接口,定义基本方法
    • AbstractCache:抽象类,缓存接口的继承者,提供基本实现,具体实现交由不同的子类
    • LinkedHashMapCache:基于LinkedHashMap设计的简易内存缓存
    • CaffeineCache:基于Caffeine工具设计的内存缓存
    • RedisCache:Redis实现,使用Jedis客户端
    • RedisLettuceCache:Redis实现,使用Lettuce客户端
    • MultiLevelCache:两级缓存,用于封装EmbeddedCache(本地缓存)和ExternalCache(远程缓存)
    • RefreshCache:基于装饰器模式Decorator,提供自动刷新功能
    • LazyInitCache:用于@CreateCache注解创建的缓存实例,依赖于Spring

    JetCache源码入口

    @EbableMethodCache -> JetCacheInterceptor
    JetCacheAutoConfiguration

    缓存get/put基本实现

    Cache->AbstractCache->AbstractEmbeddedCache->LinkedHashMapCache
    get()->GET()->do_GET()->map.get()

    获取/存放 数据 -> 构建CacheResult -> 数据统计CacheState -> 缓存监控CacheMonitor

    缓存过期时间的实现

    被动过期:image.png
    主动过期 Cleaner:image.pngimage.png

    缓存自动刷新的实现

    RefreshCacheimage.pngimage.pngimage.png

    防止缓存击穿的实现

    @CachePenetrationProtect -> AbstractCache.computeIfAbsentImpl() -> synchronizedLoad();image.pngimage.png

    参考:

    1. alibaba/jetcache

    2. JetCache 缓存框架的使用以及源码分析

    3. JetCache源码分析

  • 相关阅读:
    数据结构 - 栈和队列
    【Leetcode刷题Python】剑指 Offer 11. 旋转数组的最小数字
    Java 面试题集锦,横扫金九银十。
    实战!RPA厂商选型分享
    基于Vue和Element UI实现前后端分离和交互
    GBPC1510W-ASEMI铝底塑壳针脚高散热方桥GBPC1510W
    集群聊天项目
    损失函数——机器学习
    C++11之智能指针
    百度关键词pc端排名查询易语言代码
  • 原文地址:https://blog.csdn.net/u012881584/article/details/126715371