Class ByteArrayFileCache

java.lang.Object
org.openhab.core.cache.ByteArrayFileCache

@NonNullByDefault public class ByteArrayFileCache extends Object
This is a simple file based cache implementation. It is not thread-safe.
Author:
Christoph Weitkamp - Initial contribution
  • Constructor Details

    • ByteArrayFileCache

      public ByteArrayFileCache(String servicePID)
      Creates a new ByteArrayFileCache instance for a service. Creates a cache folder under $OPENHAB_USERDATA/cache/$servicePID.
      Parameters:
      servicePID - PID of the service
    • ByteArrayFileCache

      public ByteArrayFileCache(String servicePID, Duration expiry)
      Creates a new ByteArrayFileCache instance for a service. Creates a cache folder under $OPENHAB_USERDATA/cache/$servicePID/.
      Parameters:
      servicePID - PID of the service
      expiry - the duration for how long the files stay valid in the cache. Must be positive. 0 to disable this functionality.
  • Method Details

    • put

      public void put(String key, byte[] content)
      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 associated
      content - the content for the file to be associated with the specified key
    • putIfAbsent

      public void putIfAbsent(String key, byte[] content)
      Adds a file to the cache.
      Parameters:
      key - the key with which the file is to be associated
      content - the content for the file to be associated with the specified key
    • putIfAbsentAndGet

      public byte[] putIfAbsentAndGet(String key, byte[] content) throws IOException
      Adds a file to the cache and returns the content of the file.
      Parameters:
      key - the key with which the file is to be associated
      content - 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

      public boolean containsKey(String key)
      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

      public void remove(String key)
      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

      public byte[] get(String key) throws FileNotFoundException, IOException
      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 cache
      IOException - if an I/O error occurs reading the given file