首页 > 代码库 > LRU

LRU

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by pc on 2017/1/25.
 */
public class LRUCache {

    private Object data;
    private Date lastTime;

    public LRUCache(Object data, Date lastTime) {
        this.data =http://www.mamicode.com/ data;
        this.lastTime = lastTime;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data =http://www.mamicode.com/ data;
    }

    public Date getLastTime() {
        return lastTime;
    }

    public void setLastTime(Date lastTime) {
        this.lastTime = lastTime;
    }

    @Override
    public String toString() {
        return "data:"+data.toString()+" lastTime:"+date2Str(lastTime);
    }

    /**
     * 获取系统当前时间
     * @return
     */
    public static String date2Str(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.format(date);
    }
}

 

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 总结
 * Created by pc on 2017/1/25.
 */
public class LRUCacheImpl {

    private int total;//设置缓存集合固定大小

    private LRUCache lruCache;

    private List<LRUCache> list = new ArrayList<>();

    public LRUCacheImpl(int total){
        this.total = total;
    }

    private int getListSize(){
        return list.size();
    }

    private void setElement(LRUCache lruCache){
        int oldSize = getListSize();
        if(oldSize+1>total){//超出则淘汰最近最少访问
            //排序 淘汰
            sortAsc();//当数据量太大时,这个排序肯定不行 所以按数组这样的方式淘汰是不行的,需要使用链表FIFO节省
            list.remove(0);//移除第一个
            list.add(lruCache);
        }else{
            list.add(lruCache);
        }
    }

    private void sortAsc(){
        //按时间升序排
        list.sort(new Comparator<LRUCache>() {
            @Override
            public int compare(LRUCache o1, LRUCache o2) {
                return o1.getLastTime().compareTo(o2.getLastTime());
            }
        });
    }

    public List<LRUCache> getList(){
        return list;
    }

    public static void main(String[] args) {
        String pattern = "HH:mm:ss";

        LRUCache lruCache1 = new LRUCache("item1",parse("13:15:11",pattern));
        LRUCache lruCache2 = new LRUCache("item2",parse("11:10:01",pattern));
        LRUCache lruCache3 = new LRUCache("item3",parse("02:04:50",pattern));
        LRUCache lruCache4 = new LRUCache("item4",parse("21:35:06",pattern));
        LRUCache lruCache5 = new LRUCache("item5",parse("22:37:06",pattern));

        LRUCacheImpl impl = new LRUCacheImpl(4);
        impl.setElement(lruCache1);
        impl.setElement(lruCache2);
        impl.setElement(lruCache3);
        impl.setElement(lruCache4);

        List<LRUCache> list = impl.getList();
        System.out.println(list);

        impl.setElement(lruCache5);
        System.out.println(list);

        //[data:item1 lastTime:13:15:11, data:item2 lastTime:11:10:01, data:item3 lastTime:02:04:50, data:item4 lastTime:21:35:06]
        //[data:item3 lastTime:02:04:50, data:item2 lastTime:11:10:01, data:item1 lastTime:13:15:11, data:item4 lastTime:21:35:06]
        //最后一次访问时间最早的元素首先被淘汰
    }


    /**
     * 使用用户格式提取字符串日期
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */
    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


}

 

LRU