关于ConcurrentHashMap作缓存
来源:互联网 发布:金元证券交易软件下载 编辑:程序博客网 时间:2024/06/07 13:15
阅读spring的aop部分源码,AdvisedSupport这个类中把拦截器链放在一个ConcurrentHashMap中,作为缓存,这里考虑到并发因素显然不能使用hashmap,hashtable也许也可以,可以尝试一把。
// 初始化拦截器链缓存 private void initMethodCache() { this.methodCache = new ConcurrentHashMap<MethodCacheKey, List<Object>>(32); } ... private static class MethodCacheKey { private final Method method; private final int hashCode; public MethodCacheKey(Method method) { this.method = method; this.hashCode = method.hashCode(); } @Override public boolean equals(Object other) { if (other == this) { return true; } MethodCacheKey otherKey = (MethodCacheKey) other; return (this.method == otherKey.method); } @Override public int hashCode() { return this.hashCode; } }
可以看到key是一个私有内部类对象,根据Map的机制 ,put时会首先进行hashcode的比较,相同再根据 equals的结果判断是否key相同,决定能不能put。
hashcode()直接使用的是method的hashcode,equals()也是比较的method的地址,显然这个缓存map完全依赖于method变量,所以他必须是不可变的,如果提供了set方法,可能会导致key-value在放入map之后,key的hashcode发生了改变,这会导致数据的丢失,这也是把自定义对象作为Map的key时需要注意的。
0 0
- 关于ConcurrentHashMap作缓存
- ConcurrentHashMap缓存
- 关于ConcurrentHashMap
- ConcurrentHashMap进行缓存
- springboot+redis作缓存
- 通过ConcurrentHashMap实现本地缓存
- 通过ConCurrentHashMap构造结果缓存
- Java-ConcurrentHashMap实现缓存、异步
- 关于ConcurrentHashMap总结
- 使用IBatis作数据缓存
- 使用IBatis作数据缓存
- 采用静态Map、ConcurrentHashMap处理缓存
- 采用static Map、ConcurrentHashMap实现数据缓存
- 采用静态Map、ConcurrentHashMap处理缓存
- java 缓存 ConcurrentHashMap 单例 简单示例
- 基于ConcurrentHashMap实现带时限的缓存
- 关于 ConcurrentHashMap 的同步问题
- 关于1.8的ConcurrentHashMap资料
- 对http协议的一点理解
- C++ Primer_4th学习笔记(6)- string 对象的定义和初始化
- Spark常用函数讲解之键值RDD转换
- 设计模式---抽象工厂模式(C++实现)
- 语音学习笔记9------Matlab R2015a实现BP神经网络的嗓音识别
- 关于ConcurrentHashMap作缓存
- Android中Fragment的使用总结示例
- HDU 4908 BestCoder Sequence (哈希)
- 从梯度下降到拟牛顿法:详解训练神经网络的五大学习算法
- Collections工具类
- GPLT L2-005. 集合相似度【set集合】
- Java相关知识普及
- TCP/IP系列——长连接与短连接的区别
- Bootstrap 环境安装