In previous articles we have already seen, how to setup Redis Standalone Server in Ubuntu. In this particular article we will setup Spring Data with Standalone Redis Using Jedis Client.

Make sure you have a running Redis standalone server, host: 127.0.0.1, port: 6379. Click here to know how to setup and run a standaone redis server on ubuntu.

Dependencies Required


	<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>${jedis.version}</version>
	</dependency>
	<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>${spring.data.redis.version}</version>
	</dependency>

Redis Configuration (redis-config.xml)


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:ehcache="http://www.springframework.org/schema/cache"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
              http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cacheDashboardAudienceRedisClusterService.xsd">

	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="127.0.0.1"></property>
		<property name="port" value="6379"></property>
		<property name="usePool" value="true" />
	</bean>

	<bean id="stringSerializer"
		class="org.springframework.data.redis.serializer.StringRedisSerializer" />

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="keySerializer" ref="stringSerializer" />
		<property name="valueSerializer" ref="stringSerializer" />
	</bean>
</beans>

Java code to perform common Operations

JedisService.java

package com.tb.services;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public interface JedisService {
	public void save(String key, String value);

	public void save(String key, String value, long expiryInSeconds);

	public int append(String key, String valueToAppend);

	public boolean exists(String key);

	public Object get(String key);

	public void delete(String key);

	public long getExpireDuration(String key);
	
	public Set<String> getKeysForAPattern(String pattern);
	
	public List<Object> multiGet(Collection<String> keys);
	
	public void multiSet(Map<String,Object> keyValuePair);
	
	public boolean removeExpireDuration(String key);

}


JedisServiceImpl.java

package com.tb.services;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class JedisServiceImpl implements JedisService {

	@Autowired
	private RedisTemplate<String, Object> template;

	// Save String key value
	@Override
	public void save(String key, String value) {
		template.opsForValue().set(key, value);
	}

	// Save String key value with expire time in seconds
	@Override
	public void save(String key, String value, long expiryInSeconds) {
		template.opsForValue().set(key, value);
		template.expire(key, expiryInSeconds, TimeUnit.SECONDS);
	}

	// Append a string value to existing key
	@Override
	public int append(String key, String valueToAppend) {
		int added = 0;
		if (template.hasKey(key))
			added = template.opsForValue().append(key, valueToAppend);
		return added;
	}

	// Check if a key exists
	@Override
	public boolean exists(String key) {
		return template.hasKey(key);
	}

	// Get String key value
	@Override
	public Object get(String key) {
		return template.opsForValue().get(key);
	}

	// Delete a key
	@Override
	public void delete(String key) {
		template.delete(key);
	}

	// Get expire duration of a key in second
	@Override
	public long getExpireDuration(String key) {
		return template.getExpire(key, TimeUnit.SECONDS);

	}

	// Get keys of a pettern
	@Override
	public Set<String> getKeysForAPattern(String pattern) {
		return template.keys(pattern + "*");
	}

	// Get multiple values by multiple keys
	@Override
	public List<Object> multiGet(Collection<String> keys) {
		return template.opsForValue().multiGet(keys);
	}

	// Set multiple values by multiple keys
	@Override
	public void multiSet(Map<String, Object> keyValuePair) {
		template.opsForValue().multiSet(keyValuePair);
	}

	// Set expire duration of a key
	@Override
	public boolean removeExpireDuration(String key) {
		return template.persist(key);
	}

}

In this article we have seen how to setup Spring Data with Standalone Redis Using Jedis Client, in upcoming articles we will see more about Spring and Redis integration.
  • By Techburps.com
  • Sep 1, 2017
  • Big Data