1.RedisService的工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
public RedisService() {
}
public boolean set(String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception var5) {
var5.printStackTrace();
}
return result;
}
public boolean set(String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
operations.set(key, value);
this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
result = true;
} catch (Exception var6) {
var6.printStackTrace();
}
return result;
}
public void remove(String... keys) {
String[] var2 = keys;
int var3 = keys.length;
for(int var4 = 0; var4 < var3; ++var4) {
String key = var2[var4];
this.remove(key);
}
}
public void removePattern(String pattern) {
Set<Serializable> keys = this.redisTemplate.keys(pattern);
if(keys.size() > 0) {
this.redisTemplate.delete(keys);
}
}
public void remove(String key) {
if(this.exists(key)) {
this.redisTemplate.delete(key);
}
}
public boolean exists(String key) {
return this.redisTemplate.hasKey(key).booleanValue();
}
public Object get(String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
public void hmSet(String key, Object hashKey, Object value) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
public Object hmGet(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
public Set<Object> hmKeys(String key) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
return hash.keys(key);
}
public void hmDel(String masterKey, Object... hashKey) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
hash.delete(masterKey, hashKey);
}
public void lPush(String k, Object v) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
list.rightPush(k, v);
}
public List<Object> lRange(String k, long l, long l1) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.range(k, l, l1);
}
public void add(String key, Object value) {
SetOperations<String, Object> set = this.redisTemplate.opsForSet();
set.add(key, new Object[]{value});
}
public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = this.redisTemplate.opsForSet();
return set.members(key);
}
public void zAdd(String key, Object value, double scoure) {
ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
zset.add(key, value, scoure);
}
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
public Object lPop(String k) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.leftPop(k);
}
public boolean expire(String key, long timeout) {
return this.redisTemplate.expire(key, timeout, TimeUnit.SECONDS).booleanValue();
}
public long ttl(String key) {
return this.redisTemplate.getExpire(key).longValue();
}
}
2.redis的配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
@Configuration
public class RedisConfig {
/**
* 注入 RedisConnectionFactory
*/
@Resource
RedisConnectionFactory redisConnectionFactory;
/**
* 实例化 RedisTemplate 对象
*
* @return
*/
@Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
}
/**
* 设置数据存入 redis 的序列化方式
*
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
}
}
3.Redis的配置
# REDIS (RedisProperties)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=172.22.11.38
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=600
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=1000
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=300
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=100
# 连接超时时间(毫秒)
spring.redis.timeout=30000
4.pom文件的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>