一、引入jar包java
<!--緩存--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
.properties配置文件配置(xml中):node
#J2Cache configuration ######################################### # Cache Broadcast Method # values: # redis -> use redis publish/subscribe mechanism (using jedis) # lettuce -> use redis publish/subscribe mechanism (using lettuce) # jgroups -> use jgroups's multicast # rabbitmq -> use RabbitMQ publisher/consumer mechanism # rocketmq -> use RocketMQ publisher/consumer mechanism # 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 ######################################### #j2cache.broadcast = redis j2cache.broadcast = com.jeesite.common.j2cache.cache.support.redis.SpringRedisPubSubPolicy ######################################### # Cache Clean Mode # active -> 主動清除,二級緩存過時主動通知各節點清除,優勢在於全部節點能夠同時收到緩存清除 # passive -> 被動清除,一級緩存過時進行通知各節點清除一二級緩存 # blend -> 兩種模式一塊兒運做,對於各個節點緩存準確以及及時性要求高的能夠使用,正經常使用前兩種模式中一個就可 ######################################### j2cache.broadcast.cache_clean_mode = passive ######################################### # Level 1&2 provider # values: # none -> disable this level cache # ehcache -> use ehcache2 as level 1 cache # ehcache3 -> use ehcache3 as level 1 cache # caffeine -> use caffeine as level 1 cache(only in memory) # redis -> use redis as level 2 cache (using jedis) # lettuce -> use redis as level 2 cache (using 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. # memcached -> use memcached as level 2 cache (xmemcached), # [classname] -> use custom provider ######################################### j2cache.L1.provider_class = caffeine #j2cache.L2.provider_class = redis j2cache.L2.provider_class = com.jeesite.common.j2cache.cache.support.redis.SpringRedisProvider # When L2 provider isn't `redis`, using `L2.config_section = redis` to read redis configurations j2cache.L2.config_section = redis # Enable/Disable ttl in redis cache data (if disabled, the object in redis will never expire, default:true) # NOTICE: redis hash mode (redis.storage = hash) do not support this feature) j2cache.sync_ttl_to_redis = true # Whether to cache null objects by default (default false) j2cache.default_cache_null_object = true ######################################### # Cache Serialization Provider # values: # fst -> using fast-serialization (recommend) # kyro -> using kyro serialization # json -> using fst's json serialization (testing) # fastjson -> using fastjson serialization (embed non-static class not support) # java -> java standard # [classname implements Serializer] ######################################### j2cache.serialization = fst ######################################### # Caffeine configuration # caffeine.region.[name] = size, xxxx[s|m|h|d] ######################################### #caffeine.properties = /config/caffeine.properties caffeine.region.default = 10000, 1h caffeine.region.sessionCache = 100000, 30m ######################################### # Redis connection configuration ######################################### #redis storage mode (generic|hash) redis.storage = hash ## redis pub/sub channel name redis.channel = j2cache ## redis pub/sub server (using redis.hosts when empty) redis.channel.host = ## redis cache namespace optional, default[j2cache] redis.namespace = jeesite
二、代碼應用redis
類前加註解 @CacheConfig(cacheNames = {"comCodes","cardSubType"}) 一個方法對應數組中的一個元素,多個方法則對應多個元素spring
方法前加註解 @Cacheable(cacheNames = {"comCode"})json
三、服務器中須要啓用Redis服務,網上下載Redis-x64-3.2.100.zip(我本身使用的是3.2.100版本)並解壓,點擊redis-server.exe啓動緩存便可(服務運行期間,切勿關閉redis-server服務窗口)數組