From 6c034ba876e4cc93be891fd4a54aa328ac3e2110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Fri, 15 Apr 2016 16:33:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BF=E9=97=AEredis=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter3-2-5/pom.xml | 55 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 13 +++++ .../main/java/com/didispace/RedisConfig.java | 35 ++++++++++++ .../com/didispace/RedisObjectSerializer.java | 43 +++++++++++++++ .../main/java/com/didispace/domain/User.java | 39 +++++++++++++ .../src/main/resources/application.properties | 22 ++++++++ .../java/com/didispace/ApplicationTests.java | 48 ++++++++++++++++ 7 files changed, 255 insertions(+) create mode 100755 Chapter3-2-5/pom.xml create mode 100755 Chapter3-2-5/src/main/java/com/didispace/Application.java create mode 100644 Chapter3-2-5/src/main/java/com/didispace/RedisConfig.java create mode 100644 Chapter3-2-5/src/main/java/com/didispace/RedisObjectSerializer.java create mode 100644 Chapter3-2-5/src/main/java/com/didispace/domain/User.java create mode 100644 Chapter3-2-5/src/main/resources/application.properties create mode 100755 Chapter3-2-5/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter3-2-5/pom.xml b/Chapter3-2-5/pom.xml new file mode 100755 index 0000000..dc1d438 --- /dev/null +++ b/Chapter3-2-5/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.didispace + demo + 1.0.0 + jar + + demo + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-redis + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/Chapter3-2-5/src/main/java/com/didispace/Application.java b/Chapter3-2-5/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..2a14b75 --- /dev/null +++ b/Chapter3-2-5/src/main/java/com/didispace/Application.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter3-2-5/src/main/java/com/didispace/RedisConfig.java b/Chapter3-2-5/src/main/java/com/didispace/RedisConfig.java new file mode 100644 index 0000000..cf103ff --- /dev/null +++ b/Chapter3-2-5/src/main/java/com/didispace/RedisConfig.java @@ -0,0 +1,35 @@ +package com.didispace; + +import com.didispace.domain.User; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.*; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/4/15 下午3:19. + * @blog http://blog.didispace.com + */ +@Configuration +public class RedisConfig { + + @Bean + JedisConnectionFactory jedisConnectionFactory() { + return new JedisConnectionFactory(); + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(jedisConnectionFactory()); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new RedisObjectSerializer()); + return template; + } + + +} diff --git a/Chapter3-2-5/src/main/java/com/didispace/RedisObjectSerializer.java b/Chapter3-2-5/src/main/java/com/didispace/RedisObjectSerializer.java new file mode 100644 index 0000000..9aecc44 --- /dev/null +++ b/Chapter3-2-5/src/main/java/com/didispace/RedisObjectSerializer.java @@ -0,0 +1,43 @@ +package com.didispace; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.serializer.support.DeserializingConverter; +import org.springframework.core.serializer.support.SerializingConverter; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +public class RedisObjectSerializer implements RedisSerializer { + + private Converter serializer = new SerializingConverter(); + private Converter deserializer = new DeserializingConverter(); + + static final byte[] EMPTY_ARRAY = new byte[0]; + + public Object deserialize(byte[] bytes) { + if (isEmpty(bytes)) { + return null; + } + + try { + return deserializer.convert(bytes); + } catch (Exception ex) { + throw new SerializationException("Cannot deserialize", ex); + } + } + + public byte[] serialize(Object object) { + if (object == null) { + return EMPTY_ARRAY; + } + + try { + return serializer.convert(object); + } catch (Exception ex) { + return EMPTY_ARRAY; + } + } + + private boolean isEmpty(byte[] data) { + return (data == null || data.length == 0); + } +} \ No newline at end of file diff --git a/Chapter3-2-5/src/main/java/com/didispace/domain/User.java b/Chapter3-2-5/src/main/java/com/didispace/domain/User.java new file mode 100644 index 0000000..4161519 --- /dev/null +++ b/Chapter3-2-5/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,39 @@ +package com.didispace.domain; + +import java.io.Serializable; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/4/15 下午1:58. + * @blog http://blog.didispace.com + */ +public class User implements Serializable { + + private static final long serialVersionUID = -1L; + + private String username; + private Integer age; + + public User(String username, Integer age) { + this.username = username; + this.age = age; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + +} diff --git a/Chapter3-2-5/src/main/resources/application.properties b/Chapter3-2-5/src/main/resources/application.properties new file mode 100644 index 0000000..f79fc03 --- /dev/null +++ b/Chapter3-2-5/src/main/resources/application.properties @@ -0,0 +1,22 @@ +# REDIS (RedisProperties) +# Redis数据库索引(默认为0) +spring.redis.database=0 +# Redis服务器地址 +spring.redis.host=localhost +# Redis服务器连接端口 +spring.redis.port=6379 +# Redis服务器连接密码(默认为空) +spring.redis.password= +# 连接池最大连接数(使用负值表示没有限制) +spring.redis.pool.max-active=8 +# 连接池最大阻塞等待时间(使用负值表示没有限制) +spring.redis.pool.max-wait=-1 +# 连接池中的最大空闲连接 +spring.redis.pool.max-idle=8 +# 连接池中的最小空闲连接 +spring.redis.pool.min-idle=0 +# 连接超时时间(毫秒) +spring.redis.timeout=0 + + + diff --git a/Chapter3-2-5/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-2-5/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..35594ea --- /dev/null +++ b/Chapter3-2-5/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,48 @@ +package com.didispace; + +import com.didispace.domain.User; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class ApplicationTests { + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Autowired + private RedisTemplate redisTemplate; + + @Test + public void test() throws Exception { + + // 保存字符串 + stringRedisTemplate.opsForValue().set("aaa", "111"); + Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); + + // 保存对象 + User user = new User("超人", 20); + redisTemplate.opsForValue().set(user.getUsername(), user); + + user = new User("蝙蝠侠", 30); + redisTemplate.opsForValue().set(user.getUsername(), user); + + user = new User("蜘蛛侠", 40); + redisTemplate.opsForValue().set(user.getUsername(), user); + + Assert.assertEquals(20, redisTemplate.opsForValue().get("超人").getAge().longValue()); + Assert.assertEquals(30, redisTemplate.opsForValue().get("蝙蝠侠").getAge().longValue()); + Assert.assertEquals(40, redisTemplate.opsForValue().get("蜘蛛侠").getAge().longValue()); + + } + +}