话不多说,直接贴代码:
Maven pom.xml引入依赖
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml
spring:
redis:
database: 5
host: 127.0.0.1
password:
port: 6379
# 连接超时时间 单位 ms(毫秒)
timeout: 3000
#################redis线程池设置
pool:
# 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
max-active: 2000
# 连接池中的最大空闲连接,默认值也是8。
max-idle: 500
# 连接池中的最小空闲连接,默认值也是0。
min-idle: 50
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
max-wait: 1000
#################redis哨兵设置#################
# Redis服务器master的名字
#spring.redis.sentinel.master=master8026
# redis-sentinel的配置地址和端口
#spring.redis.sentinel.nodes=10.189.80.25:26379,10.189.80.26:26379,10.189.80.27:26378
IRedisRepository 接口定义
public interface IRedisRepository<K, V> {
// Key
void del(final K key);
void del(final Collection<K> keys);
Boolean exists(final K key);
Boolean expire(final K key, final long timeout, final TimeUnit unit);
void expireAt(final K key, Date date);
Set<K> keys(final K pattern);
String type(final K key);
V get(final K key);
V getSet(final K key, final V value);
Long incr(final K key, final long delta);
void set(final K key, final V value);
void set(final K key, final V value, final long timeout, final TimeUnit unit);
// Hash
void hDel(final K key, final Object... hKeys);
Boolean hExists(final K key, final K hKeys);
Map<K, V> hGet(final K key);
V hGet(final K key, final K hKey);
Set<K> hKeys(final K key);
Long hLen(final K key);
void hSet(final K key, final K hk, final V hv);
void hSet(final K key, final Map<K, V> map);
List<V> hVals(final K key);
// List
V lIndex(final K key, final long index);
void lInsert(final K key, final long index, V value);
Long lLen(final K key);
V lPop(final K key);
V lPop(final K key, long timeout, TimeUnit unit);
Long lPush(final K key, final V value);
List<V> lRange(final K key, final long start, final long end);
Long lRem(final K key, final long index, final V value);
void lSet(final K key, final long index, final V value);
void ltrim(final K key, final long start, final long end);
Long rPush(final K key, final V value);
V rPop(final K key);
// Set
Long sAdd(final K key, final V value);
Set<V> sDiff(final K key);
Set<V> sMembers(final K key);
Boolean sIsMember(final K key, final V value);
V sPop(final K key);
Long sRem(final K key, final V value);
Long sCard(final K key);
// SortedSet
void zAdd(final K key, final V value, final double score);
Set<V> zRange(final K key, final long start, final long end);
Long zRem(final K key, final Object... values);
Long zCard(final K key);
RedisRepository接口实现
@Repository("redisRepository")
public class RedisRepository<K, V> implements IRedisRepository<K, V> {
@Autowired
private RedisTemplate<K, V> redisTemplate;
private BoundValueOperations<K, V> getBoundValueOps(K key) {
return redisTemplate.boundValueOps(key);
}
private BoundZSetOperations<K, V> getBoundZSetOps(K key) {
return redisTemplate.boundZSetOps(key);
}
private BoundSetOperations<K, V> getBoundSetOps(K key) {
return redisTemplate.boundSetOps(key);
}
private BoundListOperations<K, V> getBoundListOps(K key) {
return redisTemplate.boundListOps(key);
}
private <HK, HV> BoundHashOperations<K, HK, HV> getBoundHashOps(K key) {
return redisTemplate.boundHashOps(key);
}
// Key
@Override
public void del(final K key) {
redisTemplate.delete(key);
}
@Override
public void del(final Collection<K> keys) {
redisTemplate.delete(keys);
}
@Override
public Boolean exists(final K key) {
return redisTemplate.hasKey(key);
}
@Override
public Boolean expire(final K key, final long timeout, final TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
@Override
public void expireAt(final K key, Date date) {
redisTemplate.expireAt(key, date);
}
@Override
public Set<K> keys(final K pattern) {
return redisTemplate.keys(pattern);
}
@Override
public String type(final K key) {
return redisTemplate.type(key).code();
}
@Override
public V get(final K key) {
BoundValueOperations<K, V> ops = this.getBoundValueOps(key);
return ops.get();
}
@Override
public V getSet(final K key, final V value) {
BoundValueOperations<K, V> ops = this.getBoundValueOps(key);
return ops.getAndSet(value);
}
@Override
public Long incr(final K key, final long delta) {
BoundValueOperations<K, V> ops = this.getBoundValueOps(key);
return ops.increment(delta);
}
@Override
public void set(final K key, final V value) {
BoundValueOperations<K, V> ops = this.getBoundValueOps(key);
ops.set(value);
}
@Override
public void set(final K key, final V value, final long timeout, final TimeUnit unit) {
BoundValueOperations<K, V> ops = this.getBoundValueOps(key);
ops.set(value, timeout, unit);
}
// Hash
@Override
public void hDel(final K key, final Object... hKeys) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
ops.delete(hKeys);
}
@Override
public Boolean hExists(final K key, final K hKeys) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.hasKey(hKeys);
}
@Override
public Map<K, V> hGet(final K key) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.entries();
}
@Override
public V hGet(final K key, final K hKey) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.get(hKey);
}
@Override
public Set<K> hKeys(final K key) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.keys();
}
@Override
public Long hLen(final K key) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.size();
}
@Override
public void hSet(final K key, final K hk, final V hv) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
ops.put(hk, hv);
}
@Override
public void hSet(final K key, final Map<K, V> map) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
ops.putAll(map);
}
@Override
public List<V> hVals(final K key) {
BoundHashOperations<K, K, V> ops = this.getBoundHashOps(key);
return ops.values();
}
// List
@Override
public V lIndex(final K key, final long index) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.index(index);
}
@Override
public void lInsert(final K key, final long index, V value) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
ops.set(index, value);
}
@Override
public Long lLen(final K key) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.size();
}
@Override
public V lPop(final K key) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.leftPop();
}
@Override
public V lPop(final K key, long timeout, TimeUnit unit) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.leftPop(timeout, unit);
}
@Override
public Long lPush(final K key, final V value) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.leftPush(value);
}
@Override
public List<V> lRange(final K key, final long start, final long end) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.range(start, end);
}
@Override
public Long lRem(final K key, final long index, final V value) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.remove(index, value);
}
@Override
public void lSet(final K key, final long index, final V value) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
ops.set(index, value);
}
@Override
public void ltrim(final K key, final long start, final long end) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
ops.trim(start, end);
}
@Override
public Long rPush(final K key, final V value) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.rightPush(value);
}
@Override
public V rPop(final K key) {
BoundListOperations<K, V> ops = this.getBoundListOps(key);
return ops.rightPop();
}
// Set
@Override
public Long sAdd(final K key, final V value) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.add(value);
}
@Override
public Set<V> sDiff(final K key) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.diff(key);
}
@Override
public Set<V> sMembers(final K key) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.members();
}
@Override
public Boolean sIsMember(final K key, final V value) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.isMember(value);
}
@Override
public V sPop(final K key) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.pop();
}
@Override
public Long sRem(final K key, final V value) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.remove(value);
}
@Override
public Long sCard(K key) {
BoundSetOperations<K, V> ops = this.getBoundSetOps(key);
return ops.size();
}
// SortedSet
@Override
public void zAdd(final K key, final V value, final double score) {
BoundZSetOperations<K, V> ops = this.getBoundZSetOps(key);
ops.add(value, score);
}
@Override
public Set<V> zRange(final K key, final long start, final long end) {
BoundZSetOperations<K, V> ops = this.getBoundZSetOps(key);
return ops.range(start, end);
}
@Override
public Long zRem(final K key, final Object... values) {
BoundZSetOperations<K, V> ops = this.getBoundZSetOps(key);
return ops.remove(values);
}
@Override
public Long zCard(K key) {
BoundZSetOperations<K, V> ops = this.getBoundZSetOps(key);
return ops.zCard();
}
public RedisTemplate<K, V> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
测试:
@Autowired
private RedisRepository redisRepository;
redisRepository.set(key, token);
redisRepository.expireAt(key, expireTime);
这样就OK了。