springboot 整合 j2cache

J2Cache —— 基于内存和 Redis 的两级 Java 缓存框架

J2Cache 是 OSChina 目前正在使用的两级缓存框架(要求至少 Java 8)。第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached 。 由于大量的缓存读取会导致 L2 的网络成为整个系统的瓶颈,因此 L1 的目标是降低对 L2 的读取次数。 该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的缓存冷启动后对后端业务的冲击。
J2Cache gitee官网

j2cache依赖

本人使用的springboot版本是2.2.9.RELEASE,j2cache一级缓存使用Ehcache 2.x,二级缓存使用redis
j2cache依赖【springboot相关无版本号是使用了父工程管理】

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.4</version>
</dependency>
<!-- j2cache -->
<dependency>
    <groupId>net.oschina.j2cache</groupId>
    <artifactId>j2cache-core</artifactId>
    <version>2.7.7-release</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>net.oschina.j2cache</groupId>
    <artifactId>j2cache-spring-boot2-starter</artifactId>
    <version>2.7.6-release</version>
</dependency>

注意:需要把j2cache-core中的slf4j移除,因为springboot中已集成了slf4j
在这里插入图片描述

配置文件

ehcache.xml

<!-- for ehcache 2.x -->
<ehcache updateCheck="false" dynamicConfig="false">

    <diskStore path="java.io.tmpdir"/>

    <!--    <cacheManagerEventListenerFactory class="" properties=""/>-->

    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="1800"
            overflowToDisk="true">
    </defaultCache>

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <cache name="j2cache"
           maxElementsInMemory="5000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           overflowToDisk="false"
    >
    </cache>

</ehcache>

j2cache.properties lettuce

#J2Cache configuration


#########################################
# Cache Broadcast Method 缓存广播方法
# values:
# jgroups -> use jgroups's multicast 使用jgroups的多播
# redis -> use redis publish/subscribe mechanism (using jedis) 使用redis的发布/订阅机制(使用jedis)
# lettuce -> use redis publish/subscribe mechanism (using lettuce, Recommend) 使用redis的发布/订阅机制(使用lettuce,推荐)
# rabbitmq -> use RabbitMQ publisher/consumer mechanism 使用RabbitMQ发布者/消费者机制
# rocketmq -> use RocketMQ publisher/consumer mechanism 使用RocketMQ发布者/消费者机制
# none -> don't notify the other nodes in cluster 不要通知集群中的其他节点
# xx.xxxx.xxxx.Xxxxx your own cache broadcast policy classname that implement net.oschina.j2cache.cluster.ClusterPolicy 您自己的实现net.oschina.j2cache.cluster.ClusterPolicy的缓存广播策略类名
#########################################
#广播策略
j2cache.broadcast = lettuce

#组播配置
# jgroups properties
jgroups.channel.name = j2cache
#jgroups.configXml = /network.xml

# RabbitMQ properties
#rabbitmq.exchange = j2cache
#rabbitmq.host = localhost
#rabbitmq.port = 5672
#rabbitmq.username = guest
#rabbitmq.password = guest

# RocketMQ properties
#rocketmq.name = j2cache
#rocketmq.topic = j2cache
# use ; to split multi hosts
#rocketmq.hosts = 127.0.0.1:9876

#########################################
# Level 1&2 provider 一级与二级提供者
# values:
# none -> disable this level cache 禁用此级别的缓存
# ehcache -> use ehcache2 as level 1 cache 使用ehcache2作为1级缓存
# ehcache3 -> use ehcache3 as level 1 cache 使用ehcache3作为1级缓存
# caffeine -> use caffeine as level 1 cache(only in memory) 使用caffeine作为1级缓存(仅在内存中)
# redis -> use redis as level 2 cache (using jedis) 将redis用作2级缓存(使用jedis)
# lettuce -> use redis as level 2 cache (using lettuce) 使用redis作为2级缓存(使用 lettuce)
# readonly-redis -> use redis as level 2 cache ,but never write data to it. if use this provider, you must uncomment `j2cache.L2.config_section` to make the redis configurations available. 将redis用作2级缓存,但不要向其写入数据。如果使用此提供程序,则必须取消注释“ j2cache.L2.config_section”以使redis配置可用。
# memcached -> use memcached as level 2 cache (xmemcached), 使用memcached作为2级缓存(xmemcached)
# [classname] -> use custom provider 使用自定义提供程序
#########################################

# 一级缓存策略为ehcache
j2cache.L1.provider_class = ehcache
# 二级缓存策略为redis->lettuce
j2cache.L2.provider_class = lettuce

# When L2 provider isn't `redis`, using `L2.config_section = redis` to read redis configurations 当L2提供者不是`redis`时,使用`L2.config_section = redis`读取redis配置
# j2cache.L2.config_section = redis

# Enable/Disable ttl in redis cache data (if disabled, the object in redis will never expire, default:true) 在redis缓存数据中启用/禁用ttl(如果禁用,则redis中的对象将永不过期,默认值:true)
# NOTICE: redis hash mode (redis.storage = hash) do not support this feature) 注意:redis哈希模式(redis.storage =哈希)不支持此功能)
j2cache.sync_ttl_to_redis = true

# Whether to cache null objects by default (default false) 默认情况下是否缓存空对象(默认为false)
j2cache.default_cache_null_object = true

#########################################
# Cache Serialization Provider 缓存序列化提供程序
# values:
# fst -> using fast-serialization (recommend) 使用fast-serialization序列化(推荐)
# kryo -> using kryo serialization kryo序列化
# json -> using fst's json serialization (testing) 使用fst的json序列化(测试)
# fastjson -> using fastjson serialization (embed non-static class not support) 使用fastjson序列化(不支持嵌入的非静态类)
# java -> java standard Java标准
# fse -> using fse serialization 使用fse序列化
# [classname implements Serializer]
#########################################

j2cache.serialization = fst
#json.map.person = net.oschina.j2cache.demo.Person

#########################################
# Ehcache configuration
#########################################

ehcache.configXml = /j2cache/ehcache.xml

# ehcache3.configXml = /ehcache3.xml
# ehcache3.defaultHeapSize = 1000

#########################################
# Caffeine configuration
# caffeine.region.[name] = size, xxxx[s|m|h|d]
#
#########################################
#caffeine.properties = /caffeine.properties

#########################################
# Redis connection configuration
#########################################

#########################################
# Redis Cluster Mode
#
# single -> single redis server 单Redis服务器
# sentinel -> master-slaves servers 主从服务器
# cluster -> cluster servers (数据库配置无效,使用 database = 0)
# sharded -> sharded servers  (密码、数据库必须在 hosts 中指定,且连接池配置无效 ; redis://user:[email protected]:6379/0)
#
#########################################

redis.mode = single

#redis storage mode (generic|hash)
redis.storage = generic

## redis pub/sub channel name
redis.channel = j2cache
## redis pub/sub server (using redis.hosts when empty)
redis.channel.host =

#cluster name just for sharded
redis.cluster_name = j2cache

## redis cache namespace optional, default[empty]
redis.namespace =

## redis command scan parameter count, default[1000] redis命令扫描参数计数,默认为[1000]
#redis.scanCount = 1000

## connection
# Separate multiple redis nodes with commas, such as 192.168.0.10:6379,192.168.0.11:6379,192.168.0.12:6379

#redis.hosts = 127.0.0.1:6379
#redis.timeout = 2000
#redis.password =
#redis.database = 0
#redis.ssl = false

## redis pool properties redis线程池配置
#redis.maxTotal = 100
#redis.maxIdle = 10
#redis.maxWaitMillis = 5000
#redis.minEvictableIdleTimeMillis = 60000
#redis.minIdle = 1
#redis.numTestsPerEvictionRun = 10
#redis.lifo = false
#redis.softMinEvictableIdleTimeMillis = 10
#redis.testOnBorrow = true
#redis.testOnReturn = false
#redis.testWhileIdle = true
#redis.timeBetweenEvictionRunsMillis = 300000
#redis.blockWhenExhausted = false
#redis.jmxEnabled = false

#########################################
# Lettuce scheme
#
# redis -> single redis server
# rediss -> single redis server with ssl
# redis-sentinel -> redis sentinel
# redis-cluster -> cluster servers
#
#########################################

## redis command scan parameter count, default[1000]
lettuce.scanCount = 1000
lettuce.namespace =
lettuce.storage = hash
lettuce.channel = j2cache
lettuce.scheme = redis
lettuce.hosts = 127.0.0.1:6379
lettuce.password =
lettuce.database = 0
lettuce.sentinelMasterId =
lettuce.maxTotal = 100
lettuce.maxIdle = 10
lettuce.minIdle = 10
# timeout in milliseconds
lettuce.timeout = 10000
# redis cluster topology refresh interval in milliseconds
lettuce.clusterTopologyRefresh = 3000

#########################################
# memcached server configurations
# refer to https://gitee.com/mirrors/XMemcached
#########################################

#memcached.servers = 127.0.0.1:11211
#memcached.username =
#memcached.password =
#memcached.connectionPoolSize = 10
#memcached.connectTimeout = 1000
#memcached.failureMode = false
#memcached.healSessionInterval = 1000
#memcached.maxQueuedNoReplyOperations = 100
#memcached.opTimeout = 100
#memcached.sanitizeKeys = false

application-j2cache.yml

spring:
  cache:
    ehcache:
      config: classpath:j2cache/ehcache.xml

# j2cache配置
j2cache:
  config-location: classpath:j2cache/j2cache.properties
  cache-clean-mode: active
  open-spring-cache: true
  redis-client: lettuce

springboot主配置文件加入application-j2cache.yml文件使用
在这里插入图片描述

简单使用

package com.ljc.provide8001.service.impl;

import com.ljc.api.pojo.Dept;
import com.ljc.provide8001.dao.DeptDao;
import com.ljc.provide8001.service.IDeptService;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

@Service
public class DeptServiceImpl implements IDeptService {
    
    
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private CacheChannel cacheChannel;

    @Override
    @SuppressWarnings("all")
    public List<Dept> findAll() {
    
    
        List<Dept> list = new ArrayList<>();
        Collection<String> keys = cacheChannel.keys("dept");
        if (keys == null || keys.size() == 0 || !keys.contains("deptList")) {
    
    
            list = deptDao.findAll();
            cacheChannel.set("dept", "deptList", list);
        } else {
    
    
            CacheObject cacheObject = cacheChannel.get("dept", "deptList");
            System.out.println(cacheObject);
            list = (List<Dept>) cacheObject.getValue();
        }
        return list;
    }

    public boolean clearCache(){
    
    
        cacheChannel.clear("dept");
        return true;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_41995299/article/details/115552531
今日推荐