feat: add redis until
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.schisandra.auth.common.redis;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Redis的config处理
|
||||
*
|
||||
* @author: landaiqing
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
redisTemplate.setKeySerializer(redisSerializer);
|
||||
redisTemplate.setHashKeySerializer(redisSerializer);
|
||||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());
|
||||
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
|
||||
Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
||||
jsonRedisSerializer.setObjectMapper(objectMapper);
|
||||
return jsonRedisSerializer;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
package com.schisandra.auth.common.redis;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* RedisUtil工具类
|
||||
*
|
||||
* @author: landaiqing
|
||||
* @date: 2024/2/19
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RedisUtil {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
private static final String CACHE_KEY_SEPARATOR = ".";
|
||||
|
||||
/**
|
||||
* 构建缓存key
|
||||
*/
|
||||
public String buildKey(String... strObjs) {
|
||||
return Stream.of(strObjs).collect(Collectors.joining(CACHE_KEY_SEPARATOR));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在key
|
||||
*/
|
||||
public boolean exist(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除key
|
||||
*/
|
||||
public boolean del(String key) {
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* set(不带过期)
|
||||
*/
|
||||
public void set(String key, String value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* set(带过期)
|
||||
*/
|
||||
public boolean setNx(String key, String value, Long time, TimeUnit timeUnit) {
|
||||
return redisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取string类型缓存
|
||||
*/
|
||||
public String get(String key) {
|
||||
return (String) redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public Boolean zAdd(String key, String value, Long score) {
|
||||
return redisTemplate.opsForZSet().add(key, value, Double.valueOf(String.valueOf(score)));
|
||||
}
|
||||
|
||||
public Long countZset(String key) {
|
||||
return redisTemplate.opsForZSet().size(key);
|
||||
}
|
||||
|
||||
public Set<String> rangeZset(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().range(key, start, end);
|
||||
}
|
||||
|
||||
public Long removeZset(String key, Object value) {
|
||||
return redisTemplate.opsForZSet().remove(key, value);
|
||||
}
|
||||
|
||||
public void removeZsetList(String key, Set<String> value) {
|
||||
value.stream().forEach((val) -> redisTemplate.opsForZSet().remove(key, val));
|
||||
}
|
||||
|
||||
public Double score(String key, Object value) {
|
||||
return redisTemplate.opsForZSet().score(key, value);
|
||||
}
|
||||
|
||||
public Set<String> rangeByScore(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, Double.valueOf(String.valueOf(start)), Double.valueOf(String.valueOf(end)));
|
||||
}
|
||||
|
||||
public Object addScore(String key, Object obj, double score) {
|
||||
return redisTemplate.opsForZSet().incrementScore(key, obj, score);
|
||||
}
|
||||
|
||||
public Object rank(String key, Object obj) {
|
||||
return redisTemplate.opsForZSet().rank(key, obj);
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user