fiftyone.mobile.detection.entities.stream
Class Cache<T>

java.lang.Object
  extended by fiftyone.mobile.detection.entities.stream.Cache<T>
All Implemented Interfaces:
Disposable

public class Cache<T>
extends Object
implements Disposable

Many of the entities used by the detector data set are requested repeatedly. The cache improves memory usage and reduces strain on the garbage collector by storing previously requested entities for a short period of time to avoid the need to re-fetch them from the underlying storage mechanism.

The cache works by maintaining two dictionaries of entities keyed on their offset or index. The inactive list contains all items requested since the cache was created or last serviced. The active list contains all the items currently in the cache. The inactive list is always updated when an item is requested.

When the cache is serviced the active list is destroyed and the inactive list becomes the active list. i.e. all the items that were requested since the cache was last serviced are now in the cache. A new inactive list is created to store all items being requested since the cache was last serviced.


Nested Class Summary
(package private) static interface Cache.EntityCreator<T>
           
 
Field Summary
private  Timer cacheServiceTimer
           
(package private)  BaseEntityFactory<T> entityFactory
           
(package private)  ConcurrentHashMap<Integer,T> itemsActive
           
(package private)  ConcurrentHashMap<Integer,T> itemsInactive
           
(package private)  AtomicInteger misses
           
(package private)  AtomicInteger requests
           
 
Constructor Summary
Cache(BaseEntityFactory<T> creator)
          Constructs a new instance of the cache.
 
Method Summary
(package private)  void addRecent(int index, T item)
           
 void dispose()
           
(package private)  double getPercentageMisses()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

itemsActive

ConcurrentHashMap<Integer,T> itemsActive

itemsInactive

ConcurrentHashMap<Integer,T> itemsInactive

cacheServiceTimer

private final Timer cacheServiceTimer

requests

AtomicInteger requests

misses

AtomicInteger misses

entityFactory

final BaseEntityFactory<T> entityFactory
Constructor Detail

Cache

Cache(BaseEntityFactory<T> creator)
Constructs a new instance of the cache.

Parameters:
serviceCacheInternal - Time between cache services
Method Detail

addRecent

void addRecent(int index,
               T item)

getPercentageMisses

double getPercentageMisses()

dispose

public void dispose()
Specified by:
dispose in interface Disposable