J2Cache is a two-level caching framework currently used by OSChina (requires at least Java 8). The first-level cache uses memory (both Ehcache 2.x, Ehcache 3.x, and Caffeine are supported), and the second-level cache uses Redis (recommended)/Memcached. Since a large number of cached reads can cause L2’s network to become a bottleneck for the entire system, the goal of L1 is to reduce the number of reads to L2. This caching framework is primarily used in clustered environments. It can also be used on a single node to avoid the impact on backend services after a cache cold start caused by application restart.
J2Cache’s two-tier caching structure:
L1: In-process caching (caffeineehcache)
L2: Redis/Memcached centralized cache
Data reads
Read order -> L1 -> L2 -> DB
Read the latest data from the database L1, update L1 -> L2 in turn, send a broadcast to clear a certain cache information, receive the broadcast (manually clear the cache & the first-level cache is automatically invalidated), and clear the specified cache information from L1
J2Cache configuration
The configuration file is located in the core/resources directory and contains three files:
j2cache.properties J2Cache core configuration file, configurable two-level cache, Redis server, connection pool, and cache broadcast
caffeine.properties If Caffeine is selected for the first-level cache, then this file is used to configure the cache information
ehcache.xml the configuration file of Ehcache, please refer to the Ehcache documentation for configuration instructions
ehcache3.xml the configuration file of Ehcache3, please refer to the Ehcache documentation for configuration instructions
network.xml JGroups network configuration, this file is required if you use JGroups multicast, and generally does not need to modify it
The actual use process requires copying the required configuration file to the application classpath, such as the WEB-INF/classes directory.
Please refer to core/pom.xml for the jar package required by the J2Cache runtime
Test Method:
Install Redis
git clone https://gitee.com/ld/J2Cache
Modify the core/resource/j2cache.properties configuration to use the installed Redis server
Run mvn package -DskipTests=true on the command line to compile the project
Open multiple command-line windows and run runtest.sh at the same time
Enter help at the > prompt to review the command, and test it
How to use:
By default, J2Cache uses Caffeine as the first-level cache and Redis as the second-level cache. You can also choose Ehcache2 and Ehcache3 as the first-level caches.
Preparation
Install Redis
Create a new Maven-based Java project
1. Quote Maven
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>xxxxx</version>
</dependency>
2. Prepare the configuration
Copy j2cache.properties and caffeine.properties to your project’s source directory and make sure these files are compiled into your project’s classpath. If you choose ehcache as the L1 cache, you will need to copy ehcache.xml or ehcache3.xml to the source directory (the latter is the Ehcache 3.x version), and the templates for these configuration files can be obtained from https://gitee.com/ld/J2Cache/tree/master/core/resources.
Open j2cache.properties in your favorite text editor and find the redis.hosts entry to change its information to the address and port where your Redis server is located.
We recommend that the cache size and validity period should be set in advance before use, and use a text editor to open caffeine.properties for cache configuration.
For example: default = 1000, 30m #Define the cache name default, object size 1000, cached data valid for 30 minutes. You can define multiple caches with different names.
3. Write code
Test.java
public static void main(String[] args) {
CacheChannel cache = J2Cache.getChannel();
Caching operations
cache.set(“default”, “1”, “Hello J2Cache”);
System.out.println(cache.get(“default”, “1”));
cache.evict(“default”, “1”);
System.out.println(cache.get(“default”, “1”));
cache.close();
}
4. Dynamically build a J2Cache instance
J2CacheConfig config = new J2CacheConfig();
Populate the configuration information required for the config variable
J2CacheBuilder builder = J2CacheBuilder.init(config);
CacheChannel channel = builder.getChannel();
Caching operations
channel.close();
frequently asked questions
How to use JGroups multicast (not available in cloud hosts)
First modify the j2cache.broadcast value in j2cache.properties to jgroups, and then ingest it in maven
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>3.6.13.Final</version>
</dependency>
How to use ehcache as a first-level cache
First, modify j2cache in j2cache.properties. L1.provider_class ehcache or ehcache3, then copy ehcache.xml or ehcache3.xml to the classpath, and configure the cache, you need to introduce support for ehcache in your project:
<dependency><!– Ehcache 2.x //–>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
<dependency><!– Ehcache 3.x //–>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>
How to use memcached as a second-level cache
First, modify j2cache in j2cache.properties. L2.provider_class for memcached, and then configure memcached.xxx-related information in j2cache.properties.
Support for memcached needs to be introduced in the project:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.5</version>
</dependency>