Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created July 27, 2015 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mp911de/65b2639dc344fb1cbb2f to your computer and use it in GitHub Desktop.
Save mp911de/65b2639dc344fb1cbb2f to your computer and use it in GitHub Desktop.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class Config {
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jcf = new JedisConnectionFactory();
jcf.setHostName("localhost");
jcf.setPort(6379);
jcf.afterPropertiesSet();
return jcf;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 27.07.15 12:31
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, Tester.class);
context.start();
Tester bean = context.getBean(Tester.class);
// TEST CODE STARTS HERE
bean.addSomeData();
bean.logHome();
// TEST CODE ENDS HERE
context.stop();
context.destroy();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>biz.paluch.redis</groupId>
<artifactId>spring-data-redis-serializer-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
</project>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Set;
@Service
public class Tester {
public final static String KEY = "key";
// inject the actual template
@Autowired
private RedisTemplate<String, Object> template;
// inject the template as SetOperations
@Resource(name = "redisTemplate")
private SetOperations<String, String> setOps;
public String logHome() {
Set<String> set = setOps.members(KEY);
for (String str : set) {
System.out.println(str); //EMPTY
}
Set<byte[]> keys = template.getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while (it.hasNext()) {
byte[] data = (byte[]) it.next();
System.out.println(new String(data, 0, data.length)); //KEYS are printed.
}
Set<Object> mySet = template.boundSetOps(KEY).members();
System.out.println(mySet); //EMPTY
return "";
}
public void addSomeData() {
template.getConnectionFactory().getConnection().sAdd(KEY.getBytes(), "value".getBytes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment