Methods Summary |
---|
public void | delete()Deletes the cache and its underlying file.
cache = new Properties();
cachefile.delete();
cacheLoaded = true;
cacheDirty = false;
|
public java.lang.Object | get(java.lang.Object key)Returns a value for a given key from the cache.
if (!cacheLoaded) {
load();
}
try {
return cache.getProperty(String.valueOf(key));
} catch (ClassCastException e) {
return null;
}
|
public java.io.File | getCachefile()Getter.
return cachefile;
|
public boolean | isValid()This cache is valid if the cachefile is set.
return (cachefile != null);
|
public java.util.Iterator | iterator()Returns an iterator over the keys in the cache.
Vector v = new java.util.Vector();
Enumeration en = cache.propertyNames();
while (en.hasMoreElements()) {
v.add(en.nextElement());
}
return v.iterator();
|
public void | load()Load the cache from underlying properties file.
if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) {
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(cachefile));
cache.load(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// after loading the cache is up to date with the file
cacheLoaded = true;
cacheDirty = false;
|
public void | put(java.lang.Object key, java.lang.Object value)Saves a key-value-pair in the cache.
cache.put(String.valueOf(key), String.valueOf(value));
cacheDirty = true;
|
public void | save()Saves modification of the cache.
Cache is only saved if there is one ore more entries.
Because entries can not be deleted by this API, this Cache
implementation checks the existence of entries before creating the file
for performance optimisation.
if (!cacheDirty) {
return;
}
if ((cachefile != null) && cache.propertyNames().hasMoreElements()) {
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(cachefile));
cache.store(bos, null);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
cacheDirty = false;
|
public void | setCachefile(java.io.File file)Setter.
cachefile = file;
|
public java.lang.String | toString()Override Object.toString().
StringBuffer buf = new StringBuffer();
buf.append("<PropertiesfileCache:");
buf.append("cachefile=").append(cachefile);
buf.append(";noOfEntries=").append(cache.size());
buf.append(">");
return buf.toString();
|