增加redis、memcached用例
This commit is contained in:
@ -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
|
Reference in New Issue
Block a user