DEV Community

Cover image for How to use Redis with Spring in Java
juanc4milo
juanc4milo

Posted on • Edited on • Originally published at juanc4milo.dev

How to use Redis with Spring in Java

In a Java Project, you must include the following dependencies to use Redis database:

1.Use the Spring Data redis

with Maven:

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.3.4.RELEASE</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

with Gradle:

compile group: 'org.springframework.data', name: 'spring-data-redis', version: '2.3.4.RELEASE'

Enter fullscreen mode Exit fullscreen mode
2.Use Jedis as client connector

with Maven:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

with Gradle:

compile group: 'redis.clients', name: 'jedis', version: '3.3.0'
Enter fullscreen mode Exit fullscreen mode

After that, in your project create a Redis Context File to declare objects which define the connections settings to Redis server:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
          p:max-total="${redis.config.pool.maxTotal}" />

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.config.hostname}" 
          p:port="${redis.config.port}" 
          p:use-pool="true">
        <constructor-arg ref="jedisPoolConfig"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" 
          p:connection-factory-ref="jedisConnectionFactory" 
          p:enable-transaction-support="true"/>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" 
          p:connection-factory-ref="jedisConnectionFactory" 
          p:enable-transaction-support="true"/>  
</beans>
Enter fullscreen mode Exit fullscreen mode

Include some properties defined in the bean objects to the application.properties file:

#Redis Config
redis.config.hostname=127.0.0.1
redis.config.port=6379
redis.config.pool.maxTotal=5
Enter fullscreen mode Exit fullscreen mode

In your persistence context or main spring-context, inject the RedisTemplate bean into the DAO bean object that you want to use Redis cache, for example in a TransactionDao object:

Context:

<bean id="transactionDao" class="com.juanc4milo.persistence.dao.TransactionDao">
    <property name="redisTemplate" ref="redisTemplate"/>
</bean>
Enter fullscreen mode Exit fullscreen mode

TransactionDao class:

@Autowired
private RedisTemplate<String, Transaction> redisTemplate
Enter fullscreen mode Exit fullscreen mode

Then create an instance of the ValueOperations class to operate with the Redis database.

To set a value in database use the method SET from ValueOperations class, like:

valueOperation.set(<<key>>,<<value>>);
Enter fullscreen mode Exit fullscreen mode

The transaction ID will be the key in Redis database, and the entire object Transaction will be the value. After that, you could set the expiration time in seconds, minutes, hours, etc.

ValueOperations<String, Transaction> valueOperation = redisTemplate.opsForValue();
valueOperation.set(transaction.getId(), transaction);
redisTemplate.expire(transaction.getId(), 3600, TimeUnit.SECONDS);
Enter fullscreen mode Exit fullscreen mode

Redis allows cache of different types of values, such as:

  • Values
  • Sets
  • Sorted sets
  • Hashes
  • Lists

To get the data from Redis database, use the method GET from ValueOperations class, like:

valueOperation.get(<<key>>);
Enter fullscreen mode Exit fullscreen mode
ValueOperations<String, Transaction> valueOperation = redisTemplate.opsForValue();
Transaction tx = valueOperation.get(transactionId);
Enter fullscreen mode Exit fullscreen mode

Other articles of mine that you might like...

Do you like it? You can buy me a beer if you want.

Originally published at juanc4milo.dev

Top comments (0)