Package ninja.cache

Class NinjaCache


  • public class NinjaCache
    extends Object
    A convenience class to access the underlying cache implementation. Makes getting and setting of objects a lot simpler. This class originates from Play 1.2.5's excellent cache implementation.
    Author:
    ra
    • Constructor Detail

      • NinjaCache

        @Inject
        public NinjaCache​(Cache cache)
    • Method Detail

      • add

        public void add​(String key,
                        Object value,
                        String expiration)
        Add an element only if it doesn't exist.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
      • safeAdd

        public boolean safeAdd​(String key,
                               Object value,
                               String expiration)
        Add an element only if it doesn't exist, and return only when the element is effectively cached.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
        Returns:
        If the element an eventually been cached
      • add

        public void add​(String key,
                        Object value)
        Add an element only if it doesn't exist and store it indefinitely.
        Parameters:
        key - Element key
        value - Element value
      • set

        public void set​(String key,
                        Object value,
                        String expiration)
        Set an element.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
      • safeSet

        public boolean safeSet​(String key,
                               Object value,
                               String expiration)
        Set an element and return only when the element is effectively cached.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
        Returns:
        If the element an eventually been cached
      • set

        public void set​(String key,
                        Object value)
        Set an element and store it indefinitely.
        Parameters:
        key - Element key
        value - Element value
      • replace

        public void replace​(String key,
                            Object value,
                            String expiration)
        Replace an element only if it already exists.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
      • safeReplace

        public boolean safeReplace​(String key,
                                   Object value,
                                   String expiration)
        Replace an element only if it already exists and return only when the element is effectively cached.
        Parameters:
        key - Element key
        value - Element value
        expiration - Ex: 10s, 3mn, 8h
        Returns:
        If the element an eventually been cached
      • replace

        public void replace​(String key,
                            Object value)
        Replace an element only if it already exists and store it indefinitely.
        Parameters:
        key - Element key
        value - Element value
      • incr

        public long incr​(String key,
                         int by)
        Increment the element value (must be a Number).
        Parameters:
        key - Element key
        by - The incr value
        Returns:
        The new value
      • incr

        public long incr​(String key)
        Increment the element value (must be a Number) by 1.
        Parameters:
        key - Element key
        Returns:
        The new value
      • decr

        public long decr​(String key,
                         int by)
        Decrement the element value (must be a Number).
        Parameters:
        key - Element key
        by - The decr value
        Returns:
        The new value
      • decr

        public long decr​(String key)
        Decrement the element value (must be a Number) by 1.
        Parameters:
        key - Element key
        Returns:
        The new value
      • get

        public Object get​(String key)
        Retrieve an object.
        Parameters:
        key - The element key
        Returns:
        The element value or null
      • get

        public Map<String,​Object> get​(String... key)
        Bulk retrieve.
        Parameters:
        key - List of keys
        Returns:
        Map of keys & values
      • delete

        public void delete​(String key)
        Delete an element from the cache.
        Parameters:
        key - The element key
      • safeDelete

        public boolean safeDelete​(String key)
        Delete an element from the cache and return only when the element is effectively removed.
        Parameters:
        key - The element key
        Returns:
        If the element an eventually been deleted
      • clear

        public void clear()
        Clear all data from cache.
      • get

        public <T> T get​(String key,
                         Class<T> clazz)
        Convenient clazz to get a value a class type;
        Type Parameters:
        T - The needed type
        Parameters:
        key - The element key
        clazz - The type class
        Returns:
        The element value or null