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,661
Resource Number 38213 Last Updated 2025-02-24
¥ 0HKD 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/j2cache-a-two-tiered-java-caching-framework-based-on-memory-and-redis/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services