Package org.openhab.core.cache
Class ByteArrayFileCache
java.lang.Object
org.openhab.core.cache.ByteArrayFileCache
This is a simple file based cache implementation. It is not thread-safe.
- Author:
- Christoph Weitkamp - Initial contribution
-
Constructor Summary
ConstructorDescriptionByteArrayFileCache
(String servicePID) Creates a newByteArrayFileCache
instance for a service.ByteArrayFileCache
(String servicePID, Duration expiry) Creates a newByteArrayFileCache
instance for a service. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all files from the cache.void
Removes expired files from the cache.boolean
containsKey
(String key) Checks if the key is present in the cache.byte[]
Returns the content of the file associated with the given key, if it is present.void
Adds a file to the cache.void
putIfAbsent
(String key, byte[] content) Adds a file to the cache.byte[]
putIfAbsentAndGet
(String key, byte[] content) Adds a file to the cache and returns the content of the file.void
Removes the file associated with the given key from the cache.
-
Constructor Details
-
ByteArrayFileCache
Creates a newByteArrayFileCache
instance for a service. Creates acache
folder under$OPENHAB_USERDATA/cache/$servicePID
.- Parameters:
servicePID
- PID of the service
-
ByteArrayFileCache
Creates a newByteArrayFileCache
instance for a service. Creates acache
folder under$OPENHAB_USERDATA/cache/$servicePID/
.- Parameters:
servicePID
- PID of the serviceexpiry
- the duration for how long the files stay valid in the cache. Must be positive. 0 to disable this functionality.
-
-
Method Details
-
put
Adds a file to the cache. If the cache previously contained a file for the key, the old file is replaced by the new content.- Parameters:
key
- the key with which the file is to be associatedcontent
- the content for the file to be associated with the specified key
-
putIfAbsent
Adds a file to the cache.- Parameters:
key
- the key with which the file is to be associatedcontent
- the content for the file to be associated with the specified key
-
putIfAbsentAndGet
Adds a file to the cache and returns the content of the file.- Parameters:
key
- the key with which the file is to be associatedcontent
- the content for the file to be associated with the specified key- Returns:
- the content of the file associated with the given key
- Throws:
IOException
- if an I/O error occurs reading the given file
-
containsKey
Checks if the key is present in the cache.- Parameters:
key
- the key whose presence in the cache is to be tested- Returns:
- true if the cache contains a file for the specified key
-
remove
Removes the file associated with the given key from the cache.- Parameters:
key
- the key whose associated file is to be removed
-
clear
public void clear()Removes all files from the cache. -
clearExpired
public void clearExpired()Removes expired files from the cache. -
get
Returns the content of the file associated with the given key, if it is present.- Parameters:
key
- the key whose associated file is to be returned- Returns:
- the content of the file associated with the given key
- Throws:
FileNotFoundException
- if the given file could not be found in cacheIOException
- if an I/O error occurs reading the given file
-