Class CacheCustomContent<T extends Throwable>

    • Field Detail

      • INTERVAL_HOURLY

        public static final int INTERVAL_HOURLY
        Update interval meaning an update is needed each hour
      • INTERVAL_DAILY

        public static final int INTERVAL_DAILY
        Update interval meaning an update is needed each day
      • INTERVAL_WEEKLY

        public static final int INTERVAL_WEEKLY
        Update interval meaning an update is needed each week
      • INTERVAL_MONTHLY

        public static final int INTERVAL_MONTHLY
        Update interval meaning an update is needed each month
      • data

        private byte[] data
        Where the data will be stored
      • ident

        private final String ident
        The ident that identifies the stored file. Includes file-ending.
      • path

        private final File path
        The (file-)path where the data will be stored
      • updateInterval

        private final int updateInterval
        How often to update the cached version
    • Constructor Detail

      • CacheCustomContent

        protected CacheCustomContent​(String ident,
                                     int updateInterval)
        Initializes the class. Note that all read data will be stored in memory until it is flushed by flushData().
        Parameters:
        ident - ident that identifies the stored file. Includes file-ending.
        updateInterval - update interval in seconds. -1 means always
    • Method Detail

      • updateData

        protected abstract byte[] updateData()
                                      throws T extends Throwable
        This function will be executed when an update is required. It has to be implemented by the inheriting class and should use a worker if it has a long wall time as the function is executed in the current thread.
        Returns:
        the data to cache
        Throws:
        T - a Throwable
        T extends Throwable
      • isCacheValid

        protected boolean isCacheValid()
        This function serves as a comfort hook to perform additional checks if the cache is valid
        Returns:
        True if the cached copy is still valid
      • isOffline

        protected abstract boolean isOffline()
        Checks underlying resource is not accessed in offline mode.
        Returns:
        whether resource is accessed in offline mode
      • updateForce

        private byte[] updateForce()
                            throws T extends Throwable
        Executes an update regardless of updateInterval
        Returns:
        Returns the data
        Throws:
        T - if an error occurs
        T extends Throwable
      • getData

        public byte[] getData()
                       throws T extends Throwable
        Returns the data without performing any updates
        Returns:
        the data
        Throws:
        T - if an error occurs
        T extends Throwable
      • getDataString

        public String getDataString()
                             throws T extends Throwable
        Returns the data without performing any updates
        Returns:
        the data as String
        Throws:
        T - if an error occurs
        T extends Throwable
      • loadFromDisk

        private void loadFromDisk()
                           throws T extends Throwable
        Tries to load the data using the given ident from disk. If this fails, data will be updated, unless run in offline mode
        Throws:
        T - a Throwable
        T extends Throwable
      • saveToDisk

        private void saveToDisk()
        Stores the data to disk
      • flushData

        public void flushData()
        Flushes the data from memory. Class automatically reloads it from disk or updateData() if required