缓存

目录

  • 描述
  • 配置
  • 使用
  • EhCache
  • Redis
  • EhRedis
  • J2Cache
  • NoneCache

描述

Jboot 定位为高性能的微服务框架,然而高性能离不开合理的缓存设计。Jboot 内置了丰富的框架支持,比如:

  • ehcache
  • redis
  • ehredis
  • j2cache

配置

默认情况下,用户无需做任何配置就可以使用 Jboot 的缓存功能,默认情况下 Jboot 是使用 Ehcache 作为 Jboot 的缓存方案。

如果需要修改把 Ehcahce 方案修改为使用 redis ,则可以添加如下的配置:

  1. jboot.cache.type = redis

在使用 redis 作为默认的缓存方案时,需要配置上 redis 的相关信息,例如:

  1. jboot.cache.redis.host = 127.0.0.1
  2. jboot.cache.redis.port = 3306
  3. jboot.cache.redis.password
  4. jboot.cache.redis.database
  5. jboot.cache.redis.timeout
  6. jboot.cache.redis.clientName
  7. jboot.cache.redis.testOnCreate
  8. jboot.cache.redis.testOnBorrow
  9. jboot.cache.redis.testOnReturn
  10. jboot.cache.redis.testWhileIdle
  11. jboot.cache.redis.minEvictableIdleTimeMillis
  12. jboot.cache.redis.timeBetweenEvictionRunsMillis
  13. jboot.cache.redis.numTestsPerEvictionRun
  14. jboot.cache.redis.maxAttempts
  15. jboot.cache.redis.maxTotal
  16. jboot.cache.redis.maxIdle
  17. jboot.cache.redis.maxWaitMillis
  18. jboot.cache.redis.serializer

当,以上未配置的时候,Jboot 自动会去寻找 redis 模块来使用,redis 的配置为:

  1. jboot.redis.host
  2. jboot.redis.port
  3. jboot.redis.password
  4. jboot.redis.database
  5. jboot.redis.timeout
  6. jboot.redis.clientName
  7. jboot.redis.testOnCreate
  8. jboot.redis.testOnBorrow
  9. jboot.redis.testOnReturn
  10. jboot.redis.testWhileIdle
  11. jboot.redis.minEvictableIdleTimeMillis
  12. jboot.redis.timeBetweenEvictionRunsMillis
  13. jboot.redis.numTestsPerEvictionRun
  14. jboot.redis.maxAttempts
  15. jboot.redis.maxTotal
  16. jboot.redis.maxIdle
  17. jboot.redis.maxWaitMillis
  18. jboot.redis.serializer

以下是 JbootRedisCacheImpl 的部分代码:

  1. public class JbootRedisCacheImpl extends JbootCacheBase {
  2. private JbootRedis redis;
  3. public JbootRedisCacheImpl() {
  4. JbootRedisCacheConfig redisConfig = Jboot.config(JbootRedisCacheConfig.class);
  5. //优先使用 jboot.cache.redis 的配置
  6. if (redisConfig.isConfigOk()) {
  7. redis = JbootRedisManager.me().getRedis(redisConfig);
  8. }
  9. // 当 jboot.cache.redis 配置不存在时,
  10. // 使用 jboot.redis 的配置
  11. else {
  12. redis = Jboot.getRedis();
  13. }
  14. if (redis == null) {
  15. throw new JbootIllegalConfigException("can not get redis, please check your jboot.properties , please correct config jboot.cache.redis.host or jboot.redis.host ");
  16. }
  17. }
  18. //....
  19. }

使用缓存

显式代码调用

通过注解使用缓存