info.sswap.impl.empire.io
Class ModelCache

java.lang.Object
  extended by info.sswap.impl.empire.io.ModelCache
All Implemented Interfaces:
Cache

public class ModelCache
extends java.lang.Object
implements Cache

A cache for Jena models. This class is thread safe and can be used by concurrently downloading threads.

Author:
Blazej Bulka

Nested Class Summary
(package private) static class ModelCache.MemoryCacheEntry
           
 
Field Summary
private  java.util.Map<java.lang.String,java.lang.ref.SoftReference<ModelCache.MemoryCacheEntry>> cache
          Map of model URIs to Jena Models
private  java.lang.String cacheDirName
          Cache directory
private  boolean cacheEnabled
          Flag whether any (memory or disk) caching is enabled
private  java.lang.String cacheIndexFile
          Cache index file
private static long defaultEntryTTL
          Default time-to-live (TTL).
private  boolean diskCacheEnabled
          Flag whether persistent cache is enabled or disabled.
private  long entryTTL
          The time-to-live (TTL) for entries in ms.
private static java.lang.String FILE_PREFIX
           
private  java.util.Properties locationMap
          Cache contents mapping URIs to local file paths.
private static org.apache.log4j.Logger LOGGER
           
private  long negativeEntryTTL
           
private  java.util.List<java.lang.String> negativeUriQueue
           
private  java.util.List<java.lang.String> positiveUriQueue
          The queue of URIs in the order they were added (new caches are always added at the end, and the expired caches are removed from the front).
private  java.util.Map<java.lang.String,java.lang.Long> storageNegativeTimes
           
private  java.util.Map<java.lang.String,java.lang.Long> storagePositiveTimes
          Map of model URIs to the time when they were stored in this cache (to properly detect expired caches).
 
Constructor Summary
ModelCache()
          Creates an empty cache
ModelCache(long entryTTL, long negativeEntryTTL)
          Creates an empty cache with a time-to-live TTL.
ModelCache(long entryTTL, long negativeEntryTTL, boolean diskCacheEnabled)
           
 
Method Summary
 void clear()
          Removes all entries from the cache.
private  void clearDisk()
           
private  void clearMemory()
           
 boolean containsModel(java.lang.String uri)
          Deprecated. The use of this method may cause problems with TTL. (It is possible that this method would report a model as existing in this cache, but then the model's TTL would be exceeded before getModel() is called, which would cause getModel() to return null)
private static long getConfigEntryTTL()
           
private static long getConfigNegativeEntryTTL()
           
 java.io.File getDirectory()
          Gets the directory where the files are stored in the cache.
private  java.util.Properties getLocationMap()
           
 com.hp.hpl.jena.rdf.model.Model getModel(java.lang.String uri)
          Gets a copy of a model that is already in the cache.
private  com.hp.hpl.jena.rdf.model.Model getModelFromDisk(java.lang.String uri)
           
private  com.hp.hpl.jena.rdf.model.Model getModelFromMemory(java.lang.String uri)
           
 long getNegativeTimeToLive()
           
 long getTimeToLive()
          Gets the time-to-live (TTL) for entries.
private  void initDiskCache()
           
private  boolean isDiskEntryExpired(java.lang.String fileURL)
           
private  boolean isExpired(java.lang.String uri, boolean negative)
          Checks whether an entry is expired.
private  void removeExpiredData()
          Removes all entries that are expired
private  void removeExpiredData(boolean negative)
           
 void setAsInaccessible(java.lang.String uri)
           
 void setModel(java.lang.String uri, com.hp.hpl.jena.rdf.model.Model model)
          Stores a copy of a model in the cache.
private  void setModel(java.lang.String uri, com.hp.hpl.jena.rdf.model.Model model, boolean negative)
           
 void setNegativeTimeToLive(long negativeEntryTTL)
           
 void setTimeToLive(long entryTTL)
          Sets the time-to-live (in milliseconds) for entries in this cache.
private  void storeInMemory(java.lang.String uri, com.hp.hpl.jena.rdf.model.Model model, boolean negative)
           
private  void storeOnDisk(java.lang.String uri, com.hp.hpl.jena.rdf.model.Model model)
           
private static java.io.File toFile(java.lang.String fileURL)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final org.apache.log4j.Logger LOGGER

FILE_PREFIX

private static final java.lang.String FILE_PREFIX
See Also:
Constant Field Values

cacheDirName

private java.lang.String cacheDirName
Cache directory


cacheIndexFile

private java.lang.String cacheIndexFile
Cache index file


locationMap

private java.util.Properties locationMap
Cache contents mapping URIs to local file paths.


cache

private java.util.Map<java.lang.String,java.lang.ref.SoftReference<ModelCache.MemoryCacheEntry>> cache
Map of model URIs to Jena Models


storagePositiveTimes

private java.util.Map<java.lang.String,java.lang.Long> storagePositiveTimes
Map of model URIs to the time when they were stored in this cache (to properly detect expired caches).


storageNegativeTimes

private java.util.Map<java.lang.String,java.lang.Long> storageNegativeTimes

positiveUriQueue

private java.util.List<java.lang.String> positiveUriQueue
The queue of URIs in the order they were added (new caches are always added at the end, and the expired caches are removed from the front).


negativeUriQueue

private java.util.List<java.lang.String> negativeUriQueue

defaultEntryTTL

private static final long defaultEntryTTL
Default time-to-live (TTL).

See Also:
entryTTL, Constant Field Values

entryTTL

private long entryTTL
The time-to-live (TTL) for entries in ms. After this time passes since the time an entry was stored in the cache, it is considered invalid, and no longer reported as being in the cache.


negativeEntryTTL

private long negativeEntryTTL

diskCacheEnabled

private boolean diskCacheEnabled
Flag whether persistent cache is enabled or disabled.


cacheEnabled

private boolean cacheEnabled
Flag whether any (memory or disk) caching is enabled

Constructor Detail

ModelCache

public ModelCache()
Creates an empty cache


ModelCache

public ModelCache(long entryTTL,
                  long negativeEntryTTL)
Creates an empty cache with a time-to-live TTL. A negative value resets to the default.

Parameters:
entryTTL - time-to-live for a URI cache, in milliseconds, before it is considered stale and removed from the cache
See Also:
getTimeToLive()

ModelCache

public ModelCache(long entryTTL,
                  long negativeEntryTTL,
                  boolean diskCacheEnabled)
Method Detail

initDiskCache

private void initDiskCache()

getLocationMap

private java.util.Properties getLocationMap()

getTimeToLive

public long getTimeToLive()
Gets the time-to-live (TTL) for entries. After this time passes since an entry was stored in the cache, it is considered invalid, and will be replaced, if possible, by a fresh network call.

Specified by:
getTimeToLive in interface Cache
Returns:
TTL in ms

setTimeToLive

public void setTimeToLive(long entryTTL)
Sets the time-to-live (in milliseconds) for entries in this cache. A negative value resets to the default.

Specified by:
setTimeToLive in interface Cache
Parameters:
entryTTL - the new TTL in ms

getNegativeTimeToLive

public long getNegativeTimeToLive()

setNegativeTimeToLive

public void setNegativeTimeToLive(long negativeEntryTTL)

toFile

private static java.io.File toFile(java.lang.String fileURL)

clearMemory

private void clearMemory()

clearDisk

private void clearDisk()

clear

public void clear()
Description copied from interface: Cache
Removes all entries from the cache.

Specified by:
clear in interface Cache

getDirectory

public java.io.File getDirectory()
Gets the directory where the files are stored in the cache.

Specified by:
getDirectory in interface Cache
Returns:
the file object for the directory

containsModel

public boolean containsModel(java.lang.String uri)
Deprecated. The use of this method may cause problems with TTL. (It is possible that this method would report a model as existing in this cache, but then the model's TTL would be exceeded before getModel() is called, which would cause getModel() to return null)

Checks whether the cache already contains model for this URI.

Parameters:
uri - the URI of the model
Returns:
true if the model exists, false otherwise; but see deprecation warnings

getModel

public com.hp.hpl.jena.rdf.model.Model getModel(java.lang.String uri)
Gets a copy of a model that is already in the cache. (This method returns a copy so that the caller may start modifying the model.)

Parameters:
uri - the URI of the model
Returns:
the copy of the cached model or null, if the model is not currently cached

getModelFromMemory

private com.hp.hpl.jena.rdf.model.Model getModelFromMemory(java.lang.String uri)

isDiskEntryExpired

private boolean isDiskEntryExpired(java.lang.String fileURL)

getModelFromDisk

private com.hp.hpl.jena.rdf.model.Model getModelFromDisk(java.lang.String uri)

setModel

public void setModel(java.lang.String uri,
                     com.hp.hpl.jena.rdf.model.Model model)
Stores a copy of a model in the cache. (The method copies the model so that the caller may still modify its model.)

Parameters:
uri - the URI of the model
model - the model whose copy will be cached.

setModel

private void setModel(java.lang.String uri,
                      com.hp.hpl.jena.rdf.model.Model model,
                      boolean negative)

setAsInaccessible

public void setAsInaccessible(java.lang.String uri)

storeInMemory

private void storeInMemory(java.lang.String uri,
                           com.hp.hpl.jena.rdf.model.Model model,
                           boolean negative)

storeOnDisk

private void storeOnDisk(java.lang.String uri,
                         com.hp.hpl.jena.rdf.model.Model model)

isExpired

private boolean isExpired(java.lang.String uri,
                          boolean negative)
Checks whether an entry is expired. If the entry does not exists, it is always treated as expired

Parameters:
uri - the URI of the entry to check for expiration
Returns:
true if the entry is expired (or does not exist at all), false otherwise

removeExpiredData

private void removeExpiredData()
Removes all entries that are expired


removeExpiredData

private void removeExpiredData(boolean negative)

getConfigEntryTTL

private static long getConfigEntryTTL()

getConfigNegativeEntryTTL

private static long getConfigNegativeEntryTTL()


Copyright (c) 2011, iPlant Collaborative, University of Arizona, Cold Spring Harbor Laboratories, University of Texas at Austin.