Atitit spring cache key的生成 与ken生成规范 1. Good key name meth.params 1 1.1. Use epl 的mode but only clss
Atitit spring cache key的生成 与ken生成规范
1. Good key name meth.params 1
1.1. Use epl 的mode but only clss method ,and single param 1
1.2. 4、自定义key生成器 1
1.3. Spring xml cfg 2
1.4. CacheKeyGeneratorSpring 5
1. Good key name meth.params
Good key gene is class.method as package...
Param as sub package..
1.1. Use epl 的mode but only clss method ,and single param
1.2. 4、自定义key生成器
[java] view plain copy
1. @Cacheable(value="gomeo2oCache", keyGenerator = "keyGenerator")
2. public ResultDTO method(User user);
注意:Spring默认的SimpleKeyGenerator是不会将函数名组合进key中的
使用@EnableCaching启用Cache注解支持;
2、实现CachingConfigurer,然后注入需要的cacheManager和keyGenerator;从spring4开始默认的keyGenerator是SimpleKeyGenerator;
即如果只有一个参数,就使用参数作为key,否则使用SimpleKey作为key。
我们也可以自定义自己的key生成器,然后通过xml风格的<cache:annotation-driven key-generator=""/>或注解风格的CachingConfigurer中指定keyGenerator。
<!-- ati cache cfg-->
<bean id="CacheKeyGeneratorSpringAti" class="com.cnhis.cloudhealth.clinical.util.cache.CacheKeyGeneratorSpring">
</bean>
<context:annotation-config/>
<cache:annotation-driven key-generator="CacheKeyGeneratorSpringAti"/>
3. 使用 direct use or ,define the @Cacheable(value="gomeo2oCache", keyGenerator = "keyGenerator")
If cfg in spring xml..then default use ..
spring
1.3. Spring xml cfg
<!-- ati cache cfg-->
<bean id="CacheKeyGeneratorSpringAti" class="com.cnhis.cloudhealth.clinical.util.cache.CacheKeyGeneratorSpring">
</bean>
<context:annotation-config/>
<cache:annotation-driven key-generator="CacheKeyGeneratorSpringAti"/>
<!--
<bean id="cacheManager"
class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />
//C:\0wkspc\clis413\clinical\src\main\java\com\cnhis\cloudhealth\clinical\util\cache\MyConcurrentMapCacheManager.java
-->
<!-- (long expireTime sec, long maximumSize)
<bean id="cacheManager"
class="com.cnhis.cloudhealth.clinical.util.cache.MyConcurrentMapCacheManager">
<constructor-arg index="0" value="70" />
<constructor-arg index="1" value="500000" />
</bean>
<context:property-placeholder location="classpath:META-INF/prop/redis.properties"/>
-->
<context:property-placeholder location="classpath:META-INF/prop/redis.properties,classpath*:/META-INF/prop/newid.properties,classpath*:/META-INF/prop/jdbc.properties" />
<!--缓存管理器-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<!--自定义的redis缓存操作实现-->
<bean class="com.cnhis.cloudhealth.clinical.util.cache.SpringCacheWzRedis">
<property name="name" value="Cachename1"/>
<property name="redisTemplate" ref="redisTemplate"/>
</bean>
</set>
</property>
</bean>
<!--redis连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<!--
<property name="maxTotal" value="${redis.maxTotal}"/>
-->
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!--redis连接工厂配置-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="poolConfig" ref="poolConfig"/>
<property name="usePool" value="true"/>
<property name="database" value="2"/>
</bean>
<!--redis操作模板-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!-- end ati cache -->
<!-- end ati cache -->
1.4. CacheKeyGeneratorSpring
package com.cnhis.cloudhealth.clinical.util.cache;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.ClassUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.hash.Hashing;
// com.cnhis.cloudhealth.clinical.util.cache.CacheKeyGeneratorSpring
public class CacheKeyGeneratorSpring implements KeyGenerator {
// custom cache key
public static final int NO_PARAM_KEY = 0;
public static final int NULL_PARAM_KEY = 53;
private static Logger log = LoggerFactory.getLogger(CacheKeyGeneratorSpring.class);
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder key = new StringBuilder();
key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
if (params.length == 0) {
return key.append(NO_PARAM_KEY).toString();
}
for (Object param : params) {
if (param == null) {
log.warn("input null param for Spring cache, use default key={}", NULL_PARAM_KEY);
key.append(NULL_PARAM_KEY);
} else if (ClassUtils.isPrimitiveArray(param.getClass())) {
int length = Array.getLength(param);
for (int i = 0; i < length; i++) {
key.append(Array.get(param, i));
key.append(',');
}
} else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {
key.append(param);
} else {
log.warn("Using an object as a cache key may lead to unexpected results. " +
"Either use @Cacheable(key=..) or implement CacheKey. Method is " + target.getClass() + "#" + method.getName());
// key.append(param.hashCode());
if( param instanceof Map )
{
key.append( JSON.toJSONString(param) );
}
}
key.append('-');
}
String finalKey = key.toString();
long cacheKeyHash = Hashing.murmur3_128().hashString(finalKey, Charset.defaultCharset()).asLong();
log.debug("using cache key={} hashCode={}", finalKey, cacheKeyHash);
return key.toString();
}
}
Atitit spring cache key的生成 与ken生成规范 1. Good key name meth.params 1 1.1. Use epl 的mode but only clss相关推荐
- Spring Cache使用Redis自定义缓存key
一.什么是Spring Cache 从spring 3.1版本开始,提供了一种透明的方式来为现有的spring 应用添加cache.在应用层面与后端存储之间,提供了一层抽象,这层抽象目的在于封装各种可 ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)
概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...
- Spring Cache抽象-使用SpEL表达式
概述 SpEl表达式 概述 在Spring Cache注解属性中(比如key,condition和unless),Spring的缓存抽象使用了SpEl表达式,从而提供了属性值的动态生成及足够的灵活性. ...
- Spring Cache抽象-缓存注解
文章目录 概述 Spring缓存的基本原理 @Cacheable :主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 键生成器 带条件的缓存 @Cacheable 注解参数说明 示例-缓存管理 ...
- Spring Cache
在WEB后端应用程序来说,耗时比较大的往往有两个地方:一个是查数据库,一个是调用其它服务的API(因为其它服务最终也要去做查数据库等耗时操作).重复查询也有两种.一种是我们在应用程序中代码写得不好,写 ...
- springboot整合spring @Cache和Redis
转载自 springboot整合spring @Cache和Redis spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: @Cacheable:触发缓存写入. @ ...
- 注解驱动的 Spring cache 缓存介绍
概述 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术 ...
- spring cache相关注解介绍 @Cacheable、@CachePut、@CacheEvict
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. @Cacheable是用来声明方法是可缓存的.将结果存储到缓存中以便后续使用相同参数调用时不需执行实 ...
- Spring Cache 实战:兼容所有缓存中间件!
作者 | 悟空聊架构 来源 | 悟空聊架构(ID:PassJava666) 本篇给大家介绍一种兼容所有缓存中间件的方案,不论我们是使用 Redis 还是 Ehcache,都不需要关心如何操作 Redi ...
最新文章
- RHEL-5搭建SSH服务器
- WinSCP远程访问linux服务器和编辑文件
- JZOJ 5956. 【NOIP2018模拟11.7A组】easy LCA
- 线程----BlockingQueue (转)
- flask查询mysql数据展示_flask下直接展示mysql数据库 字段
- javascript console 函数详解 js开发调试的利器 浏览:3201|更新:2014-05-30 09:27
- 哪些事是你当了大学老师之后才知道的?
- Core Animation(核心动画)
- abort: error: Temporary failure in name resolution
- 导向滤波算法原理与代码
- POJ 3422 - Kaka's Matrix Travels(最小费用流)
- [AISTATS21]Towards Flexible Device Participation in Federated Learning阅读笔记
- [置顶]CHENEY-YANG'S BLOG(cheney-yang)
- 轻松查询多个韵达快运最后物流中含有某个地方的单号
- 支持向量机之线性可分支持向量机(一)
- (自)协方差矩阵与互协方差矩阵简介
- 快速校验非法字符工具
- 最优化方法 23:算子分裂法 ADMM
- 【19考研】计算机/软件等专业调剂信息集合!【完结版】
- 小i机器人最珍贵的双旦礼物:客户的认可和满意度
热门文章
- python安装opencv出错_python 3安装opencv 3时出错?
- python构建网站flask_某课Python Flask实现构建视频网站
- json转为tfrecord格式文件怎么转_怎么把pdf转换成jpg图片?pdf转图片格式的方法很好用...
- linux 汽车仪表软件架构,基于嵌入式Linux的汽车全数字仪表界面的设计
- python可不可以同时执行1000个线程_python怎么能同时执行代码(多线程)?
- SCSI硬盘设备到/dev/sd设备的映射关系
- winform 控件没有Cursor属性时的处理办法
- HDU 2531 (BFS搜索)
- textarea 在 Chrome Safari FireFox 浏览器中禁用拖动和固定大小
- Spring MVC-学习笔记(1)认识spring mvc