A good second-level cache framework J2Cache in China

Look at the picture and talk

image


Caching technology has become ubiquitous in software development. Today, I will share a domestic open source very good caching framework J2Cache. J2Cache is the two-level caching framework currently being used by OSChina (requires at least Java 8). The first level cache uses memory (supports Ehcache 2.x, Ehcache 3.x and Caffeine at the same time), and the second level cache uses Redis (recommended)/Memcached. Since a large number of cache reads will cause the L2 network to become the bottleneck of the entire system, the goal of L1 is to reduce the number of reads to L2. The caching framework is mainly used in a cluster environment. It can also be used on a stand-alone machine to avoid the impact on the back-end business after the cold restart of the cache caused by application restart.


J2Cache uses Caffeine as the first-level cache by default and Redis as the second-level cache. You can also choose Ehcache2 and Ehcache3 as the first level cache.

Warehouse Address:

https://gitee.com/ld/J2Cache

1. Preparation

1. Install Redis

2. Create a new Java project based on Maven


2. Quote Maven

<dependency>
  <groupId>net.oschina.j2cache</groupId>
  <artifactId>j2cache-core</artifactId>
  <version>xxxxx</version>
</dependency>


3. Prepare to configure

Copy j2cache.properties and caffeine.properties to the source directory of your project, and make sure that these files will be compiled into the project's classpath. If you choose ehcache as the first-level cache, you need to copy ehcache.xml or ehcache3.xml to the source directory (the latter corresponds to the Ehcache 3.x version), and the templates of these configuration files can be from  

https://gitee.com/ld/J2Cache/tree/master/core/resources 

Get it here.

Use your favorite text editor to open j2cache.properties and find the redis.hosts item, and change its information to the address and port where your Redis server is located. It is recommended that the cache size and effective time be set in advance before using the cache. Use a text editor to open caffeine.properties to configure the cache. For the configuration method, please refer to the comments in the file.

E.g:

default = 1000,30m #定义缓存名default,对象大小1000,缓存数据有效时间30分钟。 可以定义多个不同名称的缓存。


Four. Writing code

Test.java

public static void main(String[] args{

    CacheChannel cache = J2Cache.getChannel();
   //缓存操作
    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();

}

Compile and run to view the results. For more usage, please refer to the CacheChannel.java interface method.

Please note that the cache.close() method only needs to be called when the program exits.


Five. Dynamically build a J2Cache instance

J2CacheConfig config = new J2CacheConfig();//填充 config 变量所需的配置信息
J2CacheBuilder builder = J2CacheBuilder.init(config);
CacheChannel channel = builder.getChannel();//进行缓存的操作
channel.close();

J2Cache provides related plug-ins that integrate Spring, Hibernate, and Mybatis, and you can view more detailed usage methods:

https://gitee.com/ld/J2Cache/blob/master/README.md

The provided plug-in modules can be found in the following directories of the warehouse:

https://gitee.com/ld/J2Cache/tree/master/modules


Guess you like

Origin blog.51cto.com/15127574/2667874