1. 程式人生 > >spring boot 2 整合 j2Cache

spring boot 2 整合 j2Cache

maven 配置:

<dependency>
    <groupId>net.oschina.j2cache</groupId>
    <artifactId>j2cache-spring-boot2-starter</artifactId>
    <version>2.7.6-release</version>
</dependency>
<dependency>
    <groupId>net.oschina.j2cache</groupId>
    <artifactId>j2cache-core</artifactId>
    <version>2.7.8-release</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1. slf4j-simple 這個要踢除。不然logback 會出錯
2. 因為我專案有引入相同的依賴,如:fastjson。所以多餘的我也踢除了

 

application.yml 配置:

spring:
  profiles:
    active: dev
  cache:
    type: none  #現在改為none,使用j2cache 去配置
j2cache:
  config-location: classpath:/config/j2cache-${spring.profiles.active}.properties
  open-spring-cache: true
  redis-client: lettuce

spring.cache = none  //不使用快取。讓 j2Cache 去配置
# spring.cache.cache-names = default, xx, xx,xx   //這裡可以指定快取名
spring.redis // 這個不需要配置

j2cache.config-location: xxxx   //j2cache 配置檔案
j2cache.open-spring.cache: true   //開啟並配置spring CacheManger
j2cache.redis-client: lettuce     //預設使用jredis 

 

/config/j2cache-dev.properties  配置:   
1. 一級快取為 caffeine 二級快取為: redis
2.  不使用叢集 j2cache.broadcast = none   單節點使用

#J2Cache configuration

#########################################
# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism (using jedis)
# lettuce -> use redis publish/subscribe mechanism (using lettuce, Recommend)
# rabbitmq -> use RabbitMQ publisher/consumer mechanism
# rocketmq -> use RocketMQ publisher/consumer mechanism
# none -> don't notify the other nodes in cluster
# xx.xxxx.xxxx.Xxxxx your own cache broadcast policy classname that implement net.oschina.j2cache.cluster.ClusterPolicy
#########################################

j2cache.broadcast = none 

# jgroups properties
#jgroups.channel.name = j2cache
#jgroups.configXml = /network.xml

# RabbitMQ properties
#rabbitmq.exchange = j2cache
#rabbitmq.host = 7.11.16.18
#rabbitmq.port = 5672
#rabbitmq.username = xx
#rabbitmq.password = xx

# RocketMQ properties
#rocketmq.name = j2cache
#rocketmq.topic = j2cache
# use ; to split multi hosts
#rocketmq.hosts = 127.0.0.1:9876

#########################################
# Level 1&2 provider
# values:
# none -> disable this level cache
# ehcache -> use ehcache2 as level 1 cache
# ehcache3 -> use ehcache3 as level 1 cache
# caffeine -> use caffeine as level 1 cache(only in memory)
# redis -> use redis as level 2 cache (using jedis)
# lettuce -> use redis as level 2 cache (using lettuce)
# 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.
# memcached -> use memcached as level 2 cache (xmemcached),
# [classname] -> use custom provider
#########################################

j2cache.L1.provider_class = caffeine
j2cache.L2.provider_class = lettuce

# When L2 provider isn't `redis`, using `L2.config_section = redis` to read redis configurations
# j2cache.L2.config_section = redis

# Enable/Disable ttl in redis cache data (if disabled, the object in redis will never expire, default:true)
# NOTICE: redis hash mode (redis.storage = hash) do not support this feature)
j2cache.sync_ttl_to_redis = true

# Whether to cache null objects by default (default false)
j2cache.default_cache_null_object = false

#########################################
# Cache Serialization Provider
# values:
# fst -> using fast-serialization (recommend)
# kyro -> using kyro serialization
# json -> using fst's json serialization (testing)
# fastjson -> using fastjson serialization (embed non-static class not support)
# java -> java standard
# [classname implements Serializer]
#########################################

j2cache.serialization = fastjson
#json.map.person = net.oschina.j2cache.demo.Person

#########################################
# Ehcache configuration
#########################################

# ehcache.configXml = /ehcache.xml

# ehcache3.configXml = /ehcache3.xml
# ehcache3.defaultHeapSize = 1000

#########################################
# Caffeine configuration
# caffeine.region.[name] = size, xxxx[s|m|h|d]
#
#########################################
caffeine.properties = /config/caffeine.properties

#########################################
# Lettuce scheme
#
# redis -> single redis server
# rediss -> single redis server with ssl
# redis-sentinel -> redis sentinel
# redis-cluster -> cluster servers
#
#########################################

lettuce.namespace =
lettuce.mode = single
lettuce.cluster_name = j2cache
lettuce.storage = generic
lettuce.channel = j2cache
lettuce.channel.host =
lettuce.scheme = redis
lettuce.hosts = 47.10.15.18:6379
lettuce.password = xxx
lettuce.database = 3
lettuce.sentinelMasterId =
lettuce.maxTotal = 100
lettuce.maxIdle = 10
lettuce.numTestsPerEvictionRun = 10
lettuce.softMinEvictableIdleTimeMillis = 10
lettuce.minIdle = 10
lettuce.maxWaitMillis = 5000
lettuce.lifo = false
lettuce.minEvictableIdleTimeMillis = 60000
# timeout in milliseconds
lettuce.timeout = 10000
lettuce.testOnBorrow = true
lettuce.testOnReturn = false
lettuce.testWhileIdle = true
lettuce.timeBetweenEvictionRunsMillis = 300000
lettuce.blockWhenExhausted = false


#########################################
# Redis connection configuration
#########################################

#########################################
# Redis Cluster Mode
#
# single -> single redis server
# sentinel -> master-slaves servers
# cluster -> cluster servers (資料庫配置無效,使用 database = 0)
# sharded -> sharded servers  (密碼、資料庫必須在 hosts 中指定,且連線池配置無效 ; redis://user:[email protected]:6379/0)
#
#########################################

#redis.mode = single

#redis storage mode (generic|hash)
#redis.storage = generic

## redis pub/sub channel name
#redis.channel = j2cache
## redis pub/sub server (using redis.hosts when empty)
#redis.channel.host =

#cluster name just for sharded
#redis.cluster_name = j2cache

## redis cache namespace optional, default[empty]
#redis.namespace =

## connection
# Separate multiple redis nodes with commas, such as 192.168.0.10:6379,192.168.0.11:6379,192.168.0.12:6379

#redis.hosts = 47.11.16.98:6379
#redis.timeout = 2000
#redis.password = xxxx
#redis.database = 3

## redis pool properties
#redis.maxTotal = 100
#redis.maxIdle = 10
#redis.maxWaitMillis = 5000
#redis.minEvictableIdleTimeMillis = 60000
#redis.minIdle = 1
#redis.numTestsPerEvictionRun = 10
#redis.lifo = false
#redis.softMinEvictableIdleTimeMillis = 10
#redis.testOnBorrow = true
#redis.testOnReturn = false
#redis.testWhileIdle = true
#redis.timeBetweenEvictionRunsMillis = 300000
#redis.blockWhenExhausted = false
#redis.jmxEnabled = false


#########################################
# memcached server configurations
# refer to https://gitee.com/mirrors/XMemcached
#########################################

#memcached.servers = 127.0.0.1:11211
#memcached.username =
#memcached.password =
#memcached.connectionPoolSize = 10
#memcached.connectTimeout = 1000
#memcached.failureMode = false
#memcached.healSessionInterval = 1000
#memcached.maxQueuedNoReplyOperations = 100
#memcached.opTimeout = 100
#memcached.sanitizeKeys = false

 

/config/caffeine.properties 配置:

 

#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################
default=1000, 30m
testCache=10000, 6s