Package ninja.cache

Interface Cache

  • All Known Implementing Classes:
    CacheEhCacheImpl, CacheMemcachedImpl

    public interface Cache
    Interface hiding cache implementation. Inject that interface into the methods where you want to use it and you are ready to go. @Inject Cache cache. Heavily inspired by excellent Play! 1.2.5 implementation.
    • Method Detail

      • add

        void add​(String key,
                 Object value,
                 int expirationInSeconds)
        Add object to cache. Important: If the key exists already it won't be overwritten. Use set(...) instead. This method is fire and forget. Implementations may implement this async and non-blocking.
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to add to the cache.
        expirationInSeconds - Expiration time in seconds.
      • safeAdd

        boolean safeAdd​(String key,
                        Object value,
                        int expirationInSeconds)
        Similar to delete method. BUT it blocks until execution succeeds / fails AND wraps exceptions in a simple true / false return value
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to add in the cache.
        expirationInSeconds - Expiration time in seconds.
        Returns:
        true if add was successful, false if not.
      • set

        void set​(String key,
                 Object value,
                 int expirationInSeconds)
        Adds object of cache. Important: This method potentially overwrites previously existing object with new values. This method is fire and forget. Implementations may implement this async and non-blocking.
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to set in the cache.
        expirationInSeconds - Expiration time in seconds.
      • safeSet

        boolean safeSet​(String key,
                        Object value,
                        int expirationInSeconds)
        Similar to delete method. BUT it blocks until execution succeeds / fails AND wraps exceptions in a simple true / false return value
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to set in the cache.
        expirationInSeconds - Expiration time in seconds.
        Returns:
        true if set was successful, false if not.
      • replace

        void replace​(String key,
                     Object value,
                     int expirationInSeconds)
        Replaces key with new value. Important: Will do nothing if the key does not exist. This method is fire and forget. Implementations may implement this async and non-blocking.
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to replace in the cache.
        expirationInSeconds - Expiration time in seconds.
      • safeReplace

        boolean safeReplace​(String key,
                            Object value,
                            int expirationInSeconds)
        Similar to delete method. BUT it blocks until execution succeeds / fails AND wraps exceptions in a simple true / false return value
        Parameters:
        key - The key of the object to use for caching.
        value - The value of the object to replace in the cache.
        expirationInSeconds - Expiration time in seconds.
        Returns:
        true if replace was successful, false if not.
      • get

        Object get​(String key)
        Returns the object for this key or null if not found.
        Parameters:
        key - The key of the object to retrieve.
        Returns:
        The object of the key or null if not found.
      • get

        Map<String,​Object> get​(String[] keys)
        Returns all objects for the keys.
        Parameters:
        keys - The list of keys to retrieve from Cache.
        Returns:
        A map with key - object pairs that were found in cache.
      • incr

        long incr​(String key,
                  int by)
        Increments key by value.
        Parameters:
        key - The key to increment
        by - Value by which to increment.
        Returns:
        New value of the key or -1 if key does not exist.
      • decr

        long decr​(String key,
                  int by)
        Decrements key by value.
        Parameters:
        key - The key to decrement
        by - Value by which to decrement.
        Returns:
        New value of the key or -1 if key does not exist.
      • clear

        void clear()
        Clear all values in cache.
      • delete

        void delete​(String key)
        Delete key from cache.
        Parameters:
        key - The key to delete.
      • safeDelete

        boolean safeDelete​(String key)
        Similar to delete method. BUT it blocks until execution succeeds / fails AND wraps exceptions in a simple true / false return value
        Parameters:
        key - The key to delete
        Returns:
        true if deletion was successful, false if not.