增加redis、memcached用例
This commit is contained in:
90
spring/spring-redis-annotation/pom.xml
Normal file
90
spring/spring-redis-annotation/pom.xml
Normal file
@ -0,0 +1,90 @@
|
||||
<?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>com.heibaiying</groupId>
|
||||
<artifactId>spring-redis-annotation</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--Redis java 客户端 本用例会提供 关于 Jedis 和 redisson 两种客户端的整合-->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<version>3.9.1</version>
|
||||
</dependency>
|
||||
<!--redisson 中部分功能依赖了netty -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.32.Final</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,25 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Programmer {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private float salary;
|
||||
|
||||
private Date birthday;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:jedis.properties")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RedisProperty {
|
||||
|
||||
@Value("${redis.host}")
|
||||
private String host;
|
||||
@Value("${redis.port}")
|
||||
private int port;
|
||||
@Value("${redis.timeout}")
|
||||
private int timeout;
|
||||
@Value("${redis.maxIdle}")
|
||||
private int maxIdle;
|
||||
@Value("${redis.maxTotal}")
|
||||
private int maxTotal;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.heibaiying.config.jedis;
|
||||
|
||||
import com.heibaiying.config.RedisProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Jedis 集群配置
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(value = "com.heibaiying.*")
|
||||
public class ClusterJedisConfig {
|
||||
|
||||
@Bean
|
||||
public JedisCluster jedisCluster(RedisProperty property) {
|
||||
JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||
poolConfig.setMaxIdle(property.getMaxIdle());
|
||||
poolConfig.setMaxTotal(property.getMaxTotal());
|
||||
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
|
||||
nodes.add(new HostAndPort("127.0.0.1", 6379));
|
||||
nodes.add(new HostAndPort("127.0.0.1", 6380));
|
||||
return new JedisCluster(nodes, 2000);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.heibaiying.config.jedis;
|
||||
|
||||
import com.heibaiying.config.RedisProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Jedis 单机配置
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(value = "com.heibaiying.*")
|
||||
public class SingleJedisConfig {
|
||||
|
||||
@Bean
|
||||
public JedisPool jedisPool(RedisProperty property) {
|
||||
JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||
poolConfig.setMaxIdle(property.getMaxIdle());
|
||||
poolConfig.setMaxTotal(property.getMaxTotal());
|
||||
return new JedisPool(poolConfig, property.getHost(), property.getPort(), property.getTimeout());
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
@Scope(value = SCOPE_PROTOTYPE)
|
||||
public Jedis jedis(JedisPool jedisPool) {
|
||||
return jedisPool.getResource();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.config.redisson;
|
||||
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : redisson 集群配置
|
||||
*/
|
||||
@Configuration
|
||||
public class ClusterRedissonConfig {
|
||||
|
||||
//@Bean
|
||||
public RedissonClient redissonClient() {
|
||||
Config config = new Config();
|
||||
config.useClusterServers()
|
||||
.setScanInterval(2000) // 集群状态扫描间隔时间,单位是毫秒
|
||||
//可以用"rediss://"来启用SSL连接
|
||||
.addNodeAddress("redis://127.0.0.1:6379", "redis://127.0.0.1:6380")
|
||||
.addNodeAddress("redis://127.0.0.1:6381");
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.heibaiying.config.redisson;
|
||||
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : redisson 单机配置
|
||||
*/
|
||||
@Configuration
|
||||
public class SingalRedissonConfig {
|
||||
|
||||
@Bean
|
||||
public RedissonClient redissonClient() {
|
||||
Config config = new Config();
|
||||
config.setTransportMode(TransportMode.NIO);
|
||||
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
redis.host=127.0.0.1
|
||||
redis.port=6379
|
||||
redis.timeout=2000
|
||||
redis.maxIdle=8
|
||||
redis.maxTotal=16
|
@ -0,0 +1,42 @@
|
||||
package heibaiying.jedis;
|
||||
|
||||
import com.heibaiying.config.jedis.ClusterJedisConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :redis 集群测试
|
||||
*/
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClusterJedisConfig.class)
|
||||
public class JedisClusterSamples {
|
||||
|
||||
@Autowired
|
||||
private JedisCluster jedisCluster;
|
||||
|
||||
@Test
|
||||
public void Set() {
|
||||
jedisCluster.set("hello", "spring");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Get() {
|
||||
String s = jedisCluster.get("hello");
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setEx() {
|
||||
String s = jedisCluster.setex("spring", 10, "我会在10秒后过期");
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package heibaiying.jedis;
|
||||
|
||||
import com.heibaiying.config.jedis.SingleJedisConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :redis 单机版测试
|
||||
*/
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = SingleJedisConfig.class)
|
||||
public class JedisSamples {
|
||||
|
||||
@Autowired
|
||||
private Jedis jedis;
|
||||
|
||||
@Test
|
||||
public void Set() {
|
||||
jedis.set("hello", "spring annotation");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Get() {
|
||||
String s = jedis.get("hello");
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setEx() {
|
||||
String s = jedis.setex("spring", 10, "我会在10秒后过期");
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package heibaiying.redisson;
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import com.heibaiying.config.jedis.ClusterJedisConfig;
|
||||
import com.heibaiying.config.redisson.SingalRedissonConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :redisson 对象序列化与反序列化
|
||||
*/
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = SingalRedissonConfig.class)
|
||||
public class RedissonObjectSamples {
|
||||
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
// Redisson的对象编码类是用于将对象进行序列化和反序列化 默认采用Jackson
|
||||
|
||||
@Test
|
||||
public void Set() {
|
||||
RBucket<Programmer> rBucket = redissonClient.getBucket("programmer");
|
||||
rBucket.set(new Programmer("xiaoming", 12, 5000.21f, new Date()));
|
||||
redissonClient.shutdown();
|
||||
//存储结果: {"@class":"com.heibaiying.com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Get() {
|
||||
RBucket<Programmer> rBucket = redissonClient.getBucket("programmer");
|
||||
System.out.println(rBucket.get());
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
redissonClient.shutdown();
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package heibaiying.redisson;
|
||||
|
||||
import com.heibaiying.config.redisson.SingalRedissonConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :redisson 操作普通对象
|
||||
*/
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = SingalRedissonConfig.class)
|
||||
public class RedissonSamples {
|
||||
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Test
|
||||
public void Set() {
|
||||
// key 存在则更新 不存在则删除
|
||||
RBucket<String> rBucket = redissonClient.getBucket("redisson");
|
||||
rBucket.set("annotation Value");
|
||||
redissonClient.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Get() {
|
||||
// key 存在则更新 不存在则删除
|
||||
RBucket<String> rBucket = redissonClient.getBucket("redisson");
|
||||
System.out.println(rBucket.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SetEx() {
|
||||
// key 存在则更新 不存在则删除
|
||||
RBucket<String> rBucket = redissonClient.getBucket("redissonEx");
|
||||
rBucket.set("我在十秒后会消失", 10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
redissonClient.shutdown();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user