Commit 8798fd1d by liqiuwei

完善redis demo

parent 5838b1cd
/**
* Created by Administrator on 2018/1/16.
*/
package com.secooframework.redis;
import java.util.Date;
/**
* test
*
* @author Administrator
* @create 2018-01-16 11:19
**/
public class Person {
/**
* 用户ID
*/
private Long id;
/**
* 用户登录名
*/
private String loginName;
/**
* 部门名称
*/
private String deptName;
/**
* 操作时间
*/
private Date operDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Date getOperDate() {
return operDate;
}
public void setOperDate(Date operDate) {
this.operDate = operDate;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", loginName='" + loginName + '\'' +
", deptName='" + deptName + '\'' +
", operDate=" + operDate +
'}';
}
}
/**
* Created by Administrator on 2018/1/17.
*/
package com.secooframework.redis;
import com.secooframework.redis.annotation.RedisCache;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* aa
*
* @author Administrator
* @create 2018-01-17 13:47
**/
@Service
public class PersonService {
private final static Logger logger = LoggerFactory.getLogger(PersonService.class);
@RedisCache(expired = 60L, key = "'frame:PersonTest:'+#id")
public Person get(Long id) {
logger.info("get;{}", id);
Person p = new Person();
p.setOperDate(new Date());
p.setDeptName("ddd");
p.setId(id);
return p;
}
}
<?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">
<parent>
<artifactId>matrix-sample-redis</artifactId>
<groupId>com.secoo.mall</groupId>
<version>1.3.2.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-sample-redis-spring</artifactId>
</project>
\ No newline at end of file
package com.secoo.mall.redis;
import com.secoo.mall.redis.helper.RedisHelper;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2018/1/15.
*/
//@ContextConfiguration({"classpath*:spring-jedis-test.xml"})
@ContextConfiguration({"classpath*:spring-lettuce-test.xml"})
public class RedisClusterTest extends AbstractJUnit4SpringContextTests {
@Resource
private RedisHelper redisClusterHelper;
@Resource
private PersonService personService;
@Test
public void testSet() throws InterruptedException {
redisClusterHelper.set("aaa", "bbb");
String ret = redisClusterHelper.get("aaa", String.class);
Assert.assertEquals("bbb", ret);
redisClusterHelper.set("aaa", "ccc", 2l);
ret = redisClusterHelper.get("aaa", String.class);
Assert.assertEquals("ccc", ret);
Thread.sleep(3000l);
ret = redisClusterHelper.get("aaa", String.class);
Assert.assertNull(ret);
}
@Test
public void testSet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
for (int i = 0; i < 100; i++) {
dtos1.put("testKeyi" + i, "testValue" + i);
}
st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
List<String> list = redisClusterHelper.gets(new ArrayList<String>(dtos1.keySet()), String.class);
System.out.println(list);
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 1000; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
}
/**
* 测试多线程
*/
@Test
public void testThreads() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
for (int i = 20; i < 40; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
}
@Test
public void testGet() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.set("zhangsan111", p);
Person o = redisClusterHelper.get("zhangsan111", Person.class);
Assert.assertEquals("hhhhhh", o.getDeptName());
}
@Test
public void testGet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
st = System.currentTimeMillis();
List<String> vList = redisClusterHelper
.pipelineGets(new ArrayList<String>(dtos2.keySet()), String.class);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
System.out.println(vList);
}
@Test
public void testH() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.hSet("hsetKey", "f1", p);
Person person = redisClusterHelper.hGet("hsetKey", "f1", Person.class);
Assert.assertEquals("hhhhhh", p.getDeptName());
}
@Test
public void testAnn() {
Person person = personService.get(1L);
System.out.println(person);
person = personService.get(1L);
System.out.println(person);
}
@Test
public void testIncr() {
redisClusterHelper.set("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 10);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
}
@Test
public void testTtl() throws InterruptedException {
redisClusterHelper.set("incyKey", 1, 100);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.exists("incyKey"));
System.out.println(redisClusterHelper.exists("incyKey111"));
}
@Test
public void testHmset() {
Map<String, Integer> vv = new HashMap<>();
vv.put("one", 1);
vv.put("two", 2);
redisClusterHelper.hMSet("hmsetNumKey", vv);
Map<String, Integer> ret = redisClusterHelper
.hMGet("hmsetNumKey", new String[]{"one", "two"}, Integer.class);
System.out.println(ret);
ret = redisClusterHelper
.hGetAll("hmsetNumKey", String.class, Integer.class);
System.out.println(ret);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.secoo.archetype" level="DEBUG" />
<Logger name="org.springframework.beans.factory" level="INFO" />
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
redis.maxRedirects=5
redis.host1=10.185.240.142
redis.port1=7001
redis.host2=10.185.240.142
redis.port2=7002
redis.host3=10.185.240.142
redis.port3=7003
redis.host4=10.185.240.142
redis.port4=7004
redis.host5=10.185.240.142
redis.port5=7005
redis.host6=10.185.240.142
redis.port6=7006
# connnet pool
redis.maxIdle=50
redis.maxTotal=100
redis.minIdle=10
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secooframework"></context:component-scan>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="minIdle" value="${redis.minIdle}"/>
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<constructor-arg ref="jedisPoolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean>
<bean id="secooRedisTemplate" class="com.secooframework.redis.SecooRedisTemplate"/>
</beans>
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secooframework"></context:component-scan>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="minIdle" value="${redis.minIdle}"/>
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<constructor-arg ref="jedisPoolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean>
<bean id="secooRedisTemplate" class="com.secooframework.redis.SecooRedisTemplate"/>
</beans>
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secoo.mall"></context:component-scan>
<aop:config proxy-target-class="true"></aop:config>
<aop:aspectj-autoproxy/>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!--<bean id="clientResources" class="com.lambdaworks.redis.resource.DefaultClientResources"-->
<!--factory-method="create">-->
<!--</bean>-->
<bean id="lettuceConnectionFactory"
class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<!--<property name="clientResources" ref="clientResources"/>-->
</bean>
<!--可以注入一个mapper,默认的序列化会序列化类名-->
<!--ObjectMapper mapper = new ObjectMapper();-->
<!--mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);-->
<!--//日期格式-->
<!--mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));-->
<!--//null不序列化 减小报文大小-->
<!--mapper.setSerializationInclusion(Include.NON_NULL);-->
<bean id="redisCluserTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="lettuceConnectionFactory"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean>
<bean id="redisClusterHelper" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="redisCluserTemplate"/>
</bean>
</beans>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment