缓存使用ZCache
ZCache简介
ZCache是中兴软创基于Redis Cluster开发的分布式缓存平台。平台高效易用,提供了一键安装部署、系统在线扩容、多语言访问接口、可视化配置监控等一整套解决方案。
ZCache分布式缓存集群提供了以下两个好处:
- 将数据自动切分到多个节点的能力
- 当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力
1.首先添加Maven依赖
<dependency>
<groupId>com.ztesoft.zsmart.core</groupId>
<artifactId>core-boot-starter-cache</artifactId>
<version>${core.version}</version>
<exclusions>
<exclusion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</exclusion>
</exclusions>
</dependency>
这里排除了jedis
2.
/****************************************************************************************
Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or <br>
transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
****************************************************************************************/
package com.ztesoft.zsmart.bss.prov.common.config;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import com.ztesoft.zsmart.bss.prov.common.constant.CommonCacheDef;
import com.ztesoft.zsmart.core.boot.autoconfigure.cache.ZCacheProperties;
/**
* <Description> <br>
*
* @author xu.hong20<br>
* @version 1.0<br>
* @taskId <br>
* @CreateDate Nov 21, 2017 <br>
* @since R9.0<br>
* @see com.ztesoft.zsmart.bss.prov.common.config <br>
*/
@Configuration
@ConfigurationProperties(prefix = "prov.cache")
@EnableCaching // 用于开启Spring Cache支持
public class ProvZCacheConfiguration {
/**
* 缓存前缀,每个模块不能够重复
*/
private static final String PREFIX = CommonCacheDef.CACHE_MODULE + ".cache";
/**
*
* Description: <br>
*
* @author luoluocaihong<br>
* @taskId <br>
* @return <br>
*/
@Bean
@ConfigurationProperties(prefix = PREFIX)
public ZCacheProperties provZcacheProperties() {
return new ZCacheProperties();
}
/**
*
* Description: <br>
*
* @author luoluocaihong<br>
* @taskId <br>
* @param redisTemplate RedisTemplate
* @return <br>
*/
@Primary
@Bean("provCacheManager")
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setUsePrefix(true);
cacheManager.setTransactionAware(true);
ConcurrentHashMap<String, Long> map = new ConcurrentHashMap<String, Long>();
//map.put(CommonCacheDef.CACHE_SA_PARAM_REGION_DTO_LIST, 60L); // 配置缓存名
cacheManager.setExpires(map);
cacheManager.setCacheNames(map.keySet());
cacheManager.afterPropertiesSet();
return cacheManager;
}
}