Java project integrated J2Cache (two-level Java caching framework based on memory and Redis)

I. Overview

SpringCache itself is an abstract implementation of a caching system and has no specific caching capabilities. To use SpringCache, you need to cooperate with a specific caching implementation.

Even so, SpringCache is the basis of all the caching structures supported by Spring, and all the use of caching is ultimately attributed to SpringCache. Then, if you want to use SpringCache, you need to study it carefully.

Two, cache annotations

The implementation of the SpringCache cache function is accomplished by the following annotations.

@EnableCaching: enable the cache function
@Cacheable: define the cache to trigger the cache
@CachePut: define the update cache, trigger the cache update
@CacheEvict: define the clear cache, trigger the cache clear
@Caching: combine to define multiple cache functions
@CacheConfig: define the public Settings, located above the class

Three, combined with the cache framework

  1. springCache: abstract implementation of caching system
  2. caffeine: high-performance process cache
  3. j2cache: Two-level Java caching framework (first level based on memory, second level based on Redis)

Fourth, the core code

    @Override
    public CacheManager cacheManager() {
    
    
        // 引入配置
        Properties properties = load("/config/j2cache.properties");
        transfer(properties);
        J2CacheConfig config = J2CacheConfig.initFromConfig(properties);
        // 生成 J2CacheBuilder
        J2CacheBuilder j2CacheBuilder = J2CacheBuilder.init(config);
        // 构建适配器
        return new J2CacheSpringCacheManageAdapter(j2CacheBuilder, true);
    }

	@Bean("keyGenerator")
    public KeyGenerator cacheKeyGenerator(){
    
    //缓存key生成者cd
        return new CacheKeyGenerator();
    }

Guess you like

Origin blog.csdn.net/a251628111/article/details/109262082