SpringBoot(2) ---SpringBoot 集成Redis
1.pom.xml添加依赖
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.application.properties配置redis
spring.redis.database=0
spring.redis.hostName=127.0.0.1
spring.redis.password=
spring.redis.port=6379
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=0
3.RedisUtil.java
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* String 类型的set操作
*
* @param key
* @param value
* @return
*/
public boolean set(String key, String value) {
boolean result = false;
try {
stringRedisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* String 类型的set操作 有过期时间
*
* @param key
* @param value
* @return
*/
public boolean set(String key, String value, long timeout) {
boolean result = false;
try {
stringRedisTemplate.opsForValue().set(key, value);
expire(key, timeout);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* String 类型的get操作
*
* @param key
* @return
*/
public String get(String key) {
if (StringUtils.isBlank(key)) {
return null;
}
return stringRedisTemplate.opsForValue().get(key);
}
/**
* 设置过期时间,单位秒
*
* @param key
* @param timeout
*/
public void expire(String key, long timeout) {
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 以秒为单位返回给定key的剩余生存时间(ttl = time to live)
*
* @param key
* @return
*/
public long ttl(String key) {
return stringRedisTemplate.getExpire(key);
}
/**
* 增加 key 一次
*
* @param key
* @param delta
* @return
*/
public long incr(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
/**
* 查找所有符合给定模式pattern的key
*
* @param pattern
* @return
*/
public Set<String> keys(String pattern) {
return stringRedisTemplate.keys(pattern);
}
/**
* 删除key
*
* @param key
*/
public void del(String key) {
stringRedisTemplate.delete(key);
}
/**
* 将hash表key中的field设置为value,如果hash表不存则生成一个
*
* @param key
* @param field
* @param value
*/
public void hset(String key, String field, String value) {
stringRedisTemplate.opsForHash().put(key, field, value);
}
/**
* 将hash表key中的field设置为value,如果hash表不存则生成一个,添加过期时间
*
* @param key
* @param field
* @param value
*/
public void hset(String key, String field, String value, long timeout) {
stringRedisTemplate.opsForHash().put(key, field, value);
expire(key, timeout);
}
/**
* 从哈希表key中获取field的value
*
* @param key
* @param field
* @return
*/
public String hget(String key, String field) {
return (String) stringRedisTemplate.opsForHash().get(key, field);
}
/**
* 查看哈希表 key 中,指定的字段是否存在。
*
* @param key
* @param field
* @return
*/
public boolean hexists(String key, String field) {
return stringRedisTemplate.opsForHash().hasKey(key, field);
}
/**
* 获取在哈希表中指定 key 的所有字段和值
*
* @param key
* @return
*/
public Map hgetall(String key) {
return stringRedisTemplate.opsForHash().entries(key);
}
/**
* 获取哈希表中字段的数量
*
* @param key
* @return
*/
public Long hlen(String key) {
return stringRedisTemplate.opsForHash().size(key);
}
/**
* 获取所有哈希表中的字段
*
* @param key
* @return
*/
public Set hkeys(String key) {
return stringRedisTemplate.opsForHash().keys(key);
}
/**
* 删除一个或多个哈希表字段
*
* @param key
* @param field
* @return
*/
public Long hdel(String key, String field) {
return stringRedisTemplate.opsForHash().delete(key, field);
}
}
4.controller中使用
@Autowired
private RedisUtil redisUtil;
redisUtil.hset(BaseSet.WECHAT_GZH_IDX, _wechatGzhs.getId().toString(), JSONObject.toJSONString(_wechatGzhs));