J2Cache, a two-tiered Java caching framework based on memory and Redis

J2Cache, a two-tiered Java caching framework based on memory and Redis

2022-09-05 0 1,249
Resource Number 38213 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation
In this issue, we recommend J2Cache, a two-level Java caching framework based on memory and Redis.Project Introduction

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, a two-tiered Java caching framework based on memory and Redis插图

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>

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code J2Cache, a two-tiered Java caching framework based on memory and Redis https://ictcoder.com/kyym/j2cache-a-two-tiered-java-caching-framework-based-on-memory-and-redis.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service