!4 使用基于netty性能更优的lettuce代替jedis

Merge pull request !4 from Edin/master
pull/92/head
Elune 2019-06-04 15:30:02 +08:00 committed by Gitee
commit 083bbdc6e7
4 changed files with 19 additions and 102 deletions

View File

@ -35,40 +35,6 @@ import java.time.Duration;
@EnableConfigurationProperties(RedisProperties.class) @EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport { public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private int database;
/**
* redis
* @return
*/
@Bean
public JedisPool redisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
String pwd = StringUtils.isBlank(password) ? null : password;
return new JedisPool(jedisPoolConfig, host, port, timeout, pwd, database);
}
/** /**
* redis 1 * redis 1
* @cacheable * @cacheable
@ -127,4 +93,4 @@ public class RedisConfig extends CachingConfigurerSupport {
return sb.toString(); return sb.toString();
}; };
} }
} }

View File

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
@ -21,72 +22,38 @@ import java.util.List;
public class RedisServiceImpl implements RedisService { public class RedisServiceImpl implements RedisService {
@Autowired @Autowired
JedisPool pool; RedisTemplate redisTemplate;
@Override @Override
public Page findByKey(String key, Pageable pageable){ public Page findByKey(String key, Pageable pageable){
Jedis jedis = null; List<RedisVo> redisVos = new ArrayList<>();
try{ if(!key.equals("*")){
jedis = pool.getResource(); key = "*" + key + "*";
List<RedisVo> redisVos = new ArrayList<>();
if(!key.equals("*")){
key = "*" + key + "*";
}
for (String s : jedis.keys(key)) {
RedisVo redisVo = new RedisVo(s,jedis.get(s));
redisVos.add(redisVo);
}
Page<RedisVo> page = new PageImpl<RedisVo>(
PageUtil.toPage(pageable.getPageNumber(),pageable.getPageSize(),redisVos),
pageable,
redisVos.size());
return page;
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
} }
for (Object s : redisTemplate.keys(key)) {
RedisVo redisVo = new RedisVo(s.toString(),redisTemplate.opsForValue().get(s.toString()).toString());
redisVos.add(redisVo);
}
Page<RedisVo> page = new PageImpl<RedisVo>(
PageUtil.toPage(pageable.getPageNumber(),pageable.getPageSize(),redisVos),
pageable,
redisVos.size());
return page;
} }
@Override @Override
public void save(RedisVo redisVo) { public void save(RedisVo redisVo) {
Jedis jedis = null; redisTemplate.opsForValue().set(redisVo.getKey(),redisVo.getValue());
try{
jedis = pool.getResource();
jedis.set(redisVo.getKey(),redisVo.getValue());
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
} }
@Override @Override
public void delete(String key) { public void delete(String key) {
Jedis jedis = null; redisTemplate.delete(key);
try{
jedis = pool.getResource();
jedis.del(key);
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
} }
@Override @Override
public void flushdb() { public void flushdb() {
Jedis jedis = null; redisTemplate.getConnectionFactory().getConnection().flushDb();
try{
jedis = pool.getResource();
jedis.flushAll();
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
} }
} }

View File

@ -24,16 +24,6 @@ spring:
host: 127.0.0.1 host: 127.0.0.1
port: 6379 port: 6379
password: password:
jedis:
pool:
#最大连接数
max-active: 100
#最大阻塞等待时间(负数表示没限制)
max-wait: 2000
#最大空闲
max-idle: 500
#最小空闲
min-idle: 8
#连接超时时间 #连接超时时间
timeout: 5000 timeout: 5000

View File

@ -70,12 +70,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!--Spring boot end--> <!--Spring boot end-->
@ -231,4 +225,4 @@
</snapshots> </snapshots>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
</project> </project>