在分布式的程序中,cache的合理使用可以带来性能上的极大提升,尤其是在资源创建需要昂贵的开销时。cache的设计最重要的是要保证线程安全和高效性。下面以代码为例,介绍了三种cache的写法。
1. 粗放的加锁
public class Cache1 {
private HashMap<String, ServerGroup> route2SG = null;
public Cache1() {
route2SG = new HashMap<String, ServerGroup>();
}
public synchronized ServerGroup get(String routeKey) throws IOException {
ServerGroup sg = null;
sg = route2SG.get(routeKey);
if (sg == null) {
sg = getServerGroup(routeKey);
route2SG.put(routeKey, sg);
}
return sg;
}
public synchronized void remove(String routeKey) {
route2SG.remove(routeKey);
}
private ServerGroup getServerGroup(String routeKey) throws IOException {
ServerGroup sg = null;
/**
* Construct ServerGroup here
*/
return sg;
}
}
2. 读写锁
public class Cache2 {
private ConcurrentHashMap<String, ServerGroup> route2SG = null;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
public Cache2() {
route2SG = new ConcurrentHashMap<String, ServerGroup>();
}
public ServerGroup get(String routeKey) throws IOException {
ServerGroup sg = null;
try {
lock.readLock().lock();
sg = route2SG.get(routeKey);
if (sg == null) {
lock.readLock().unlock();
lock.writeLock().lock();
sg = route2SG.get(routeKey);
if (sg == null) {
sg = getServerGroup(routeKey);
route2SG.put(routeKey, sg);
}
lock.readLock().lock();
lock.writeLock().unlock();
}
} catch (IOException e) {
lock.writeLock().unlock();
throw (e);
}
lock.readLock().unlock();
return sg;
}
public void remove(String routeKey) {
try {
lock.writeLock().lock();
route2SG.remove(routeKey);
} finally {
lock.writeLock().unlock();
}
}
private ServerGroup getServerGroup(String routeKey) throws IOException {
ServerGroup sg = null;
/**
* Construct ServerGroup here
*/
return sg;
}
}
3. 无锁
public class Cache3 {
private ConcurrentHashMap<String, FutureTask<ServerGroup>> route2SGFT = null;
public Cache3() {
route2SGFT = new ConcurrentHashMap<String, FutureTask<ServerGroup>>();
}
public ServerGroup get(String routeKey) throws IOException, InterruptedException, ExecutionException {
FutureTask<ServerGroup> ft = route2SGFT.get(routeKey);
if (ft != null) {
return ft.get();
}
FutureTask<ServerGroup> sft = new FutureTask<ServerGroup>(new ConstructSGTask(routeKey));
FutureTask<ServerGroup> old = route2SGFT.putIfAbsent(routeKey, sft);
if (old == null) {
old=sft;
old.run();
}
return old.get();
}
public void remove(String routeKey) {
route2SGFT.remove(routeKey);
}
class ConstructSGTask implements Callable<ServerGroup> {
private final String key;
public ConstructSGTask(String key) {
super();
this.key = key;
}
@Override
public ServerGroup call() throws Exception {
return getServerGroup(key);
}
}
private ServerGroup getServerGroup(String routeKey) throws IOException {
ServerGroup sg = null;
/**
* Construct ServerGroup here
*/
return sg;
}
}
总结,
从三份代码中可以看出,锁的粒度从粗放到无,这个就极大的提高了cache的并发性。
分享到:
相关推荐
根据直接映射的cache工作原理,针对一个8行的cache工作过程进行仿真,每行(块)为一个字,1字4字节,需要读取的内存地址在trace.txt文件中列出。请编写程序,实现8行的cache工作过程进行仿真,可显示每个内存地址,...
等待事件row cache lock,latch row cache objects处理过程
cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验cache实验...
一个cache小组件.jar
Cache的工作原理...................................................................
Ensemble(Cache)通过ODBC连接Mysql数据库并调用其存储过程。 web.test.mysql.DHCUser.cls的源代码.
当片上多处理器系统上运行多个不同程序时,如何给这些不同的应用程序分配适当的cache空间成为一个难题。Cache划分就是解决这一难题的有效方法,目前大部分的划分方法都是针对最后一级共享cache设计的。私有cache划分...
实验五: 虚拟Cache与伪相联Cache 及实验过程。
Doctrine Cache是一个使用的php缓存类库
该工程包含数据缓存D_Cache和指令缓存I_Cache的Verilog代码和仿真文件,Cache的详细技术参数包含在.v文件的注释中。 直接相连16KB D_Cache Cache写策略: 写回法+写分配 (二路)组相连16KB I_Cache Cache替换策略: ...
ccache(common cache)是一个使用共享内存实现的cache静态库,在节点数据不足时采用LRU算法进行节点的淘汰. 与memcache的区别在于,首先memcache是一个完整的server程序,不仅有cache的处理操作,还需要监听及处理客户端...
本文是开发过程研究笔记,记录了springboot使用spring cache过程。springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
则会影响到另一个软件,因为这两个软件存储试用期相关信息当中使用了一个共用的文件, 就是windows\system32\下面的特殊隐藏文件, 重置试用期势必要执行清理这个文件的动作,所以如果同时安装使用了Primo的两款软件...
THREAD_CACHE MySQL里面为了提高客户端请求创建连接过程的性能,提供了一个连接池也就是 Thread_Cache池,将空闲的连接线程放在连接池中,而不是立即销毁.这样的好处就是,当又有一个新的请求的时候,mysql不会立即去创建...
java连接cache数据库说明,数据库驱动,cache可视化工具
头歌计算机组成原理2路组相联cache设计头歌计算机组成原理2路组相联cache设计头歌计算机组成原理2路组相联cache设计头歌计算机组成原理2路组相联cache设计头歌计算机组成原理2路组相联cache设计头歌计算机组成原理2...
3) 改变Cache容量(*2,*4,*8,*64),运行程序(指明所选的测试程序),统计各种失效的次数,并分析Cache容量对Cache性能的影响; 4) 改变Cache的相联度(1路,2路,4路,8路,64路),运行程序(指明所选的测试...
计算的存储系统包括cache---内存---外存,你真的详细了解什么是cache吗,了解cache的 Associativity 吗,这个文档会给你一个很详细的解释,希望各位喜欢!
体系结构试验报告cache存储过程.doc
Zend Cache - 一个通用的PHP缓存系统