public class PacketCache extends LinkedHashMap<CacheKey,PacketWrapper>
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
private int |
capacity
The maximum number of bytes of packets in this cache
|
private static Logger |
log |
private long |
maxAge
The max age for a valid cache entry, 0 mean infinite
|
private int |
maxCacheItemPercentage
The max size of a cached item compared to the total size
|
private static long |
serialVersionUID |
private int |
totalSize
The current number of bytes of packets in this cache
|
Constructor and Description |
---|
PacketCache(int capacityMegaBytes,
int capacityBytes,
double maxAge)
Creates a cache with a size given by
cachesizemegabytes*2^20+cachesizebytes
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
PacketWrapper |
get(CacheKey key) |
PacketWrapper |
get(CacheKey key,
long now) |
int |
getByteCapacity() |
int |
getCapacity() |
PacketWrapper |
put(CacheKey key,
PacketWrapper value)
Adds a PacketWrapper object to this cache,
unless the size is more than maxCacheItemPercentage of the total size
|
PacketWrapper |
put(CacheKey key,
PacketWrapper result,
long timestamp)
Adds a BasicPacket array to this cache,
unless the size is more than maxCacheItemPercentage of the total size
|
PacketWrapper |
remove(CacheKey key) |
protected boolean |
removeEldestEntry(Map.Entry<CacheKey,PacketWrapper> eldest)
Overrides LinkedHashMap.removeEldestEntry as suggested to implement LRU cache.
|
private void |
removeOverflow() |
void |
setMaxCacheItemPercentage(int maxCapacityPercentage)
Sets the max size of a cached item compared to the total size
Cache requests for larger objects will be ignored
|
int |
totalPacketSize() |
containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
private static final long serialVersionUID
private int totalSize
private final int capacity
private int maxCacheItemPercentage
private final long maxAge
private static final Logger log
public PacketCache(int capacityMegaBytes, int capacityBytes, double maxAge)
capacityMegaBytes
- the cache size, measured in megabytescapacityBytes
- additional number of bytes to add to the cache sizemaxAge
- seconds a cache entry is valid, 0 or less are illegal argumentspublic void clear()
clear
in interface Map<CacheKey,PacketWrapper>
clear
in class LinkedHashMap<CacheKey,PacketWrapper>
public void setMaxCacheItemPercentage(int maxCapacityPercentage)
protected boolean removeEldestEntry(Map.Entry<CacheKey,PacketWrapper> eldest)
removeEldestEntry
in class LinkedHashMap<CacheKey,PacketWrapper>
private void removeOverflow()
public int getCapacity()
public int getByteCapacity()
public PacketWrapper put(CacheKey key, PacketWrapper value)
put
in interface Map<CacheKey,PacketWrapper>
put
in class HashMap<CacheKey,PacketWrapper>
public PacketWrapper put(CacheKey key, PacketWrapper result, long timestamp)
timestamp
- the timestamp for the first packet in the array,
unit millisecondspublic PacketWrapper get(CacheKey key)
public PacketWrapper get(CacheKey key, long now)
public PacketWrapper remove(CacheKey key)
public int totalPacketSize()
Copyright © 2018. All rights reserved.