1.redis工具类
1.导入pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.填写配置文件
spring.redis.host=your_redis_host
spring.redis.port=your_redis_port
3.代码
package com.light.hexo.common.base;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtils {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
return new LettuceConnectionFactory(config);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
private static RedisTemplate<String, Object> redisTemplate;
public RedisUtils(RedisTemplate<String, Object> redisTemplate) {
RedisUtils.redisTemplate = redisTemplate;
}
public static Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public static void put(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public static void put(String key, Object value, Long timeout) {
put(key, value, Duration.ofSeconds(timeout));
}
public static void put(String key, Object value, Duration timeout) {
redisTemplate.opsForValue().set(key, value, timeout);
}
public static Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
public static void expire(String key, Long timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
public static void expireAt(String key, Date date) {
redisTemplate.expireAt(key, date);
}
public static Boolean delete(String key) {
return redisTemplate.delete(key);
}
public static void hashSet(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
public static Object hashGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
public static Map<Object, Object> entries(String key) {
return redisTemplate.opsForHash().entries(key);
}
public static Set<Object> hashKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
public static List<Object> hashValues(String key) {
return redisTemplate.opsForHash().values(key);
}
public static boolean hasKey(String key, String field) {
return redisTemplate.opsForHash().hasKey(key, field);
}
public static List hashMultiGet(String key, Collection fieldList) {
return redisTemplate.opsForHash().multiGet(key, fieldList);
}
public static Cursor<Map.Entry<Object, Object>> hScan(String key, ScanOptions options) {
return redisTemplate.opsForHash().scan(key, options);
}
public static Long delete(List<String> keys) {
return redisTemplate.delete(keys);
}
public static Long hashDelete(String key, List<String> fields) {
return redisTemplate.opsForHash().delete(key, fields.toArray());
}
public static Long increment(String key) {
return redisTemplate.opsForValue().increment(key);
}
public static Long increment(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
public static Double increment(String key, double delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
public static Long hashIncrement(String key, String field, long delta) {
return redisTemplate.opsForHash().increment(key, field, delta);
}
public static Long decrement(String key) {
return redisTemplate.opsForValue().decrement(key);
}
public static Long decrement(String key, long delta) {
return redisTemplate.opsForValue().decrement(key, delta);
}
public static Object rightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
public static void rightPush(String key, Object value) {
redisTemplate.opsForList().rightPush(key, value);
}
public static Long rightPushAll(String key, Object... values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
public static Object leftPop(String key) {
return redisTemplate.opsForList().leftPop(key);
}
public static Long leftPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
public static Long leftPushAll(String key, Object... values) {
return redisTemplate.opsForList().leftPushAll(key, values);
}
public static Long listSize(String key) {
return redisTemplate.opsForList().size(key);
}
public static List<Object> listRange(String key, int start, int end) {
return redisTemplate.opsForList().range(key, start, end);
}
public static void listTrim(String key, int start, int end) {
redisTemplate.opsForList().trim(key, start, end);
}
public static List<Object> multiGet(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
public static Boolean zAdd(String key, Object member, double score) {
return redisTemplate.opsForZSet().add(key, member, score);
}
public static Long zAdd(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
return redisTemplate.opsForZSet().add(key, tuples);
}
public static Set<ZSetOperations.TypedTuple<Object>> zRange(String key, long start, long end) {
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
}
public static Set<ZSetOperations.TypedTuple<Object>> zRangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
}
public static Set<ZSetOperations.TypedTuple<Object>> zRangeByScore(String key, double min, double max, int offset, int count) {
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max, offset, count);
}
public static Long removeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
}
public static Long zSetRemove(String key, Object... values) {
return redisTemplate.opsForZSet().remove(key, values);
}
public static Set<ZSetOperations.TypedTuple<Object>> zReverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
}
public static Set<ZSetOperations.TypedTuple<Object>> zReverseRangeByScoreWithScores(String key, double min, double max) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
}
public static Set<ZSetOperations.TypedTuple<Object>> zReverseRangeByScore(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}
public static Double score(String key, Object value) {
return redisTemplate.opsForZSet().score(key, value);
}
public static Double incrementScore(String key, Object value, double delta) {
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
}
public static Long zSize(String key) {
return redisTemplate.opsForZSet().size(key);
}
public static Long zCount(String key, double min, double max) {
return redisTemplate.opsForZSet().count(key, min, max);
}
public static Long listRem(String key, long count, Object value) {
return redisTemplate.opsForList().remove(key, count, value);
}
public static Long zRemByRank(String key, long start, long end) {
return redisTemplate.opsForZSet().removeRange(key, start, end);
}
public static Map<Object, Object> hashAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
}