zoukankan      html  css  js  c++  java
  • LRU算法简单例子

      1 package com.xy.thread;
      2 
      3 import java.util.HashMap;
      4 import java.util.Iterator;
      5 import java.util.Set;
      6 
      7 public class LRU
      8 {
      9     protected HashMap lruCache = new HashMap(2);
     10     //可操作的最大使用次数
     11     protected int MAX_INTEGER_NUMBER=2147483647;
     12     //缓存中保存的数大对象数目
     13     protected int max_object_num=1000;
     14     public LRU()
     15     {
     16     }
     17     
     18     public LRU(int maxObjectNum)
     19     {
     20         
     21         max_object_num=maxObjectNum;
     22     }
     23     
     24     
     25     
     26     /**
     27      * 增加对象到缓存中
     28      * @param key
     29      * @param value
     30      */
     31     public Object put(Object key, Object value)
     32     {
     33         CacheObject newValue = new CacheObject(value);
     34         if (lruCache.size()>=max_object_num)
     35         {
     36             removeLease();
     37         }
     38         
     39         return lruCache.put(key, newValue);
     40     }
     41     
     42     /**
     43      * 使用key来获取对象
     44      * 
     45      * @param key
     46      * @return
     47      */
     48     public Object get(Object key)
     49     {
     50         CacheObject object=(CacheObject)lruCache.get(key);
     51         if (object==null)
     52         {
     53             return null;
     54         }
     55         //根据LRU算法原则,将命中的对象计算器0,将其他对象的计算值加1
     56         Set set=lruCache.keySet();
     57         Iterator iter=set.iterator();
     58         Object keyObject=null;
     59         CacheObject cacheObject=null;
     60         while(iter.hasNext())
     61         {
     62             keyObject=iter.next();
     63             cacheObject=(CacheObject) lruCache.get(keyObject);
     64             cacheObject.setUsetimes(cacheObject.getUsetimes()+1);
     65         }
     66         object.setUsetimes(0);
     67         
     68   return object!=null? object.getValue():null;
     69     }
     70     public boolean containsKey(Object key)
     71     {
     72         return lruCache.containsKey(key);
     73     }
     74     public void clear()
     75     {
     76         lruCache.clear();
     77     }
     78     public int size()
     79     {
     80         return lruCache.size();
     81     }
     82     public boolean isEmpty()
     83     {
     84         return lruCache.isEmpty();
     85     }
     86     public boolean containsValue(Object value)
     87     {
     88         return lruCache.containsKey(value);
     89     }
     90     /**
     91      * 移除使用最少的对象
     92      */
     93     public void removeLease()
     94     {
     95         Object leaseUseObjectKey=null;
     96         int usetimes=0;
     97         Set set=lruCache.keySet();
     98         Iterator iter=set.iterator();
     99         while(iter.hasNext())
    100         {
    101             Object keyObject=iter.next();
    102             CacheObject object=(CacheObject) lruCache.get(keyObject);
    103             if (object.getUsetimes()>usetimes)
    104             {
    105                 usetimes=object.getUsetimes();
    106                 leaseUseObjectKey=keyObject;
    107             }
    108         }
    109         lruCache.remove(leaseUseObjectKey);
    110     }
    111     public Set keySet()
    112     {
    113         return lruCache.keySet();
    114     }
    115     /**
    116      * 移除使用最频繁的对象
    117      */
    118     public void removeMost()
    119     {
    120         Object leaseUseObjectKey=null;
    121         int usetimes=MAX_INTEGER_NUMBER;
    122         Set set=lruCache.keySet();
    123         Iterator iter=set.iterator();
    124         while(iter.hasNext())
    125         {
    126             Object keyObject=iter.next();
    127             CacheObject object=(CacheObject) lruCache.get(keyObject);
    128             if (object.getUsetimes()<usetimes)
    129             {
    130                 usetimes=object.getUsetimes();
    131                 leaseUseObjectKey=keyObject;
    132             }
    133         }
    134         lruCache.remove(leaseUseObjectKey);
    135     }
    136     
    137     /**
    138      * 移除最早置入缓存的对象
    139      */
    140     public void removeEarly()
    141     {
    142         Object leaseUseObjectKey=null;
    143         long time=System.currentTimeMillis()+365*24*60*60*1000;
    144         Set set=lruCache.keySet();
    145         Iterator iter=set.iterator();
    146         while(iter.hasNext())
    147         {
    148             Object keyObject=iter.next();
    149             CacheObject object=(CacheObject) lruCache.get(keyObject);
    150             if (object.getPushtime()<time)
    151             {
    152                 time=object.getPushtime();
    153                     leaseUseObjectKey=keyObject;
    154             }
    155         }
    156         lruCache.remove(leaseUseObjectKey);        
    157     }
    158     
    159     /**
    160      * 移除最迟放入的对象
    161      */
    162     public void removeLater()
    163     {
    164         Object leaseUseObjectKey=null;
    165         long time=-1;
    166         Set set=lruCache.keySet();
    167         Iterator iter=set.iterator();
    168         while(iter.hasNext())
    169         {
    170             Object keyObject=iter.next();
    171             CacheObject object=(CacheObject) lruCache.get(keyObject);
    172             if (object.getPushtime()>time)
    173             {
    174                 time=object.getPushtime();
    175                 leaseUseObjectKey=keyObject;
    176             }
    177         }
    178         lruCache.remove(leaseUseObjectKey);       
    179     }    
    180     
    181     /**
    182      * 删除某个键值及对应对象
    183      * @param key
    184      */
    185     public void remove(Object key)
    186     {
    187         lruCache.remove(key);
    188     }
    189     
    190     public static void main(String[] args)
    191     {
    192         LRU lru = new LRU(4);
    193         lru.put("a","The A String");
    194         lru.put("b","The B String");
    195         lru.put("d","The D String");
    196         lru.put("c","The C String");
    197         
    198         System.out.println(lru.toString()+"-----------------------------------------------");     
    199         
    200         lru.get("a");
    201         lru.get("b");
    202         lru.get("d");
    203         lru.get("a");
    204         lru.get("b");
    205         lru.get("d");
    206         lru.put("e","The E String");
    207         lru.get("e");
    208         lru.get("e");
    209         lru.get("e");
    210         lru.get("e");
    211         System.out.println(lru.toString());
    212     }
    213     public String toString()
    214     {
    215         StringBuffer strBf= new StringBuffer(10);
    216         Set set1=lruCache.keySet();
    217         Iterator iter1=set1.iterator();
    218         while(iter1.hasNext())
    219         {
    220             Object key=iter1.next();
    221             strBf.append(key+"=");
    222             strBf.append(lruCache.get(key));
    223             strBf.append("\n");
    224         }
    225         return strBf.toString();
    226     }
    227     
    228   
    229 }
     1 package com.xy.thread;
     2 
     3 /*
     4  * Created on 2004-9-7
     5  *
     6  * TODO To change the template for this generated file go to
     7  * Window - Preferences - Java - Code Style - Code Templates
     8  */
     9 /**
    10  *
    11  * TODO To change the template for this generated type comment go to
    12  * Window - Preferences - Java - Code Style - Code Templates
    13  */
    14 public class CacheObject
    15 {
    16     long pushtime = 0;
    17     int usetimes = 0;
    18     Object value = null;
    19     CacheObject( Object value )
    20     {
    21         pushtime = System.currentTimeMillis();
    22         usetimes = 0;
    23         this.value = value;
    24     }
    25     
    26     /**
    27      * @return Returns the pushtime.
    28      */
    29     public final long getPushtime()
    30     {
    31         return pushtime;
    32     }
    33     
    34     /**
    35      * @return Returns the usetimes.
    36      */
    37     public final int getUsetimes()
    38     {
    39         return usetimes;
    40     }
    41     /**
    42      * @param usetimes The usetimes to set.
    43      */
    44     public final void setUsetimes(int usetimes)
    45     {
    46         this.usetimes = usetimes;
    47     }
    48     /**
    49      * @return Returns the value.
    50      */
    51     public final Object getValue()
    52     {
    53         return value;
    54     }
    55     /**
    56      * @param value The value to set.
    57      */
    58     public final void setValue(Object value)
    59     {
    60         this.value = value;
    61     }
    62         
    63     public String toString()
    64     {
    65         StringBuffer strBf=new StringBuffer(10);
    66         strBf.append("value="+value+"\n");
    67         strBf.append("pushtime="+pushtime+"\n");
    68         strBf.append("usetimes="+usetimes+"\n");
    69         return strBf.toString();
    70     }
    71 }
  • 相关阅读:
    如何在页面上输出html标签:符号实体【转】
    js sort方法根据数组中对象的某一个属性值进行排序【转】
    原生js判断某个元素是否有指定的class名的几种方法【转】
    js支持的编码转换方法【转】
    windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 解决方案【转】
    PHP 生成随机数rand()和mt_rand()的区别【转】
    php中var关键字用法【转】
    2020软件工程个人作业06——软件工程实践总结作业
    旺宝的家——事后诸葛亮
    旺宝的家—冲刺总结
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/2953908.html
Copyright © 2011-2022 走看看