• 写了一个Java的简单缓存模型


    缓存操作接口

    [java] view plain copy
     
    1. /** 
    2.  * 缓存操作接口 
    3.  *  
    4.  * @author xiudong 
    5.  * 
    6.  * @param <T> 
    7.  */  
    8. public interface Cache<T> {  
    9.   
    10.     /** 
    11.      * 刷新缓存数据 
    12.      *  
    13.      * @param key 缓存key 
    14.      * @param target 新数据 
    15.      */  
    16.     void refresh(String key, T target);  
    17.       
    18.     /** 
    19.      * 获取缓存 
    20.      *  
    21.      * @param key 缓存key 
    22.      * @return 缓存数据 
    23.      */  
    24.     T getCache(String key);  
    25.       
    26.     /** 
    27.      * 判断缓存是否过期 
    28.      *  
    29.      * @param key 缓存key 
    30.      * @return 如果缓存过期返回true, 否则返回false 
    31.      */  
    32.     Boolean isExpired(String key);  
    33.       
    34.     /** 
    35.      * 设置缓存过期时间 
    36.      *  
    37.      * @param key 缓存key 
    38.      * @param millsec 缓存过期时间,单位:毫秒 
    39.      */  
    40.     void setExpired(Long millsec);  
    41.       
    42.     /** 
    43.      * 是否存在缓存对象 
    44.      *  
    45.      * @param key 缓存key 
    46.      * @return 存在返回true,不存在返回false 
    47.      */  
    48.     Boolean exist(String key);  
    49. }  
    [java] view plain copy
     
    1. import java.util.Date;  
    2.   
    3. /** 
    4.  * 缓存实体 
    5.  *  
    6.  * @author xiudong 
    7.  * 
    8.  * @param <T> 
    9.  */  
    10. public class LastCache<T> {  
    11.       
    12.     /** 
    13.      * 上次缓存的数据 
    14.      */  
    15.     private T data;  
    16.       
    17.     /** 
    18.      * 最后刷新时间 
    19.      */  
    20.     private long refreshtime;  
    21.       
    22.     public LastCache(T data) {  
    23.         this(data, new Date().getTime());  
    24.     }  
    25.       
    26.     public LastCache(T data, long refreshtime) {  
    27.         this.data = data;  
    28.         this.refreshtime = refreshtime;  
    29.     }  
    30.       
    31.     public T getData() {  
    32.         return data;  
    33.     }  
    34.       
    35.     public void setData(T data) {  
    36.         this.data = data;  
    37.     }  
    38.       
    39.     public long getRefreshtime() {  
    40.         return refreshtime;  
    41.     }  
    42.       
    43.     public void setRefreshtime(long refreshtime) {  
    44.         this.refreshtime = refreshtime;  
    45.     }  
    46. }  
    [java] view plain copy
     
      1. import java.util.Date;  
      2. import java.util.Map;  
      3. import java.util.concurrent.ConcurrentHashMap;  
      4.   
      5. /** 
      6.  * 简单的缓存模型 
      7.  *  
      8.  * @author xiudong 
      9.  * 
      10.  * @param <T> 
      11.  */  
      12. public class SimpleCached<T> implements Cache<T> {  
      13.   
      14.     /** 
      15.      * 缓存数据索引 
      16.      */  
      17.     private Map<String, LastCache<T>> cache = new ConcurrentHashMap<String, LastCache<T>>();  
      18.       
      19.     /** 
      20.      * 缓存超时时间,单位:毫秒 
      21.      */  
      22.     private Long expired = 0L;  
      23.       
      24.     public SimpleCached() {  
      25.         this(5 * 1000 * 60L);  
      26.     }  
      27.       
      28.     public SimpleCached(Long expired) {  
      29.         this.expired = expired;  
      30.     }  
      31.   
      32.     @Override  
      33.     public void refresh(String key, T target) {  
      34.         if (cache.containsKey(key)) {  
      35.             cache.remove(key);  
      36.         }  
      37.         cache.put(key, new LastCache<T>(target));  
      38.     }  
      39.   
      40.     @Override  
      41.     public T getCache(String key) {  
      42.         if (!this.exist(key)) {  
      43.             return null;  
      44.         }  
      45.           
      46.         return cache.get(key).getData();  
      47.     }  
      48.   
      49.     @Override  
      50.     public Boolean isExpired(String key) {  
      51.         if (!this.exist(key)) {  
      52.             return null;  
      53.         }  
      54.           
      55.         long currtime = new Date().getTime();  
      56.         long lasttime = cache.get(key).getRefreshtime();  
      57.           
      58.         return (currtime - lasttime) > expired;  
      59.     }  
      60.   
      61.     @Override  
      62.     public void setExpired(Long millsec) {  
      63.         this.expired = millsec;  
      64.     }  
      65.   
      66.     @Override  
      67.     public Boolean exist(String key) {  
      68.         return cache.containsKey(key);  
      69.     }  
      70.       
      71. }  
  • 相关阅读:
    .NET WinForm 状态栏添加分隔符
    使用 MMC / IE 查看证书
    配置IIS在64位Windows上运行 32 位 ASP.NET 应用程序
    WCF部署到IIS:证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限
    PB6 调用 .net Web Service
    .NET程序加壳 — 之动态加载程序集
    statusStrip 状态条 toolStripStatusLabel 居右显示
    C# 使用 Stopwatch 测量代码运行时间
    Application_Start 事件中使用 Response.Redirect
    解决IIS中部署WCF时,访问.svc文件的404错误问题
  • 原文地址:https://www.cnblogs.com/du-0210/p/8427729.html
Copyright © 2020-2023  润新知