1 引入pom
org.springframework.boot spring-boot-starter-data-redis
2.添加配置文件
# redis配置 spring.redis.host=127.0.0.1 spring.redis.timeout=3000 spring.redis.password= spring.redis.port=6379 spring.redis.database=5 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.min-idle=0
3装配redisTemplate
import com.alan.lawyer.redis.RedisUtils;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@import({RedisUtils.class})
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate
4创建工具类
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
@SuppressWarnings({"unchecked", "unused"})
public class RedisUtils implements InitializingBean {
@Resource
private RedisTemplate redisTemplate;
private static RedisTemplate template;
@Override
public void afterPropertiesSet() {
template = redisTemplate;
}
public static class KeyOps {
public static void delete(String key) {
template.delete(key);
}
public static void delete(Collection keys) {
template.delete(keys);
}
public static byte[] dump(String key) {
return template.dump(key);
}
public static Boolean hasKey(String key) {
return template.hasKey(key);
}
public static Boolean expire(String key, long timeout, TimeUnit unit) {
return template.expire(key, timeout, unit);
}
public static Boolean expireAt(String key, Date date) {
return template.expireAt(key, date);
}
public static Set keys(String pattern) {
return template.keys(pattern);
}
public static Boolean move(String key, int dbIndex) {
return template.move(key, dbIndex);
}
public static Boolean persist(String key) {
return template.persist(key);
}
public static Long getExpire(String key, TimeUnit unit) {
return template.getExpire(key, unit);
}
public static Long getExpire(String key) {
return template.getExpire(key);
}
public static String randomKey() {
return template.randomKey();
}
public static void rename(String oldKey, String newKey) {
template.rename(oldKey, newKey);
}
public static Boolean renameIfAbsent(String oldKey, String newKey) {
return template.renameIfAbsent(oldKey, newKey);
}
public static DataType type(String key) {
return template.type(key);
}
}
public static class ValueOps {
public static void set(String key, T value) {
template.opsForValue().set(key, value);
}
public static T get(String key) {
return (T) template.opsForValue().get(key);
}
public static String get(String key, long start, long end) {
return template.opsForValue().get(key, start, end);
}
public static T getAndSet(String key, T value) {
return (T) template.opsForValue().getAndSet(key, value);
}
public static Boolean getBit(String key, long offset) {
return template.opsForValue().getBit(key, offset);
}
public static List multiGet(Collection keys) {
return (List) template.opsForValue().multiGet(keys);
}
public static Boolean setBit(String key, long offset, boolean value) {
return template.opsForValue().setBit(key, offset, value);
}
public static void set(String key, T value, long timeout, TimeUnit unit) {
template.opsForValue().set(key, value, timeout, unit);
}
public static Boolean setIfAbsent(String key, T value) {
return template.opsForValue().setIfAbsent(key, value);
}
public static Boolean setIfAbsent(String key, T value, Duration timeout) {
return template.opsForValue().setIfAbsent(key, value, timeout);
}
public static Boolean setIfAbsent(String key, T value, long timeout, TimeUnit unit) {
return template.opsForValue().setIfAbsent(key, value, timeout, unit);
}
public static void set(String key, T value, long offset) {
template.opsForValue().set(key, value, offset);
}
public static Long size(String key) {
return template.opsForValue().size(key);
}
public static void multiSet(Map maps) {
template.opsForValue().multiSet(maps);
}
public static Boolean multiSetIfAbsent(Map maps) {
return template.opsForValue().multiSetIfAbsent(maps);
}
public static Long increment(String key, long increment) {
return template.opsForValue().increment(key, increment);
}
public static Double increment(String key, double increment) {
return template.opsForValue().increment(key, increment);
}
public static Integer append(String key, String value) {
return template.opsForValue().append(key, value);
}
}
public static class HashOps {
public static T get(String key, String field) {
HashOperations hashOps = template.opsForHash();
return hashOps.get(key, field);
}
public static Map entries(String key) {
HashOperations hashOps = template.opsForHash();
return hashOps.entries(key);
}
public static List multiGet(String key, Collection fields) {
HashOperations hashOps = template.opsForHash();
return hashOps.multiGet(key, fields);
}
public static void put(String key, String hashKey, T value) {
template.opsForHash().put(key, hashKey, value);
}
public static void putAll(String key, Map maps) {
template.opsForHash().putAll(key, maps);
}
public static Boolean putIfAbsent(String key, String hashKey, T value) {
return template.opsForHash().putIfAbsent(key, hashKey, value);
}
public static Long delete(String key, String... fields) {
return template.opsForHash().delete(key, Arrays.stream(fields).toArray(Object[]::new));
}
public static Boolean hasKey(String key, String field) {
return template.opsForHash().hasKey(key, field);
}
public static Long increment(String key, String field, long increment) {
return template.opsForHash().increment(key, field, increment);
}
public static Double increment(String key, String field, double delta) {
return template.opsForHash().increment(key, field, delta);
}
public static Set keys(String key) {
HashOperations opsForHash = template.opsForHash();
return opsForHash.keys(key);
}
public static Long size(String key) {
return template.opsForHash().size(key);
}
public static List values(String key) {
HashOperations hashOps = template.opsForHash();
return hashOps.values(key);
}
public static Cursor> scan(String key, ScanOptions options) {
HashOperations hashOps = template.opsForHash();
return hashOps.scan(key, options);
}
}
public static class ListOps {
public static T index(String key, long index) {
return (T) template.opsForList().index(key, index);
}
public static List range(String key, long start, long end) {
return (List) template.opsForList().range(key, start, end);
}
public static Long leftPush(String key, T value) {
return template.opsForList().leftPush(key, value);
}
public static Long leftPushAll(String key, T... values) {
return template.opsForList().leftPushAll(key, values);
}
public static Long leftPushAll(String key, Collection values) {
return template.opsForList().leftPushAll(key, values.toArray());
}
public static Long leftPushIfPresent(String key, T value) {
return template.opsForList().leftPushIfPresent(key, value);
}
public static Long leftPush(String key, String pivot, T value) {
return template.opsForList().leftPush(key, pivot, value);
}
public static Long rightPush(String key, T value) {
return template.opsForList().rightPush(key, value);
}
public static Long rightPushAll(String key, T... value) {
return template.opsForList().rightPushAll(key, value);
}
public static Long rightPushAll(String key, Collection value) {
return template.opsForList().rightPushAll(key, value.toArray());
}
public static Long rightPushIfPresent(String key, T value) {
return template.opsForList().rightPushIfPresent(key, value);
}
public static Long rightPush(String key, String pivot, T value) {
return template.opsForList().rightPush(key, pivot, value);
}
public static void set(String key, long index, T value) {
template.opsForList().set(key, index, value);
}
public static T leftPop(String key) {
return (T) template.opsForList().leftPop(key);
}
public static T leftPop(String key, long timeout, TimeUnit unit) {
return (T) template.opsForList().leftPop(key, timeout, unit);
}
public static T rightPop(String key) {
return (T) template.opsForList().rightPop(key);
}
public static T rightPop(String key, long timeout, TimeUnit unit) {
return (T) template.opsForList().rightPop(key, timeout, unit);
}
public static T rightPopAndLeftPush(String sourceKey, String destinationKey) {
return (T) template.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
}
public static T rightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit unit) {
return (T) template.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
}
public static Long remove(String key, long index, T value) {
return template.opsForList().remove(key, index, value);
}
public static void trim(String key, long start, long end) {
template.opsForList().trim(key, start, end);
}
public static Long size(String key) {
return template.opsForList().size(key);
}
}
public static class SetOps {
public static Long add(String key, T... values) {
return template.opsForSet().add(key, values);
}
public static Long add(String key, Collection values) {
return template.opsForSet().add(key, values.toArray());
}
public static Long remove(String key, T... values) {
return template.opsForSet().remove(key, Arrays.stream(values).toArray());
}
public static T pop(String key) {
return (T) template.opsForSet().pop(key);
}
public static Boolean move(String key, T value, String destKey) {
return template.opsForSet().move(key, value, destKey);
}
public static Long size(String key) {
return template.opsForSet().size(key);
}
public static Boolean isMember(String key, T value) {
return template.opsForSet().isMember(key, value);
}
public static Set intersect(String key, String otherKey) {
return (Set) template.opsForSet().intersect(key, otherKey);
}
public static Set intersect(String key, Collection otherKeys) {
return (Set) template.opsForSet().intersect(key, otherKeys);
}
public static Long intersectAndStore(String key, String otherKey, String destKey) {
return template.opsForSet().intersectAndStore(key, otherKey, destKey);
}
public static Long intersectAndStore(String key, Collection otherKeys, String destKey) {
return template.opsForSet().intersectAndStore(key, otherKeys, destKey);
}
public static Set union(String key, String otherKeys) {
return (Set) template.opsForSet().union(key, otherKeys);
}
public static Set union(String key, Collection otherKeys) {
return (Set) template.opsForSet().union(key, otherKeys);
}
public static Long unionAndStore(String key, String otherKey, String destKey) {
return template.opsForSet().unionAndStore(key, otherKey, destKey);
}
public static Long unionAndStore(String key, Collection otherKeys, String destKey) {
return template.opsForSet().unionAndStore(key, otherKeys, destKey);
}
public static Set difference(String key, String otherKey) {
return (Set) template.opsForSet().difference(key, otherKey);
}
public static Set difference(String key, Collection otherKeys) {
return (Set) template.opsForSet().difference(key, otherKeys);
}
public static Long differenceAndStore(String key, String otherKey, String destKey) {
return template.opsForSet().differenceAndStore(key, otherKey, destKey);
}
public static Long differenceAndStore(String key, Collection otherKeys,
String destKey) {
return template.opsForSet().differenceAndStore(key, otherKeys, destKey);
}
public static Set members(String key) {
return (Set) template.opsForSet().members(key);
}
public static T randomMember(String key) {
return (T) template.opsForSet().randomMember(key);
}
public static List randomMembers(String key, long count) {
return (List) template.opsForSet().randomMembers(key, count);
}
public static Set distinctRandomMembers(String key, long count) {
return (Set) template.opsForSet().distinctRandomMembers(key, count);
}
public static Cursor scan(String key, ScanOptions options) {
return (Cursor) template.opsForSet().scan(key, options);
}
}
public static class ZSetOps {
public static Boolean add(String key, T value, double score) {
return template.opsForZSet().add(key, value, score);
}
public static Long add(String key, Set> values) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.add(key, values);
}
public static Long remove(String key, T... values) {
return template.opsForZSet().remove(key, values);
}
public static Double incrementScore(String key, T value, double delta) {
return template.opsForZSet().incrementScore(key, value, delta);
}
public static Long rank(String key, T value) {
return template.opsForZSet().rank(key, value);
}
public static Long reverseRank(String key, T value) {
return template.opsForZSet().reverseRank(key, value);
}
public static Set range(String key, long start, long end) {
return (Set) template.opsForZSet().range(key, start, end);
}
public static Set> rangeWithScores(String key, long start,
long end) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.rangeWithScores(key, start, end);
}
public static Set rangeByScore(String key, double min, double max) {
return (Set) template.opsForZSet().rangeByScore(key, min, max);
}
public static Set> rangeByScoreWithScores(String key, double min, double max) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.rangeByScoreWithScores(key, min, max);
}
public static Set> rangeByScoreWithScores(String key, double min, double max,
long start, long end) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.rangeByScoreWithScores(key, min, max, start, end);
}
public static Set reverseRange(String key, long start, long end) {
return (Set) template.opsForZSet().reverseRange(key, start, end);
}
public static Set> reverseRangeWithScores(String key, long start, long end) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.reverseRangeWithScores(key, start, end);
}
public static Set reverseRangeByScore(String key, double min, double max) {
return (Set) template.opsForZSet().reverseRangeByScore(key, min, max);
}
public static Set> reverseRangeByScoreWithScores(String key, double min,
double max) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.reverseRangeByScoreWithScores(key, min, max);
}
public static Set reverseRangeByScore(String key, double min, double max, long start, long end) {
return (Set) template.opsForZSet().reverseRangeByScore(key, min, max, start, end);
}
public static Long count(String key, double min, double max) {
return template.opsForZSet().count(key, min, max);
}
public static Long size(String key) {
return template.opsForZSet().size(key);
}
public static Long zCard(String key) {
return template.opsForZSet().zCard(key);
}
public static Double score(String key, T value) {
return template.opsForZSet().score(key, value);
}
public static Long removeRange(String key, long start, long end) {
return template.opsForZSet().removeRange(key, start, end);
}
public static Long removeRangeByScore(String key, double min, double max) {
return template.opsForZSet().removeRangeByScore(key, min, max);
}
public static Long unionAndStore(String key, String otherKey, String destKey) {
return template.opsForZSet().unionAndStore(key, otherKey, destKey);
}
public static Long unionAndStore(String key, Collection otherKeys, String destKey) {
return template.opsForZSet().unionAndStore(key, otherKeys, destKey);
}
public static Long intersectAndStore(String key, String otherKey, String destKey) {
return template.opsForZSet().intersectAndStore(key, otherKey, destKey);
}
public static Long intersectAndStore(String key, Collection otherKeys, String destKey) {
return template.opsForZSet().intersectAndStore(key, otherKeys, destKey);
}
public static Cursor> scan(String key, ScanOptions options) {
ZSetOperations ops = (ZSetOperations) template.opsForZSet();
return ops.scan(key, options);
}
}
}
5装配工具类
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.alan.lawyer.redis.autoconfigure.RedisAutoConfiguration
6监听
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
String expiredKey = message.toString();
System.out.println(expiredKey);
}
}
7监听配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}



