当前位置:   article > 正文

SpringBoot 整合 各种缓存技术_springboot集成缓存

springboot集成缓存

单一缓存方案

使用SpringBoot提供的统一缓存接口spring-boot-starter-cache, 以下的几个例子都是spring-boot-starter-cache的具体底层实现,但是不推荐使用这些,功能不灵活。只是介绍

提前准备的类

  1. import lombok.Builder;
  2. import lombok.Data;
  3. @Data
  4. @Builder
  5. public class Book {
  6. private Integer id;
  7. private String name;
  8. private String description;
  9. public Book(Integer id, String name, String description) {
  10. this.id = id;
  11. this.name = name;
  12. this.description = description;
  13. System.out.println("Book 创建完成"+ this.id);
  14. }
  15. }

1.SpringBoot内置缓存方案(Simple)

springboot技术提供有内置的缓存解决方案,可以帮助开发者快速开启缓存技术,并使用缓存技术进行数据的快速操作

步骤①:导入springboot提供的缓存技术对应的starter

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>

步骤②:启用缓存,在引导类上方标注注解@EnableCaching配置springboot程序中可以使用缓存

  1. @SpringBootApplication
  2. //开启缓存功能
  3. @EnableCaching
  4. public class Springboot19CacheApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Springboot19CacheApplication.class, args);
  7. }
  8. }

步骤③:设置获得的数据是否来自于缓存

  1. @Service
  2. public class BookService {
  3. @Cacheable(value = "cache_namespace",key = "#id")
  4. public Book getById(Integer id) {
  5. return Book.builder()
  6. .id(id)
  7. .name("book_name" + id)
  8. .description("book_desc" + id)
  9. .build();
  10. }
  11. }

在业务方法上面使用注解@Cacheable声明当前方法的返回值放入缓存中,其中要指定缓存的存储位置,以及缓存中保存当前方法返回值对应的名称。上例中value属性描述缓存的存储位置,可以理解为是一个存储空间名,key属性描述了缓存中保存数据的名称,使用#id读取形参中的id值作为缓存名称。

使用@Cacheable注解后,执行当前操作,如果发现对应名称在缓存中没有数据,就正常Build数据,然后放入缓存;如果对应名称在缓存中有数据,就终止当前业务方法执行,直接返回缓存中的数据。

缺点: 无法设置过期时间等,灵活性差

2.SpringBoot整合Ehcache缓存

步骤①:导入Ehcache的坐标

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>net.sf.ehcache</groupId>
  7. <artifactId>ehcache</artifactId>
  8. </dependency>

此处为什么不是导入Ehcache的starter,而是导入技术坐标呢?其实springboot整合缓存技术做的是通用格式,不管你整合哪种缓存技术,只是实现变化了,操作方式一样。

步骤②:配置缓存技术实现使用Ehcache

  1. spring:
  2. cache:
  3. type: ehcache
  4. ehcache:
  5. config: classpath:ehcache.xml

ehcache.xml配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  4. updateCheck="false">
  5. <diskStore path="D:\ehcache" />
  6. <!--默认缓存策略 -->
  7. <!-- external:是否永久存在,设置为true则不会被清除,此时与timeout冲突,通常设置为false-->
  8. <!-- diskPersistent:是否启用磁盘持久化-->
  9. <!-- maxElementsInMemory:最大缓存数量-->
  10. <!-- overflowToDisk:超过最大缓存数量是否持久化到磁盘-->
  11. <!-- timeToIdleSeconds:最大不活动间隔,设置过长缓存容易溢出,设置过短无效果,可用于记录时效性数据,例如验证码-->
  12. <!-- timeToLiveSeconds:最大存活时间-->
  13. <!-- memoryStoreEvictionPolicy:缓存清除策略-->
  14. <defaultCache
  15. eternal="false"
  16. diskPersistent="false"
  17. maxElementsInMemory="1000"
  18. overflowToDisk="false"
  19. timeToIdleSeconds="60"
  20. timeToLiveSeconds="60"
  21. memoryStoreEvictionPolicy="LRU" />
  22. <cache
  23. name="myNameSpace"
  24. eternal="false"
  25. diskPersistent="false"
  26. maxElementsInMemory="1000"
  27. overflowToDisk="false"
  28. timeToIdleSeconds="10"
  29. timeToLiveSeconds="10"
  30. memoryStoreEvictionPolicy="LRU" />
  31. </ehcache>

注意前面的案例中,设置了数据保存的位置是myNameSpace

  1. @Service
  2. public class BookService {
  3. @Cacheable(value = "myNameSpace",key = "#id")
  4. public Book getById(Integer id) {
  5. return Book.builder()
  6. .id(id)
  7. .name("book_name" + id)
  8. .description("book_desc" + id)
  9. .build();
  10. }
  11. }

到这里springboot整合Ehcache就做完了,可以发现一点,原始代码没有任何修改,仅仅是加了一组配置就可以变更缓存供应商了,这也是springboot提供了统一的缓存操作接口的优势,变更实现并不影响原始代码的书写。

3.SpringBoot整合Redis缓存

推荐使用jetcache或者Redis本身的API进行缓存操作

步骤①:导入redis的坐标

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-data-redis</artifactId>
  8. </dependency>

步骤②:配置缓存技术实现使用redis  

  1. spring:
  2. redis:
  3. host: localhost
  4. port: 6379
  5. cache:
  6. type: redis

如果需要对redis作为缓存进行配置,注意不是对原始的redis进行配置,而是配置redis作为缓存使用相关的配置,隶属于spring.cache.redis节点下,注意不要写错位置了。

  1. spring:
  2. redis:
  3. host: localhost
  4. port: 6379
  5. cache:
  6. type: redis
  7. redis:
  8. use-key-prefix: false
  9. key-prefix: sms_
  10. cache-null-values: false
  11. time-to-live: 10s

4.SpringBoot整合Memecached

目前我们已经掌握了3种缓存解决方案的配置形式,分别是springboot内置缓存,ehcache和redis,这里研究一下国内比较流行的一款缓存memcached。

提前安装,启动Memecached。

memcached目前提供有三种客户端技术,分别是Memcached Client for Java、SpyMemcached和Xmemcached,其中性能指标各方面最好的客户端是Xmemcached,本次整合就使用这个作为客户端实现技术了。下面开始使用Xmemcached

步骤①:导入xmemcached的坐标

  1. <dependency>
  2. <groupId>com.googlecode.xmemcached</groupId>
  3. <artifactId>xmemcached</artifactId>
  4. <version>2.4.7</version>
  5. </dependency>

步骤②:配置memcached,制作memcached的配置类

 

  1. memcached:
  2. servers: localhost:11211
  3. poolSize: 10
  4. opTimeout: 3000
  1. @Component
  2. @ConfigurationProperties(prefix = "memcached")
  3. @Data
  4. public class XMemcachedProperties {
  5. private String servers;
  6. private int poolSize;
  7. private long opTimeout;
  8. }
  1. @Configuration
  2. public class XMemcachedConfig {
  3. @Autowired
  4. private XMemcachedProperties props;
  5. @Bean
  6. public MemcachedClient getMemcachedClient() throws IOException {
  7. MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder(props.getServers());
  8. memcachedClientBuilder.setConnectionPoolSize(props.getPoolSize());
  9. memcachedClientBuilder.setOpTimeout(props.getOpTimeout());
  10. MemcachedClient memcachedClient = memcachedClientBuilder.build();
  11. return memcachedClient;
  12. }
  13. }

步骤③:使用xmemcached客户端操作缓存,注入MemcachedClient对象

  1. @Service
  2. public class SMSCodeServiceImpl implements SMSCodeService {
  3. @Autowired
  4. private CodeUtils codeUtils;
  5. @Autowired
  6. private MemcachedClient memcachedClient;
  7. public String sendCodeToSMS(String tele) {
  8. String code = codeUtils.generator(tele);
  9. try {
  10. memcachedClient.set(tele,10,code);
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. return code;
  15. }
  16. public boolean checkCode(SMSCode smsCode) {
  17. String code = null;
  18. try {
  19. code = memcachedClient.get(smsCode.getTele()).toString();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. return smsCode.getCode().equals(code);
  24. }
  25. }

多个缓存方案

1.SpringBoot整合jetcache缓存

目前我们使用的缓存都是要么A要么B, springboot针对缓存的整合仅仅停留在用缓存上面,如果缓存自身不支持同时支持AB一起用,springboot也没办法,所以要想解决AB缓存一起用的问题,就必须找一款缓存能够支持AB两种缓存一起用,有这种缓存吗?还真有,阿里出品,jetcache。

jetcache并不是随便拿两个缓存都能拼到一起去的。目前jetcache支持的缓存方案本地缓存支持两种,远程缓存支持两种,分别如下:

  • 本地缓存(Local)

    • LinkedHashMap

    • Caffeine

  • 远程缓存(Remote)

    • Redis

    • Tair

纯远程方案

步骤①:导入springboot整合jetcache对应的坐标starter,当前坐标默认使用的远程方案是redis

  1. <dependency>
  2. <groupId>com.alicp.jetcache</groupId>
  3. <artifactId>jetcache-starter-redis</artifactId>
  4. <version>2.6.2</version>
  5. </dependency>

步骤②:远程方案基本配置

  1. jetcache:
  2. remote:
  3. default:
  4. type: redis
  5. host: localhost
  6. port: 6379
  7. poolConfig:
  8. maxTotal: 50

其中poolConfig是必配项,否则会报错

步骤③:启用缓存,在引导类上方标注注解@EnableCreateCacheAnnotation配置springboot程序中可以使用注解的形式创建缓存

  1. @SpringBootApplication
  2. //jetcache启用缓存的主开关
  3. @EnableCreateCacheAnnotation
  4. public class Springboot20JetCacheApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Springboot20JetCacheApplication.class, args);
  7. }
  8. }

步骤④:创建缓存对象Cache,并使用注解@CreateCache标记当前缓存的信息,然后使用Cache对象的API操作缓存,put写缓存,get读缓存。

  1. @Service
  2. public class SMSCodeServiceImpl implements SMSCodeService {
  3. @Autowired
  4. private CodeUtils codeUtils;
  5. @CreateCache(name="jetCache_",expire = 10,timeUnit = TimeUnit.SECONDS)
  6. private Cache<String ,String> jetCache;
  7. public String sendCodeToSMS(String tele) {
  8. String code = codeUtils.generator(tele);
  9. jetCache.put(tele,code);
  10. return code;
  11. }
  12. public boolean checkCode(SMSCode smsCode) {
  13. String code = jetCache.get(smsCode.getTele());
  14. return smsCode.getCode().equals(code);
  15. }
  16. }

纯本地方案

远程方案中,配置中使用remote表示远程,换成local就是本地,只不过类型不一样而已。

步骤①:导入springboot整合jetcache对应的坐标starter

  1. <dependency>
  2. <groupId>com.alicp.jetcache</groupId>
  3. <artifactId>jetcache-starter-redis</artifactId>
  4. <version>2.6.2</version>
  5. </dependency>

步骤②:本地缓存基本配置

  1. jetcache:
  2. local:
  3. default:
  4. type: linkedhashmap
  5. keyConvertor: fastjson

为了加速数据获取时key的匹配速度,jetcache要求指定key的类型转换器。简单说就是,如果你给了一个Object作为key的话,我先用key的类型转换器给转换成字符串,然后再保存。等到获取数据时,仍然是先使用给定的Object转换成字符串,然后根据字符串匹配。由于jetcache是阿里的技术,这里推荐key的类型转换器使用阿里的fastjson。

步骤③:启用缓存

  1. @SpringBootApplication
  2. //jetcache启用缓存的主开关
  3. @EnableCreateCacheAnnotation
  4. public class Springboot20JetCacheApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Springboot20JetCacheApplication.class, args);
  7. }
  8. }

步骤④:创建缓存对象Cache时,标注当前使用本地缓存

  1. @Service
  2. public class SMSCodeServiceImpl implements SMSCodeService {
  3. @CreateCache(name="jetCache_",expire = 1000,timeUnit = TimeUnit.SECONDS,cacheType = CacheType.LOCAL)
  4. private Cache<String ,String> jetCache;
  5. public String sendCodeToSMS(String tele) {
  6. String code = codeUtils.generator(tele);
  7. jetCache.put(tele,code);
  8. return code;
  9. }
  10. public boolean checkCode(SMSCode smsCode) {
  11. String code = jetCache.get(smsCode.getTele());
  12. return smsCode.getCode().equals(code);
  13. }
  14. }

cacheType控制当前缓存使用本地缓存还是远程缓存,配置cacheType=CacheType.LOCAL即使用本地缓存。

本地+远程方案

本地和远程方法都有了,两种方案一起使用如何配置呢?其实就是将两种配置合并到一起就可以了。

  1. jetcache:
  2. local:
  3. default:
  4. type: linkedhashmap
  5. keyConvertor: fastjson
  6. remote:
  7. default:
  8. type: redis
  9. host: localhost
  10. port: 6379
  11. poolConfig:
  12. maxTotal: 50
  13. sms:
  14. type: redis
  15. host: localhost
  16. port: 6379
  17. poolConfig:
  18. maxTotal: 50

在创建缓存的时候,配置cacheType为BOTH即则同时到本地缓存与远程缓存。

  1. @Service
  2. public class SMSCodeServiceImpl implements SMSCodeService {
  3. @CreateCache(name="jetCache_",expire = 1000,timeUnit = TimeUnit.SECONDS,cacheType = CacheType.BOTH)
  4. private Cache<String ,String> jetCache;
  5. }

cacheType如果不进行配置,默认值是REMOTE,即仅使用远程缓存方案。关于jetcache的配置,参考以下信息

属性默认值说明
jetcache.statIntervalMinutes0统计间隔,0表示不统计
jetcache.hiddenPackages自动生成name时,隐藏指定的包名前缀
jetcache.[local|remote].${area}.type缓存类型,本地支持linkedhashmap、caffeine,远程支持redis、tair
jetcache.[local|remote].${area}.keyConvertorkey转换器,当前仅支持fastjson
jetcache.[local|remote].${area}.valueEncoderjava仅remote类型的缓存需要指定,可选java和kryo
jetcache.[local|remote].${area}.valueDecoderjava仅remote类型的缓存需要指定,可选java和kryo
jetcache.[local|remote].${area}.limit100仅local类型的缓存需要指定,缓存实例最大元素数
jetcache.[local|remote].${area}.expireAfterWriteInMillis无穷大默认过期时间,毫秒单位
jetcache.local.${area}.expireAfterAccessInMillis0仅local类型的缓存有效,毫秒单位,最大不活动间隔

方法缓存

jetcache提供了方法缓存方案,只不过名称变更了而已。在对应的操作接口上方使用注解@Cached即可

 步骤①:导入springboot整合jetcache对应的坐标starte

  1. <dependency>
  2. <groupId>com.alicp.jetcache</groupId>
  3. <artifactId>jetcache-starter-redis</artifactId>
  4. <version>2.6.2</version>
  5. </dependency>

步骤②:配置缓存

  1. jetcache:
  2. local:
  3. default:
  4. type: linkedhashmap
  5. keyConvertor: fastjson
  6. remote:
  7. default:
  8. type: redis
  9. host: localhost
  10. port: 6379
  11. keyConvertor: fastjson
  12. valueEncode: java
  13. valueDecode: java
  14. poolConfig:
  15. maxTotal: 50
  16. sms:
  17. type: redis
  18. host: localhost
  19. port: 6379
  20. poolConfig:
  21. maxTotal: 50

由于redis缓存中不支持保存对象,因此需要对redis设置当Object类型数据进入到redis中时如何进行类型转换。需要配置keyConvertor表示key的类型转换方式,同时标注value的转换类型方式,值进入redis时是java类型,标注valueEncode为java,值从redis中读取时转换成java,标注valueDecode为java。

注意,为了实现Object类型的值进出redis,需要保障进出redis的Object类型的数据必须实现序列化接口。

  1. @Data
  2. public class Book implements Serializable {
  3. private Integer id;
  4. private String type;
  5. private String name;
  6. private String description;
  7. }

步骤③:启用缓存时开启方法缓存功能,并配置basePackages,说明在哪些包中开启方法缓存

  1. @SpringBootApplication
  2. //jetcache启用缓存的主开关
  3. @EnableCreateCacheAnnotation
  4. //开启方法注解缓存
  5. @EnableMethodCache(basePackages = "com.itheima")
  6. public class Springboot20JetCacheApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(Springboot20JetCacheApplication.class, args);
  9. }
  10. }

步骤④:使用注解@Cached标注当前方法使用缓存

  1. @Service
  2. public class BookServiceImpl implements BookService {
  3. @Autowired
  4. private BookDao bookDao;
  5. @Override
  6. @Cached(name="book_",key="#id",expire = 3600,cacheType = CacheType.REMOTE)
  7. public Book getById(Integer id) {
  8. return bookDao.selectById(id);
  9. }
  10. }

远程方案的数据同步

由于远程方案中redis保存的数据可以被多个客户端共享,这就存在了数据同步问题。jetcache提供了3个注解解决此问题,分别在更新、删除操作时同步缓存数据,和读取缓存时定时刷新数据

更新缓存

  1. @CacheUpdate(name="book_",key="#book.id",value="#book")
  2. public boolean update(Book book) {
  3. return bookDao.updateById(book) > 0;
  4. }

删除缓存

  1. @CacheInvalidate(name="book_",key = "#id")
  2. public boolean delete(Integer id) {
  3. return bookDao.deleteById(id) > 0;
  4. }

定时刷新缓存 

refresh如果时间太短,会对数据库性能有很大影响

  1. @Cached(name="book_",key="#id",expire = 3600,cacheType = CacheType.REMOTE)
  2. @CacheRefresh(refresh = 5)
  3. public Book getById(Integer id) {
  4. return bookDao.selectById(id);
  5. }

数据报表

jetcache还提供有简单的数据报表功能,帮助开发者快速查看缓存命中信息,只需要添加一个配置即可

  1. jetcache:
  2. statIntervalMinutes: 1

设置后,每1分钟在控制台输出缓存数据命中信息

  1. [DefaultExecutor] c.alicp.jetcache.support.StatInfoLogger : jetcache stat from 2022-02-28 09:32:15,892 to 2022-02-28 09:33:00,003
  2. cache | qps| rate| get| hit| fail| expire| avgLoadTime| maxLoadTime
  3. ---------+-------+-------+------+-------+-------+---------+--------------+--------------
  4. book_ | 0.66| 75.86%| 29| 22| 0| 0| 28.0| 188
  5. ---------+-------+-------+------+-------+-------+---------+--------------+--------------

总结

  1. jetcache是一个类似于springcache的缓存解决方案,自身不具有缓存功能,它提供有本地缓存与远程缓存多级共同使用的缓存解决方案

  2. jetcache提供的缓存解决方案受限于目前支持的方案,本地缓存支持两种,远程缓存支持两种

  3. 注意数据进入远程缓存时的类型转换问题

  4. jetcache提供方法缓存,并提供了对应的缓存更新与刷新功能

  5. jetcache提供有简单的缓存信息命中报表方便开发者即时监控缓存数据命中情况

SpringBoot整合j2cache缓存

jetcache可以在限定范围内构建多级缓存,但是灵活性不足,不能随意搭配缓存,本节介绍一种可以随意搭配缓存解决方案的缓存整合框架,j2cache。下面就来讲解如何使用这种缓存框架,以Ehcache与redis整合为例:

步骤①:导入j2cache、redis、ehcache坐标

  1. <dependency>
  2. <groupId>net.oschina.j2cache</groupId>
  3. <artifactId>j2cache-core</artifactId>
  4. <version>2.8.4-release</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>net.oschina.j2cache</groupId>
  8. <artifactId>j2cache-spring-boot2-starter</artifactId>
  9. <version>2.8.0-release</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>net.sf.ehcache</groupId>
  13. <artifactId>ehcache</artifactId>
  14. </dependency>

j2cache的starter中默认包含了redis坐标,官方推荐使用redis作为二级缓存,因此此处无需导入redis坐标

步骤②:配置一级与二级缓存,并配置一二级缓存间数据传递方式,配置书写在名称为j2cache.properties的文件中。如果使用ehcache还需要单独添加ehcache的配置文件

  1. # 1级缓存
  2. j2cache.L1.provider_class = ehcache
  3. ehcache.configXml = ehcache.xml
  4. # 2级缓存
  5. j2cache.L2.provider_class = net.oschina.j2cache.cache.support.redis.SpringRedisProvider
  6. j2cache.L2.config_section = redis
  7. redis.hosts = localhost:6379
  8. # 1级缓存中的数据如何到达二级缓存
  9. j2cache.broadcast = net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy

此处配置不能乱配置,需要参照官方给出的配置说明进行。例如1级供应商选择ehcache,供应商名称仅仅是一个ehcache,但是2级供应商选择redis时要写专用的Spring整合Redis的供应商类名SpringRedisProvider,而且这个名称并不是所有的redis包中能提供的,也不是spring包中提供的。因此配置j2cache必须参照官方文档配置,而且还要去找专用的整合包,导入对应坐标才可以使用。

一级与二级缓存最重要的一个配置就是两者之间的数据沟通方式,此类配置也不是随意配置的,并且不同的缓存解决方案提供的数据沟通方式差异化很大,需要查询官方文档进行设置。

步骤③:使用缓存

  1. @Service
  2. public class SMSCodeServiceImpl implements SMSCodeService {
  3. @Autowired
  4. private CodeUtils codeUtils;
  5. @Autowired
  6. private CacheChannel cacheChannel;
  7. public String sendCodeToSMS(String tele) {
  8. String code = codeUtils.generator(tele);
  9. cacheChannel.set("sms",tele,code);
  10. return code;
  11. }
  12. public boolean checkCode(SMSCode smsCode) {
  13. String code = cacheChannel.get("sms",smsCode.getTele()).asString();
  14. return smsCode.getCode().equals(code);
  15. }
  16. }

j2cache的使用和jetcache比较类似,但是无需开启使用的开关,直接定义缓存对象即可使用,缓存对象名CacheChannel。

j2cache的使用不复杂,配置是j2cache的核心,毕竟是一个整合型的缓存框架。缓存相关的配置过多,可以查阅j2cache-core核心包中的j2cache.properties文件中的说明。如下:

  1. #J2Cache configuration
  2. #########################################
  3. # Cache Broadcast Method
  4. # values:
  5. # jgroups -> use jgroups's multicast
  6. # redis -> use redis publish/subscribe mechanism (using jedis)
  7. # lettuce -> use redis publish/subscribe mechanism (using lettuce, Recommend)
  8. # rabbitmq -> use RabbitMQ publisher/consumer mechanism
  9. # rocketmq -> use RocketMQ publisher/consumer mechanism
  10. # none -> don't notify the other nodes in cluster
  11. # xx.xxxx.xxxx.Xxxxx your own cache broadcast policy classname that implement net.oschina.j2cache.cluster.ClusterPolicy
  12. #########################################
  13. j2cache.broadcast = redis
  14. # jgroups properties
  15. jgroups.channel.name = j2cache
  16. jgroups.configXml = /network.xml
  17. # RabbitMQ properties
  18. rabbitmq.exchange = j2cache
  19. rabbitmq.host = localhost
  20. rabbitmq.port = 5672
  21. rabbitmq.username = guest
  22. rabbitmq.password = guest
  23. # RocketMQ properties
  24. rocketmq.name = j2cache
  25. rocketmq.topic = j2cache
  26. # use ; to split multi hosts
  27. rocketmq.hosts = 127.0.0.1:9876
  28. #########################################
  29. # Level 1&2 provider
  30. # values:
  31. # none -> disable this level cache
  32. # ehcache -> use ehcache2 as level 1 cache
  33. # ehcache3 -> use ehcache3 as level 1 cache
  34. # caffeine -> use caffeine as level 1 cache(only in memory)
  35. # redis -> use redis as level 2 cache (using jedis)
  36. # lettuce -> use redis as level 2 cache (using lettuce)
  37. # readonly-redis -> use redis as level 2 cache ,but never write data to it. if use this provider, you must uncomment `j2cache.L2.config_section` to make the redis configurations available.
  38. # memcached -> use memcached as level 2 cache (xmemcached),
  39. # [classname] -> use custom provider
  40. #########################################
  41. j2cache.L1.provider_class = caffeine
  42. j2cache.L2.provider_class = redis
  43. # When L2 provider isn't `redis`, using `L2.config_section = redis` to read redis configurations
  44. # j2cache.L2.config_section = redis
  45. # Enable/Disable ttl in redis cache data (if disabled, the object in redis will never expire, default:true)
  46. # NOTICE: redis hash mode (redis.storage = hash) do not support this feature)
  47. j2cache.sync_ttl_to_redis = true
  48. # Whether to cache null objects by default (default false)
  49. j2cache.default_cache_null_object = true
  50. #########################################
  51. # Cache Serialization Provider
  52. # values:
  53. # fst -> using fast-serialization (recommend)
  54. # kryo -> using kryo serialization
  55. # json -> using fst's json serialization (testing)
  56. # fastjson -> using fastjson serialization (embed non-static class not support)
  57. # java -> java standard
  58. # fse -> using fse serialization
  59. # [classname implements Serializer]
  60. #########################################
  61. j2cache.serialization = json
  62. #json.map.person = net.oschina.j2cache.demo.Person
  63. #########################################
  64. # Ehcache configuration
  65. #########################################
  66. # ehcache.configXml = /ehcache.xml
  67. # ehcache3.configXml = /ehcache3.xml
  68. # ehcache3.defaultHeapSize = 1000
  69. #########################################
  70. # Caffeine configuration
  71. # caffeine.region.[name] = size, xxxx[s|m|h|d]
  72. #
  73. #########################################
  74. caffeine.properties = /caffeine.properties
  75. #########################################
  76. # Redis connection configuration
  77. #########################################
  78. #########################################
  79. # Redis Cluster Mode
  80. #
  81. # single -> single redis server
  82. # sentinel -> master-slaves servers
  83. # cluster -> cluster servers (数据库配置无效,使用 database = 0
  84. # sharded -> sharded servers (密码、数据库必须在 hosts 中指定,且连接池配置无效 ; redis://user:password@127.0.0.1:6379/0
  85. #
  86. #########################################
  87. redis.mode = single
  88. #redis storage mode (generic|hash)
  89. redis.storage = generic
  90. ## redis pub/sub channel name
  91. redis.channel = j2cache
  92. ## redis pub/sub server (using redis.hosts when empty)
  93. redis.channel.host =
  94. #cluster name just for sharded
  95. redis.cluster_name = j2cache
  96. ## redis cache namespace optional, default[empty]
  97. redis.namespace =
  98. ## redis command scan parameter count, default[1000]
  99. #redis.scanCount = 1000
  100. ## connection
  101. # Separate multiple redis nodes with commas, such as 192.168.0.10:6379,192.168.0.11:6379,192.168.0.12:6379
  102. redis.hosts = 127.0.0.1:6379
  103. redis.timeout = 2000
  104. redis.password =
  105. redis.database = 0
  106. redis.ssl = false
  107. ## redis pool properties
  108. redis.maxTotal = 100
  109. redis.maxIdle = 10
  110. redis.maxWaitMillis = 5000
  111. redis.minEvictableIdleTimeMillis = 60000
  112. redis.minIdle = 1
  113. redis.numTestsPerEvictionRun = 10
  114. redis.lifo = false
  115. redis.softMinEvictableIdleTimeMillis = 10
  116. redis.testOnBorrow = true
  117. redis.testOnReturn = false
  118. redis.testWhileIdle = true
  119. redis.timeBetweenEvictionRunsMillis = 300000
  120. redis.blockWhenExhausted = false
  121. redis.jmxEnabled = false
  122. #########################################
  123. # Lettuce scheme
  124. #
  125. # redis -> single redis server
  126. # rediss -> single redis server with ssl
  127. # redis-sentinel -> redis sentinel
  128. # redis-cluster -> cluster servers
  129. #
  130. #########################################
  131. #########################################
  132. # Lettuce Mode
  133. #
  134. # single -> single redis server
  135. # sentinel -> master-slaves servers
  136. # cluster -> cluster servers (数据库配置无效,使用 database = 0
  137. # sharded -> sharded servers (密码、数据库必须在 hosts 中指定,且连接池配置无效 ; redis://user:password@127.0.0.1:6379/0
  138. #
  139. #########################################
  140. ## redis command scan parameter count, default[1000]
  141. #lettuce.scanCount = 1000
  142. lettuce.mode = single
  143. lettuce.namespace =
  144. lettuce.storage = hash
  145. lettuce.channel = j2cache
  146. lettuce.scheme = redis
  147. lettuce.hosts = 127.0.0.1:6379
  148. lettuce.password =
  149. lettuce.database = 0
  150. lettuce.sentinelMasterId =
  151. lettuce.maxTotal = 100
  152. lettuce.maxIdle = 10
  153. lettuce.minIdle = 10
  154. # timeout in milliseconds
  155. lettuce.timeout = 10000
  156. # redis cluster topology refresh interval in milliseconds
  157. lettuce.clusterTopologyRefresh = 3000
  158. #########################################
  159. # memcached server configurations
  160. # refer to https://gitee.com/mirrors/XMemcached
  161. #########################################
  162. memcached.servers = 127.0.0.1:11211
  163. memcached.username =
  164. memcached.password =
  165. memcached.connectionPoolSize = 10
  166. memcached.connectTimeout = 1000
  167. memcached.failureMode = false
  168. memcached.healSessionInterval = 1000
  169. memcached.maxQueuedNoReplyOperations = 100
  170. memcached.opTimeout = 100
  171. memcached.sanitizeKeys = false

总结

  1. j2cache是一个缓存框架,自身不具有缓存功能,它提供多种缓存整合在一起使用的方案

  2. j2cache需要通过复杂的配置设置各级缓存,以及缓存之间数据交换的方式

  3. j2cache操作接口通过CacheChannel实现

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/164596
推荐阅读
相关标签
  

闽ICP备14008679号