java cache 简单应用

本文介绍了一个简单的Java缓存实现方式,使用静态HashMap作为缓存容器,并提供了增删查改等基本操作。此外还实现了缓存过期检查机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://oyhk.iteye.com/blog/907712

说明:转载只是给自己一个笔记而已,没有什么别的商业用途。


java cache 简单应用

 

HK学习笔记

首先什么是java 缓存的应用大家就看这个东西吧!!! 我上传一个PPT大家可以下载!

其实java cache就是一个static map;当你把它初始化了,你可以在同一个线程里共用

下面我来做一个例子,这些都是初学者的哦.高手就不要介意了

  • 如果 大家有什么不明白..请留言吧,我马上解释!!!

  • 注意:想取到缓存的时候,一定要在同一个进程里,才能访问,不然的话是null的!
  • 到于怎么才是同一个进程,就是测试类里main方法开始执行到毕业里的的线程,
  • 或者子线程都可以访问共享的缓存

 

首先是一个类

1.首先定义一个static HashMap ; 这个map用来存放缓存的!!

Java代码  收藏代码
  1. package com.oywk.cache;  
  2.   
  3. import java.util.Date;  
  4. import java.util.HashMap;  
  5.   
  6. /** 
  7.  * @author HK 
  8.  * 
  9.  * @date 2011-2-15 下午09:40:00 
  10.  */  
  11. public class CacheManager {  
  12.       
  13.      private static HashMap cacheMap = new HashMap();  
  14.   
  15.      /** 
  16.       * This class is singleton so private constructor is used. 
  17.       */  
  18.      private CacheManager() {  
  19.              super();  
  20.      }  
  21.   
  22.      /** 
  23.       * returns cache item from hashmap 
  24.       * @param key 
  25.       * @return Cache 
  26.       */  
  27.      private synchronized static Cache getCache(String key) {  
  28.              return (Cache)cacheMap.get(key);  
  29.      }  
  30.   
  31.      /** 
  32.       * Looks at the hashmap if a cache item exists or not 
  33.       * @param key 
  34.       * @return Cache 
  35.       */  
  36.      private synchronized static boolean hasCache(String key) {  
  37.              return cacheMap.containsKey(key);  
  38.      }  
  39.   
  40.      /** 
  41.       * Invalidates all cache 
  42.       */  
  43.      public synchronized static void invalidateAll() {  
  44.              cacheMap.clear();  
  45.      }  
  46.   
  47.      /** 
  48.       * Invalidates a single cache item 
  49.       * @param key 
  50.       */  
  51.      public synchronized static void invalidate(String key) {  
  52.              cacheMap.remove(key);  
  53.      }  
  54.   
  55.      /** 
  56.       * Adds new item to cache hashmap 
  57.       * @param key 
  58.       * @return Cache 
  59.       */  
  60.      private synchronized static void putCache(String key, Cache object) {  
  61.         cacheMap.put(key, object);  
  62.      }  
  63.   
  64.      /** 
  65.       * Reads a cache item's content 
  66.       * @param key 
  67.       * @return 
  68.       */  
  69.      public static Cache getContent(String key) {  
  70.               if (hasCache(key)) {  
  71.                      Cache cache = getCache(key);  
  72.                      if (cacheExpired(cache)) {  
  73.                              cache.setExpired(true);  
  74.                      }  
  75.                      return cache;  
  76.               } else {  
  77.                       return null;  
  78.               }  
  79.      }  
  80.   
  81.      /** 
  82.       *  
  83.       * @param key 
  84.       * @param content 
  85.       * @param ttl 
  86.       */  
  87.      public static void putContent(String key, Object content, long ttl) {  
  88.              Cache cache = new Cache();  
  89.              cache.setKey(key);  
  90.              cache.setValue(content);  
  91.              cache.setTimeOut(ttl + new Date().getTime());  
  92.              cache.setExpired(false);  
  93.              putCache(key, cache);  
  94.      }  
  95.        
  96.      /** @modelguid {172828D6-3AB2-46C4-96E2-E72B34264031} */  
  97.      private static boolean cacheExpired(Cache cache) {  
  98.              if (cache == null) {  
  99.                      return false;  
  100.              }  
  101.              long milisNow = new Date().getTime();  
  102.              long milisExpire = cache.getTimeOut();  
  103.              if (milisExpire < 0) {                // Cache never expires   
  104.                      return false;  
  105.              } else if (milisNow >= milisExpire) {  
  106.                      return true;  
  107.              } else {  
  108.                      return false;  
  109.              }  
  110.      }  
  111.   
  112. }  

 

 

跟着下面又一个类

Java代码  收藏代码
  1. package com.oywk.cache;  
  2.   
  3. /** 
  4.  * @author HK 
  5.  * 
  6.  * @date 2011-2-15 下午06:45:57 
  7.  */  
  8. public class Cache {  
  9.      private String key;  
  10.      private Object value;  
  11.      private long timeOut;  
  12.      private boolean expired;  
  13.      public Cache() {  
  14.              super();  
  15.      }  
  16.                
  17.      public Cache(String key, String value, long timeOut, boolean expired) {  
  18.              this.key = key;  
  19.              this.value = value;  
  20.              this.timeOut = timeOut;  
  21.              this.expired = expired;  
  22.      }  
  23.   
  24.      public String getKey() {  
  25.              return key;  
  26.      }  
  27.   
  28.      public long getTimeOut() {  
  29.              return timeOut;  
  30.      }  
  31.   
  32.      public Object getValue() {  
  33.              return value;  
  34.      }  
  35.   
  36.      public void setKey(String string) {  
  37.              key = string;  
  38.      }  
  39.   
  40.      public void setTimeOut(long l) {  
  41.              timeOut = l;  
  42.      }  
  43.   
  44.      public void setValue(Object object) {  
  45.              value = object;  
  46.      }  
  47.   
  48.      public boolean isExpired() {  
  49.              return expired;  
  50.      }  
  51.   
  52.      public void setExpired(boolean b) {  
  53.              expired = b;  
  54.      }  
  55. }  

 

 

下面是测试的类

Java代码  收藏代码
  1. package com.oywk.cache;  
  2.   
  3. /** 
  4.  * @author HK 
  5.  * 
  6.  * @date 2011-2-16 下午02:21:38 
  7.  */  
  8. public class CacheTest {  
  9.   
  10.     public static void main(String[] args) {  
  11.         //在缓存里加入三个学生  
  12.         for(int i = 0;i<3;i++){  
  13.             Student s = new Student();  
  14.             s.setId(i);  
  15.             s.setName("student"+i);  
  16.             //把一个student 加入缓存  
  17.             CacheManager.putContent(""+i, s, 300000);  
  18.         }  
  19.         //从缓存中把一个student 取出来  
  20.         Student student = (Student) CacheManager.getContent("1").getValue();  
  21.         System.out.println("id : " +student.getId()+ "      name :  "+ student.getName());  
  22.     }  
  23. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值