springboot integrated J2Cache

Keywords: PHP Redis Ehcache xml Java

J2Cache is a two-level caching framework currently being used by OSChina. First level cache usage Ehcache , second level cache uses Redis  . Because a large number of cache reads will cause L2 network to become the bottleneck of the whole system, the goal of L1 is to reduce the number of L2 reads. This caching framework is mainly used in cluster environment. Single machine can also be used to avoid the loss of Ehcache cache data caused by application restart.

j2Cache provides the integration of springboot.

The integration method is as follows:

1. Introduce pom.xml

 <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.7-release</version>
        </dependency>

2. Add configuration file

ehcache3.xml

<!-- for ehcache 3.x -->
<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

    <!-- Don't remote default cache configuration -->
    <cache-template name="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.io.Serializable</value-type>
        <expiry>
            <ttl unit="seconds">1800</ttl>
        </expiry>
        <resources>
            <heap>1000</heap>
            <offheap unit="MB">100</offheap>
        </resources>

    </cache-template>
    <!--
    <persistence directory="${ecache.path}"/>
    -->
    <cache alias="default" uses-template="default"/>

</config>

j2cache.properties

#J2Cache configuration


#########################################
# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism
#########################################

j2cache.broadcast = net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy

#Channel name of multicast
jgroups.channel.name = j2cache

#########################################
# 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(hashs) as level 2 cache
# [classname] -> use custom provider
#########################################

j2cache.L1.provider_class = ehcache3
j2cache.L2.provider_class = net.oschina.j2cache.cache.support.redis.SpringRedisProvider
#j2cache.L2.provider_class = redis
j2cache.L2.config_section = redis
#j2cache.L2.provider_class = redis

#########################################
# Cache Serialization Provider
# values:
# fst -> fast-serialization
# kyro -> kyro
# java -> java standard
# [classname implements Serializer]
#########################################

j2cache.serialization = fst

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

#ehcache.name=
#ehcache.configXml=/ehcache.xml
ehcache3.configXml = /config/ehcache3.xml

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

caffeine.region.default = 1000, 1h 

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

#########################################
# Redis Cluster Mode
#
# single -> single redis server
# sentinel -> master-slaves servers
# cluster -> cluster servers (Invalid database configuration, using database = 0)
# sharded -> sharded servers  (Password and database must be in hosts Specified in and the connection pool configuration is invalid ; redis://user:password@127.0.0.1:6379/0)
#
#########################################

#redis.mode = sentinel
redis.mode = single
#cluster name just for sharded
redis.cluster_name = mymaster

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

## connection
#redis.hosts = 127.0.0.1:26378,127.0.0.1:26379,127.0.0.1:26380
redis.hosts = 192.168.1.100:6379
redis.timeout = 2000
redis.password =
#redis.database = 0

## redis pub/sub channel name
redis.channel = j2cache

## redis pool properties
redis.maxTotal = -1
redis.maxIdle = 2000
redis.maxWaitMillis = 100
redis.minEvictableIdleTimeMillis = 864000000
redis.minIdle = 1000
redis.numTestsPerEvictionRun = 10
redis.lifo = false
redis.softMinEvictableIdleTimeMillis = 10
redis.testOnBorrow = true
redis.testOnReturn = false
redis.testWhileIdle = false
redis.timeBetweenEvictionRunsMillis = 300000
redis.blockWhenExhausted = true
redis.mode = single mode supports single,sentinel,cluster
redis.hosts: the host to be configured as redis

3. Inject CacheChannel

@Component(value = "iCache")
public class J2cacheImpl implements ICache {

    private String region="rx";

    @Autowired
    private CacheChannel cacheChannel;

4. Modify the configuration file

application.properties

j2cache:
  config-location: classpath:/config/j2cache.properties
  open-spring-cache: true
  allow-null-values: true
  cache-clean-mode: active
  redis-client: jedis
  l2-cache-open: false

We can turn off the second level cache, that is, only local cache.

Posted by yuan on Sun, 03 Nov 2019 11:52:51 -0800