Redis為一個後台緩存快取的noSQL資料庫,用key、value型態,可跨語言及設定有效時間。
1.pom.xml依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.application.properties設定檔
# Redis數據庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=127.0.0.1
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=
#最大連接數量
spring.redis.jedis.pool.max-active=1000
#連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-idle=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=1000
# 連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=500
3.Jedis配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class TestJedis {
@Value("${spring.redis.host}")
String host;
@Value("${spring.redis.port}")
int port;
@Value("${spring.redis.timeout}")
int time;
@Value("${spring.redis.jedis.pool.max-active}")
int poolTotal;
@Value("${spring.redis.jedis.pool.max-idle}")
int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
int minIdle;
@Bean
public JedisPool JedisPoolUtil() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setTestOnBorrow(true);
poolConfig.setMaxTotal(poolTotal);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(time);
poolConfig.setTestWhileIdle(true);
return new JedisPool(poolConfig, host, port);
}
}
4.RedisService
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class RedisService {
@Autowired
JedisPool jedisPool;
public Set<String> getAllKey() {
Jedis jedis = jedisPool.getResource();
Set<String> set = jedis.keys("*");
jedis.close();
return set;
}
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String set = jedis.set(key, value);// return OK
jedis.close();
return set;
}
public boolean setTime(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long success = jedis.expire(key, second);
jedis.close();
if (success == 1)
return true;
else {
return false;
}
}
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String value = jedis.get(key);
jedis.close();
return value;
}
public Long delete(String key) {
Jedis jedis = jedisPool.getResource();
Long quantity = jedis.del(key);
jedis.close();
return quantity;
}
public long getTime(String key) {
Jedis jedis = jedisPool.getResource();
Long time = jedis.ttl(key);// -1為永遠有效 -2為過期
jedis.close();
return time;
}
public Long setKeyAdd(String key) {
Jedis jedis = jedisPool.getResource();
Long value = jedis.incr(key);
jedis.close();
return value;
}
public Long setKeySub(String key) {
Jedis jedis = jedisPool.getResource();
Long value = jedis.decr(key);
jedis.close();
return value;
}
public Long setKeyAddValue(String key, Long value) {
Jedis jedis = jedisPool.getResource();
Long newValue = jedis.incrBy(key, value);
jedis.close();
return newValue;
}
public Long setKeySubValue(String key, Long value) {
Jedis jedis = jedisPool.getResource();
Long newValue = jedis.decrBy(key, value);
jedis.close();
return newValue;
}
}
5.Controller
@Autowired
RedisService redisService;
留言列表