K - Key for the cache items.V - Value for the cache items.public class LruCache<K,V> extends Object implements ILoadingCache<K,V>
A variation of a Least Recently Used (LRU) cache is used. LRU cache keeps track of what was used when in order to discard the least recently used items first. Every time a cache item is used the "age" of the item used is updated.
This implementation supports concurrency by using multiple linked lists in place of a single linked list in the original implementation. The linked list to use is assigned at random and stored in the cached item. This will generate an even set of results across the different linked lists. The approach reduces the probability of the same linked list being locked when used in a environments with a high degree of concurrency. If the feature is not required then the constructor should be provided with a concurrency value of 1.
For a vast majority of the real life environments a constant stream of unique User-Agents is a fairly rare event. Usually the same User-Agent can be encountered multiple times within a fairly short period of time as the user is making a subsequent request. Caching frequently occurring User-Agents improved detection speed considerably.
Some devices are also more popular than others and while the User-Agents for such devices may differ, the combination of components used would be very similar. Therefore internal caching is also used to take advantage of the more frequently occurring entities.
This class should not be called as it is part of the internal logic.
| Modifier and Type | Class and Description |
|---|---|
static class |
LruCache.LruBuilder |
ICache.Base<K,V>| Constructor and Description |
|---|
LruCache(int cacheSize)
Constructs a new instance of the cache.
|
LruCache(int cacheSize,
int concurrency,
IValueLoader<K,V> loader)
Constructs a new instance of the cache.
|
LruCache(int cacheSize,
IValueLoader<K,V> loader)
Constructs a new instance of the cache.
|
| Modifier and Type | Method and Description |
|---|---|
static ICacheBuilder |
builder()
Return builder for an LRU cache
|
V |
get(K key)
Retrieves the value for key requested.
|
V |
get(K key,
IValueLoader<K,V> loader)
Retrieves the value for key requested.
|
long |
getCacheMisses()
The number of requests that could not be served
|
long |
getCacheRequests()
The number of requests made to the cache
|
long |
getCacheSize()
The number of items the cache lists should have capacity for.
|
double |
getPercentageMisses()
a fraction < 1 (not a percentage) misses/requests
|
void |
resetCache()
Resets the 'stats' for the cache.
|
void |
setCacheLoader(IValueLoader<K,V> loader)
Loader used to fetch items not in the cache.
|
public LruCache(int cacheSize)
cacheSize - The number of items to store in the cache.public LruCache(int cacheSize,
IValueLoader<K,V> loader)
cacheSize - The number of items to store in the cache.loader - used to fetch items not in the cache.public LruCache(int cacheSize,
int concurrency,
IValueLoader<K,V> loader)
cacheSize - The number of items to store in the cache.loader - used to fetch items not in the cache.public void setCacheLoader(IValueLoader<K,V> loader)
public long getCacheSize()
getCacheSize in interface ICache<K,V>public long getCacheMisses()
ICachegetCacheMisses in interface ICache<K,V>public long getCacheRequests()
ICachegetCacheRequests in interface ICache<K,V>public double getPercentageMisses()
ICachegetPercentageMisses in interface ICache<K,V>public V get(K key)
get in interface ICache<K,V>key - or the item required.IllegalStateException - if there was a problem accessing data file.public V get(K key, IValueLoader<K,V> loader) throws IOException
get in interface ILoadingCache<K,V>key - or the item requiredloader - to fetch the items fromIOException - if there was a problem accessing data file.public void resetCache()
resetCache in interface ICache<K,V>public static ICacheBuilder builder()
Copyright © 2017 51Degrees. All rights reserved.