首页

关于oschrenk-util源码包定义Cache缓存接口类、LRUCache/LFUCache/RandomCache缓存实现类源码说明

标签:oschrenk-util,Cache,缓存接口,LRUCache,LFUCache,RandomCache,缓存实现类     发布时间:2018-07-23   

一、前言

关于oschrenk-util源码包中com.oschrenk.io.Cache缓存定义接口、com.oschrenk.io.AbstractCache抽象缓存类、com.oschrenk.io.LFUCache/LRUCache/RandomCache缓存实现类,详情参见源码说明。

二、源码说明  

1.Cache缓存接口、AbstractCache抽象实现类

package com.oschrenk.io;@b@@b@import java.util.Collection;@b@import java.util.Map.Entry;@b@@b@public abstract interface Cache<K, V>@b@{@b@  public abstract void put(K paramK, V paramV);@b@@b@  public abstract V get(K paramK);@b@@b@  public abstract Collection<Map.Entry<K, V>> getAll();@b@@b@  public abstract void clear();@b@@b@  public abstract void remove(K paramK);@b@@b@  public abstract int capacity();@b@@b@  public abstract int numEntries();@b@}
package com.oschrenk.io;@b@@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@@b@public abstract class AbstractCache<K, V>@b@  implements Cache<K, V>@b@{@b@  static final float DEFAULT_LOAD_FACTOR = 0.75F;@b@  int capacity;@b@  Map<K, V> map;@b@@b@  public AbstractCache(int capacity)@b@  {@b@    this.capacity = capacity;@b@  }@b@@b@  public void put(K key, V value)@b@  {@b@    this.map.put(key, value);@b@  }@b@@b@  public V get(K key)@b@  {@b@    if (this.map.get(key) != null)@b@      return this.map.get(key);@b@@b@    return null;@b@  }@b@@b@  public synchronized Collection<Map.Entry<K, V>> getAll()@b@  {@b@    return new ArrayList(this.map.entrySet());@b@  }@b@@b@  public void clear()@b@  {@b@    this.map.clear();@b@  }@b@@b@  public void remove(K key)@b@  {@b@    this.map.remove(key);@b@  }@b@@b@  public int capacity()@b@  {@b@    return this.capacity;@b@  }@b@@b@  public int numEntries()@b@  {@b@    return this.map.size();@b@  }@b@}

2.LFUCache/LRUCache/RandomCache缓存实现类

package com.oschrenk.io;@b@@b@import com.oschrenk.utils.CollectionUtils;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.SortedSet;@b@@b@public class LFUCache<K, V> extends AbstractCache<K, V>@b@{@b@  Map<K, Integer> lfuMap;@b@  SortedSet<Map.Entry<K, Integer>> lfuSet;@b@@b@  public LFUCache(int capacity)@b@  {@b@    super(capacity);@b@    int hashTableCapacity = (int)Math.ceil(capacity / 0.75F) + 1;@b@@b@    this.lfuMap = new HashMap();@b@    this.map = new HashMap(hashTableCapacity, 0.75F);@b@  }@b@@b@  public void put(K key, V value)@b@  {@b@    if (capacity() == numEntries()) {@b@      this.lfuSet = CollectionUtils.getSortedMapEntries(this.lfuMap);@b@@b@      this.map.remove(((Map.Entry)this.lfuSet.first()).getKey());@b@      this.lfuMap.remove(this.lfuSet.first());@b@    }@b@@b@    this.lfuMap.put(key, Integer.valueOf(0));@b@    this.map.put(key, value);@b@  }@b@@b@  public V get(K key)@b@  {@b@    if (this.map.get(key) != null) {@b@      this.lfuMap.put(key, Integer.valueOf(((Integer)this.lfuMap.get(key)).intValue() + 1));@b@      return this.map.get(key);@b@    }@b@    return null;@b@  }@b@}
package com.oschrenk.io;@b@@b@import java.util.LinkedHashMap;@b@import java.util.Map.Entry;@b@@b@public class LRUCache<K, V> extends AbstractCache<K, V>@b@{@b@  public LRUCache(int capacity)@b@  {@b@    super(capacity);@b@    int hashTableCapacity = (int)Math.ceil(capacity / 0.75F) + 1;@b@@b@    this.map = new LinkedHashMap(this, hashTableCapacity, 0.75F, true)@b@    {@b@      private static final long serialVersionUID = 1L;@b@@b@      protected boolean removeEldestEntry()@b@      {@b@        return (size() > this.this$0.capacity());@b@      }@b@    };@b@  }@b@}
package com.oschrenk.io;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Random;@b@@b@public class RandomCache<K, V> extends AbstractCache<K, V>@b@{@b@  private Random random;@b@  private Map<Integer, K> indexKeyMap;@b@@b@  public RandomCache(int capacity)@b@  {@b@    super(capacity);@b@@b@    this.random = new Random();@b@    int hashTableCapacity = (int)Math.ceil(capacity / 0.75F) + 1;@b@@b@    this.indexKeyMap = new HashMap(hashTableCapacity);@b@    this.map = new HashMap(hashTableCapacity, 0.75F);@b@  }@b@@b@  public void put(K key, V value)@b@  {@b@    if (capacity() == numEntries()) {@b@      int i = this.random.nextInt(this.capacity);@b@      Object oldKey = this.indexKeyMap.get(Integer.valueOf(i));@b@@b@      this.indexKeyMap.put(Integer.valueOf(i), key);@b@      this.map.remove(oldKey);@b@    }@b@    else {@b@      this.indexKeyMap.put(Integer.valueOf(numEntries() + 1), key);@b@    }@b@@b@    this.map.put(key, value);@b@  }@b@}