欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中常用缓存Cache机制的实现

程序员文章站 2024-03-12 17:52:20
缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。 缓存主要可分为二大类:&n...

缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。

缓存主要可分为二大类:   

一、通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以xml格式,序列化文件dat格式还是其它文件格式; 

二、内存缓存,也就是实现一个类中静态map,对这个map进行常规的增删查.

import java.util.*; 

 //description: 管理缓存 

 //可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间 

public class cachemanager { 
 private static hashmap cachemap = new hashmap(); 

 //单实例构造方法 
 private cachemanager() { 
  super(); 
 } 
 //获取布尔值的缓存 
 public static boolean getsimpleflag(string key){ 
  try{ 
   return (boolean) cachemap.get(key); 
  }catch(nullpointerexception e){ 
   return false; 
  } 
 } 
 public static long getserverstartdt(string key){ 
  try { 
   return (long)cachemap.get(key); 
  } catch (exception ex) { 
   return 0; 
  } 
 } 
 //设置布尔值的缓存 
 public synchronized static boolean setsimpleflag(string key,boolean flag){ 
  if (flag && getsimpleflag(key)) {//假如为真不允许被覆盖 
   return false; 
  }else{ 
   cachemap.put(key, flag); 
   return true; 
  } 
 } 
 public synchronized static boolean setsimpleflag(string key,long serverbegrundt){ 
  if (cachemap.get(key) == null) { 
   cachemap.put(key,serverbegrundt); 
   return true; 
  }else{ 
   return false; 
  } 
 } 


 //得到缓存。同步静态方法 
 private synchronized static cache getcache(string key) { 
  return (cache) cachemap.get(key); 
 } 

 //判断是否存在一个缓存 
 private synchronized static boolean hascache(string key) { 
  return cachemap.containskey(key); 
 } 

 //清除所有缓存 
 public synchronized static void clearall() { 
  cachemap.clear(); 
 } 

 //清除某一类特定缓存,通过遍历hashmap下的所有对象,来判断它的key与传入的type是否匹配 
 public synchronized static void clearall(string type) { 
  iterator i = cachemap.entryset().iterator(); 
  string key; 
  arraylist arr = new arraylist(); 
  try { 
   while (i.hasnext()) { 
    java.util.map.entry entry = (java.util.map.entry) i.next(); 
    key = (string) entry.getkey(); 
    if (key.startswith(type)) { //如果匹配则删除掉 
     arr.add(key); 
    } 
   } 
   for (int k = 0; k < arr.size(); k++) { 
    clearonly(arr.get(k)); 
   } 
  } catch (exception ex) { 
   ex.printstacktrace(); 
  } 
 } 

 //清除指定的缓存 
 public synchronized static void clearonly(string key) { 
  cachemap.remove(key); 
 } 

 //载入缓存 
 public synchronized static void putcache(string key, cache obj) { 
  cachemap.put(key, obj); 
 } 

 //获取缓存信息 
 public static cache getcacheinfo(string key) { 

  if (hascache(key)) { 
   cache cache = getcache(key); 
   if (cacheexpired(cache)) { //调用判断是否终止方法 
    cache.setexpired(true); 
   } 
   return cache; 
  }else 
   return null; 
 } 

 //载入缓存信息 
 public static void putcacheinfo(string key, cache obj, long dt,boolean expired) { 
  cache cache = new cache(); 
  cache.setkey(key); 
  cache.settimeout(dt + system.currenttimemillis()); //设置多久后更新缓存 
  cache.setvalue(obj); 
  cache.setexpired(expired); //缓存默认载入时,终止状态为false 
  cachemap.put(key, cache); 
 } 
 //重写载入缓存信息方法 
 public static void putcacheinfo(string key,cache obj,long dt){ 
  cache cache = new cache(); 
  cache.setkey(key); 
  cache.settimeout(dt+system.currenttimemillis()); 
  cache.setvalue(obj); 
  cache.setexpired(false); 
  cachemap.put(key,cache); 
 } 

 //判断缓存是否终止 
 public static boolean cacheexpired(cache cache) { 
  if (null == cache) { //传入的缓存不存在 
   return false; 
  } 
  long nowdt = system.currenttimemillis(); //系统当前的毫秒数 
  long cachedt = cache.gettimeout(); //缓存内的过期毫秒数 
  if (cachedt <= 0||cachedt>nowdt) { //过期时间小于等于零时,或者过期时间大于当前时间时,则为false 
   return false; 
  } else { //大于过期时间 即过期 
   return true; 
  } 
 } 

 //获取缓存中的大小 
 public static int getcachesize() { 
  return cachemap.size(); 
 } 

 //获取指定的类型的大小 
 public static int getcachesize(string type) { 
  int k = 0; 
  iterator i = cachemap.entryset().iterator(); 
  string key; 
  try { 
   while (i.hasnext()) { 
    java.util.map.entry entry = (java.util.map.entry) i.next(); 
    key = (string) entry.getkey(); 
    if (key.indexof(type) != -1) { //如果匹配则删除掉 
     k++; 
    } 
   } 
  } catch (exception ex) { 
   ex.printstacktrace(); 
  } 

  return k; 
 } 

 //获取缓存对象中的所有键值名称 
 public static arraylist getcacheallkey() { 
  arraylist a = new arraylist(); 
  try { 
   iterator i = cachemap.entryset().iterator(); 
   while (i.hasnext()) { 
    java.util.map.entry entry = (java.util.map.entry) i.next(); 
    a.add((string) entry.getkey()); 
   } 
  } catch (exception ex) {} finally { 
   return a; 
  } 
 } 

 //获取缓存对象中指定类型 的键值名称 
 public static arraylist getcachelistkey(string type) { 
  arraylist a = new arraylist(); 
  string key; 
  try { 
   iterator i = cachemap.entryset().iterator(); 
   while (i.hasnext()) { 
    java.util.map.entry entry = (java.util.map.entry) i.next(); 
    key = (string) entry.getkey(); 
    if (key.indexof(type) != -1) { 
     a.add(key); 
    } 
   } 
  } catch (exception ex) {} finally { 
   return a; 
  } 
 } 

} 


package lhm.hcy.guge.frameset.cache; 

public class cache { 
  private string key;//缓存id 
  private object value;//缓存数据 
  private long timeout;//更新时间 
  private boolean expired; //是否终止 
  public cache() { 
    super(); 
  } 

  public cache(string key, object value, long timeout, boolean expired) { 
    this.key = key; 
    this.value = value; 
    this.timeout = timeout; 
    this.expired = expired; 
  } 

  public string getkey() { 
    return key; 
  } 

  public long gettimeout() { 
    return timeout; 
  } 

  public object getvalue() { 
    return value; 
  } 

  public void setkey(string string) { 
    key = string; 
  } 

  public void settimeout(long l) { 
    timeout = l; 
  } 

  public void setvalue(object object) { 
    value = object; 
  } 

  public boolean isexpired() { 
    return expired; 
  } 

  public void setexpired(boolean b) { 
    expired = b; 
  } 
} 

//测试类, 
class test { 
 public static void main(string[] args) { 
  system.out.println(cachemanager.getsimpleflag("alksd")); 
//  cachemanager.putcache("abc", new cache()); 
//  cachemanager.putcache("def", new cache()); 
//  cachemanager.putcache("ccc", new cache()); 
//  cachemanager.clearonly(""); 
//  cache c = new cache(); 
//  for (int i = 0; i < 10; i++) { 
//   cachemanager.putcache("" + i, c); 
//  } 
//  cachemanager.putcache("aaaaaaaa", c); 
//  cachemanager.putcache("abchcy;alskd", c); 
//  cachemanager.putcache("cccccccc", c); 
//  cachemanager.putcache("abcoqiwhcy", c); 
//  system.out.println("删除前的大小:"+cachemanager.getcachesize()); 
//  cachemanager.getcacheallkey(); 
//  cachemanager.clearall("aaaa"); 
//  system.out.println("删除后的大小:"+cachemanager.getcachesize()); 
//  cachemanager.getcacheallkey(); 


 } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。