Class DefaultRedis

java.lang.Object
dev.voidframework.redis.impl.DefaultRedis
All Implemented Interfaces:
Redis

public class DefaultRedis extends Object implements Redis
Implementation of Redis.
Since:
1.1.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultRedis(com.google.inject.Provider<redis.clients.jedis.Jedis> jedisProvider, com.typesafe.config.Config configuration)
    Build a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> long
    addInList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Object value)
    Adds a value in a list.
    <T> long
    addInList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Object value, int maxItem)
    Adds a value in a list.
    long
    addInList(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value)
    Adds a value in a list.
    long
    addInList(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value, int maxItem)
    Adds a value in a list.
    <T> long
    addInList(String key, Class<T> clazz, T value)
    Adds a value in a list.
    <T> long
    addInList(String key, Class<T> clazz, T value, int maxItem)
    Adds a value in a list.
    long
    Decrements a number value.
    long
    decrement(String key, int expiration)
    Decrements a number value.
    boolean
    Checks if key is present on Redis database.
    <T> T
    get(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
    Retrieves an object by key.
    <T> T
    get(String key, com.fasterxml.jackson.databind.JavaType javaType)
    Retrieves an object by key.
    <T> T
    get(String key, Class<T> clazz)
    Retrieves an object by key.
    redis.clients.jedis.Jedis
    Gets a Redis connection from the pool.
    redis.clients.jedis.Jedis
    getConnection(int db)
    Gets a Redis connection from the pool pre-configured with the right database.
    <T> List<T>
    getFromList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
    Gets values from a list.
    <T> List<T>
    getFromList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, int offset, int count)
    Gets values from a list.
    <T> List<T>
    getFromList(String key, com.fasterxml.jackson.databind.JavaType javaType)
    Gets values from a list.
    <T> List<T>
    getFromList(String key, com.fasterxml.jackson.databind.JavaType javaType, int offset, int count)
    Gets values from a list.
    <T> List<T>
    getFromList(String key, Class<T> clazz)
    Gets values from a list.
    <T> List<T>
    getFromList(String key, Class<T> clazz, int offset, int count)
    Gets values from a list.
    <T> T
    getOrElse(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Callable<T> block)
    Retrieves a value from the cache, or set it from a default Callable function.
    <T> T
    getOrElse(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Callable<T> block, int expiration)
    Retrieves a value from the cache, or set it from a default Callable function.
    <T> T
    getOrElse(String key, com.fasterxml.jackson.databind.JavaType javaType, Callable<T> block)
    Retrieves a value from the cache, or set it from a default Callable function.
    <T> T
    getOrElse(String key, com.fasterxml.jackson.databind.JavaType javaType, Callable<T> block, int expiration)
    Retrieves a value from the cache, or set it from a default Callable function.
    <T> T
    getOrElse(String key, Class<T> clazz, Callable<T> block)
    Retrieves a value from the cache, or set it from a default Callable function.
    <T> T
    getOrElse(String key, Class<T> clazz, Callable<T> block, int expiration)
    Retrieves a value from the cache, or set it from a default Callable function.
    long
    Increments an integer value.
    long
    increment(String key, int expiration)
    Increments a number value.
    void
    Removes a value from the cache.
    void
    remove(String... keys)
    Removes a value from the cache.
    <T> void
    set(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, T value)
    Sets a value without expiration.
    <T> void
    set(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, T value, int expiration)
    Sets a value with expiration.
    void
    set(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value)
    Sets a value without expiration.
    void
    set(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value, int expiration)
    Sets a value with expiration.
    <T> void
    set(String key, Class<T> clazz, T value)
    Sets a value without expiration.
    <T> void
    set(String key, Class<T> clazz, T value, int expiration)
    Sets a value with expiration.
    boolean
    tryLock(String key, int expiration)
    Tries to acquire a lock.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DefaultRedis

      @Inject public DefaultRedis(com.google.inject.Provider<redis.clients.jedis.Jedis> jedisProvider, com.typesafe.config.Config configuration)
      Build a new instance.
      Parameters:
      jedisProvider - The Jedis resource provider
      configuration - The application configuration
      Since:
      1.1.0
  • Method Details

    • getConnection

      public redis.clients.jedis.Jedis getConnection()
      Description copied from interface: Redis
      Gets a Redis connection from the pool.
      Specified by:
      getConnection in interface Redis
      Returns:
      A Redis connection
    • getConnection

      public redis.clients.jedis.Jedis getConnection(int db)
      Description copied from interface: Redis
      Gets a Redis connection from the pool pre-configured with the right database. If the database number is under zero, the database "zero" will be selected.
      Specified by:
      getConnection in interface Redis
      Parameters:
      db - The database number to use
      Returns:
      A Redis connection
    • get

      public <T> T get(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      Description copied from interface: Redis
      Retrieves an object by key.
      Specified by:
      get in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      typeReference - The object type reference
      Returns:
      object or null
    • get

      public <T> T get(String key, Class<T> clazz)
      Description copied from interface: Redis
      Retrieves an object by key.
      Specified by:
      get in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      clazz - The object class
      Returns:
      object or null
    • get

      public <T> T get(String key, com.fasterxml.jackson.databind.JavaType javaType)
      Description copied from interface: Redis
      Retrieves an object by key.
      Specified by:
      get in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      javaType - The object java type
      Returns:
      object or null
    • set

      public <T> void set(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, T value)
      Description copied from interface: Redis
      Sets a value without expiration.
      Specified by:
      set in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      typeReference - The object type reference
      value - The value to set
    • set

      public <T> void set(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, T value, int expiration)
      Description copied from interface: Redis
      Sets a value with expiration.
      Specified by:
      set in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      typeReference - The object type reference
      value - The value to set
      expiration - expiration in seconds
    • set

      public <T> void set(String key, Class<T> clazz, T value)
      Description copied from interface: Redis
      Sets a value without expiration.
      Specified by:
      set in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      clazz - The object class
      value - The value to set
    • set

      public <T> void set(String key, Class<T> clazz, T value, int expiration)
      Description copied from interface: Redis
      Sets a value with expiration.
      Specified by:
      set in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      clazz - The object class
      value - The value to set
      expiration - expiration in seconds
    • set

      public void set(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value)
      Description copied from interface: Redis
      Sets a value without expiration.
      Specified by:
      set in interface Redis
      Parameters:
      key - Item key
      javaType - The object java type
      value - The value to set
    • set

      public void set(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value, int expiration)
      Description copied from interface: Redis
      Sets a value with expiration.
      Specified by:
      set in interface Redis
      Parameters:
      key - Item key
      javaType - The object java type
      value - The value to set
      expiration - expiration in seconds
    • getOrElse

      public <T> T getOrElse(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Callable<T> block)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function. The value has no expiration.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      typeReference - The object type reference
      block - block returning value to set if key does not exist
      Returns:
      value
    • getOrElse

      public <T> T getOrElse(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Callable<T> block, int expiration)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - Item key
      typeReference - The object type reference
      block - block returning value to set if key does not exist
      expiration - expiration period in seconds
      Returns:
      value
    • getOrElse

      public <T> T getOrElse(String key, Class<T> clazz, Callable<T> block)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function. The value has no expiration.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      clazz - The object class
      block - block returning value to set if key does not exist
      Returns:
      value
    • getOrElse

      public <T> T getOrElse(String key, Class<T> clazz, Callable<T> block, int expiration)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - Item key
      clazz - The object class
      block - block returning value to set if key does not exist
      expiration - expiration period in seconds
      Returns:
      value
    • getOrElse

      public <T> T getOrElse(String key, com.fasterxml.jackson.databind.JavaType javaType, Callable<T> block)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function. The value has no expiration.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something
      Parameters:
      key - Item key
      javaType - The object java type
      block - block returning value to set if key does not exist
      Returns:
      value
    • getOrElse

      public <T> T getOrElse(String key, com.fasterxml.jackson.databind.JavaType javaType, Callable<T> block, int expiration)
      Description copied from interface: Redis
      Retrieves a value from the cache, or set it from a default Callable function.
      Specified by:
      getOrElse in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - Item key
      javaType - The object java type
      block - block returning value to set if key does not exist
      expiration - expiration period in seconds
      Returns:
      value
    • remove

      public void remove(String key)
      Description copied from interface: Redis
      Removes a value from the cache.
      Specified by:
      remove in interface Redis
      Parameters:
      key - The key to remove the value for
    • remove

      public void remove(String... keys)
      Description copied from interface: Redis
      Removes a value from the cache.
      Specified by:
      remove in interface Redis
      Parameters:
      keys - Keys to remove from redis
    • exists

      public boolean exists(String key)
      Description copied from interface: Redis
      Checks if key is present on Redis database.
      Specified by:
      exists in interface Redis
      Parameters:
      key - The key to test
      Returns:
      true if the key is present on Redis database
    • addInList

      public <T> long addInList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Object value)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      typeReference - The object type reference
      value - The value to add in the list
      Returns:
      The list size
    • addInList

      public <T> long addInList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Object value, int maxItem)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      typeReference - The object type reference
      value - The value to add in the list
      maxItem - The number of entries to keep in list
      Returns:
      The list size
    • addInList

      public <T> long addInList(String key, Class<T> clazz, T value)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      clazz - The object class
      value - The value to add in the list
      Returns:
      The list size
    • addInList

      public <T> long addInList(String key, Class<T> clazz, T value, int maxItem)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      clazz - The object class
      value - The value to add in the list
      maxItem - The number of entries to keep in list
      Returns:
      The list size
    • addInList

      public long addInList(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Parameters:
      key - The list key
      javaType - The object java type
      value - The value to add in the list
      Returns:
      The list size
    • addInList

      public long addInList(String key, com.fasterxml.jackson.databind.JavaType javaType, Object value, int maxItem)
      Description copied from interface: Redis
      Adds a value in a list.
      Specified by:
      addInList in interface Redis
      Parameters:
      key - The list key
      javaType - The object java type
      value - The value to add in the list
      maxItem - The number of entries to keep in list
      Returns:
      The list size
    • getFromList

      public <T> List<T> getFromList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      typeReference - The object type reference
      Returns:
      The values list
    • getFromList

      public <T> List<T> getFromList(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, int offset, int count)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      typeReference - The object type reference
      offset - From where
      count - The number of items to retrieve
      Returns:
      The values list
    • getFromList

      public <T> List<T> getFromList(String key, Class<T> clazz)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      clazz - The object class
      Returns:
      The values list
    • getFromList

      public <T> List<T> getFromList(String key, Class<T> clazz, int offset, int count)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      clazz - The object class
      offset - From where
      count - The number of items to retrieve
      Returns:
      The values list
    • getFromList

      public <T> List<T> getFromList(String key, com.fasterxml.jackson.databind.JavaType javaType)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      javaType - The object java type
      Returns:
      The values list
    • getFromList

      public <T> List<T> getFromList(String key, com.fasterxml.jackson.databind.JavaType javaType, int offset, int count)
      Description copied from interface: Redis
      Gets values from a list.
      Specified by:
      getFromList in interface Redis
      Type Parameters:
      T - Generic type of something implementing java.io.Serializable
      Parameters:
      key - The list key
      javaType - The object java type
      offset - From where
      count - The number of items to retrieve
      Returns:
      The values list
    • tryLock

      public boolean tryLock(String key, int expiration)
      Description copied from interface: Redis
      Tries to acquire a lock. This method will return false if it can't acquire lock or can't connect to Redis server.
      Specified by:
      tryLock in interface Redis
      Parameters:
      key - The lock key
      expiration - The lock TTL
      Returns:
      true in case of success, otherwise, false
    • decrement

      public long decrement(String key, int expiration)
      Description copied from interface: Redis
      Decrements a number value. If key does not exist, it will be created automatically. Expiration is set only when the key is created.
      Specified by:
      decrement in interface Redis
      Parameters:
      key - The value key
      expiration - The value TTL
      Returns:
      The decremented value
    • decrement

      public long decrement(String key)
      Description copied from interface: Redis
      Decrements a number value. If key does not exist, it will be created automatically.
      Specified by:
      decrement in interface Redis
      Parameters:
      key - The value key
      Returns:
      The decremented value
    • increment

      public long increment(String key)
      Description copied from interface: Redis
      Increments an integer value. If key does not exist, it will be created automatically.
      Specified by:
      increment in interface Redis
      Parameters:
      key - The value key
      Returns:
      The incremented value
    • increment

      public long increment(String key, int expiration)
      Description copied from interface: Redis
      Increments a number value. If key does not exist, it will be created automatically. Expiration is set only when the key is created.
      Specified by:
      increment in interface Redis
      Parameters:
      key - The value key
      expiration - The value TTL
      Returns:
      The incremented value