package com.bdc.cache;
import java.util.HashMap;
import java.util.Map;
public class CacheTest {
private Map map = new HashMap();
public Object getValue(String key){
Object obj = map.get(key);
//先从缓存里面取值
if(obj == null){
//判断缓存里面是否有值数据
System.out.println("缓存中不存在,从数据库中取出");
obj =“The Value You Put” //模拟从数据库读取数据
map.put(key, obj);
//将取出的数据 放入缓存,以便下次从缓存取数据
}else{
System.out.println("缓存中存在此key的数据");
}
return obj;
}
public void
deleteCache(String key){
if(map.get(key)!=null){
map.remove(key);
}
}
public void updateCache(String key){
//模拟从数据库取出此数据
Object obj = "GetDataFromDB";
if(obj!=map.get(key)){
map.remove(key);
map.put(key, obj);
}
}
}
Junit测试-
package com.bdc.test;
import org.junit.Test;
import com.bdc.cache.CacheTest;
public class TestCache {
@Test
public void test(){
CacheTest cache = new CacheTest();
System.out.println("********************");
long t1 = System.currentTimeMillis();
String str1 = (String) cache.getValue("name");
for(int i=0;i<=10000;i++){
System.out.print("");
}
long t2 = System.currentTimeMillis();
System.out.println("str1 is :"+str1);
System.out.println("用时 :"+(t2-t1));
System.out.println("********************");
long t3 = System.currentTimeMillis();
String str2 = (String) cache.getValue("name");
for(int i=0;i<=10000;i++){
System.out.print("");
}
long t4 = System.currentTimeMillis();
System.out.println("str2 is :"+str2);
System.out.println("用时 :"+(t4-t3));
System.out.println("********************");
long t5 = System.currentTimeMillis();
String str3 = (String) cache.getValue("age");
for(int i=0;i<=10000;i++){
System.out.print("");
}
long t6 = System.currentTimeMillis();
System.out.println("str3 is :"+str3);
System.out.println("用时 :"+(t6-t5));
System.out.println("********************");
}
}
输出结果-
********************
缓存中不存在,从数据库中取出
str1 is :name baby
用时 :3
********************
缓存中存在
str2 is :name baby
用时 :1
********************
缓存中不存在,从数据库中取出
str3 is :age baby
用时 :2
********************
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java中的缓存框架
Java中的缓存框架
缓存 spring ide -
web缓存示例其他
-
Web缓存技术
本章导读 缓存主要是为了提高数据的读取速度。因为服务器和应用客户端之间提供的缓存类和操作
缓存 数据 sql 数据库 xml -
Android怎么在flagment里面写activity
一.Flag标志位在阅读源码的时候经常发现有一些标志属性使用一些位操作来判断是否具有该标志,增加标志或者去除标志。如:为什么要使用16进制呢?为何不使用十进制0,1,2...?原因是因为这样就可以方便的用位运算来表示共同具有某些属性或者直接判断是否具有某个属性。上面这句话我们稍微来分析一下。拿文件的权限来分析一下,我们知道文件有三个权限:读,写,执行。分别用4 , 2 , 1来表示,这样一来就可以
Android flag 标志位 addFlags (int flags) setFlags (int flags) 数据