Java 实现缓存机制

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
********************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值