1.环境
<!-- RedisTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 192.168.8.128
port: 6380
password: 1234
database: 0
timeout: 3000
jedis:
pool:
max-wait: -1
max-active: -1
max-idle: 20
min-idle: 10
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/664ea658dfa2755eb045e1a14798a6ef.png)
2.配置
package com.yzm.redis02.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class ObjectMapperConfig {
private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";
@Bean(name = "myObjectMapper")
public ObjectMapper objectMapper() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
return new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setDateFormat(new SimpleDateFormat(PATTERN))
.registerModule(javaTimeModule)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
;
}
static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(DateTimeFormatter.ofPattern(PATTERN)));
}
}
static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(PATTERN));
}
}
}
package com.yzm.redis02.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.time.Duration;
@Configuration
public class RedisConfig {
@Resource(name = "myObjectMapper")
private ObjectMapper objectMapper;
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<Object> jacksonSerializer = jackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(jacksonSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(jacksonSerializer);
template.afterPropertiesSet();
return template;
}
private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
Jackson2JsonRedisSerializer<Object> jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
jacksonSerializer.setObjectMapper(objectMapper);
return jacksonSerializer;
}
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
3.Common
package com.yzm.redis02.utils;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisCommonUtils {
private final RedisTemplate<String, Object> redisTemplate;
public RedisCommonUtils(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
public Long delete(Collection<String> keys) {
return redisTemplate.delete(keys);
}
public Boolean unlink(String key) {
return redisTemplate.unlink(key);
}
public Long unlink(Collection<String> keys) {
return redisTemplate.unlink(keys);
}
public Boolean expire(String key, long time, TimeUnit timeUnit) {
return redisTemplate.expire(key, time, timeUnit);
}
public Boolean expire(String key, Duration duration) {
return redisTemplate.expire(key, duration);
}
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
public Boolean persist(String key) {
return redisTemplate.persist(key);
}
public void rename(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
public Boolean renameIfAbsent(String oldKey, String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
public Long getExpire(String key, TimeUnit timeUnit) {
return redisTemplate.getExpire(key, timeUnit);
}
public DataType type(String key) {
return redisTemplate.type(key);
}
public String randomKey() {
return redisTemplate.randomKey();
}
}
4.String
package com.yzm.redis02.utils;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisStringUtils {
private final ValueOperations<String, Object> valueOperations;
public RedisStringUtils(ValueOperations<String, Object> valueOperations) {
this.valueOperations = valueOperations;
}
public void set(String key, Object value) {
valueOperations.set(key, value);
}
public void set(String key, Object value, long time, TimeUnit timeUnit) {
valueOperations.set(key, value, time, timeUnit);
}
public void set(String key, Object value, Duration duration) {
valueOperations.set(key, value, duration);
}
public Boolean setIfAbsent(String key, Object value) {
return valueOperations.setIfAbsent(key, value);
}
public Boolean setIfAbsent(String key, Object value, long time, TimeUnit timeUnit) {
return valueOperations.setIfAbsent(key, value, time, timeUnit);
}
public Boolean setIfAbsent(String key, Object value, Duration duration) {
return valueOperations.setIfAbsent(key, value, duration);
}
public Boolean setIfPresent(String key, Object value) {
return valueOperations.setIfPresent(key, value);
}
public Boolean setIfPresent(String key, Object value, long time, TimeUnit timeUnit) {
return valueOperations.setIfPresent(key, value, time, timeUnit);
}
public Boolean setIfPresent(String key, Object value, Duration duration) {
return valueOperations.setIfPresent(key, value, duration);
}
public Object getAndSet(String key, Object newValue) {
return valueOperations.getAndSet(key, newValue);
}
public void multiSet(Map<String, Object> map) {
valueOperations.multiSet(map);
}
public Boolean multiSetIfAbsent(Map<String, Object> map) {
return valueOperations.multiSetIfAbsent(map);
}
public Object get(String key) {
return valueOperations.get(key);
}
public List<Object> multiGet(Collection<String> keys) {
return valueOperations.multiGet(keys);
}
public Long increment(String key) {
return valueOperations.increment(key);
}
public Long increment(String key, long incValue) {
return valueOperations.increment(key, incValue);
}
public Double increment(String key, double incValue) {
return valueOperations.increment(key, incValue);
}
}
5.List
package com.yzm.redis02.utils;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
@Component
public class RedisListUtils {
private final ListOperations<String, Object> listOperations;
public RedisListUtils(ListOperations<String, Object> listOperations) {
this.listOperations = listOperations;
}
public Long leftPush(String key, Object var) {
return listOperations.leftPush(key, var);
}
public Long leftPushAll(String key, Object... vars) {
return listOperations.leftPushAll(key, vars);
}
public Long leftPushAll(String key, Collection<Object> vars) {
return listOperations.leftPushAll(key, vars);
}
public Long leftPushIfPresent(String key, Object var) {
return listOperations.leftPushIfPresent(key, var);
}
public Long leftPush(String key, Object var1, Object var2) {
return listOperations.leftPush(key, var1, var2);
}
public Long rightPush(String key, Object var) {
return listOperations.rightPush(key, var);
}
public Long rightPushAll(String key, Object... vars) {
return listOperations.rightPushAll(key, vars);
}
public Long rightPushAll(String key, Collection<Object> vars) {
return listOperations.rightPushAll(key, vars);
}
public Long rightPushIfPresent(String key, Object var) {
return listOperations.rightPushIfPresent(key, var);
}
public Long rightPush(String key, Object var1, Object var2) {
return listOperations.rightPush(key, var1, var2);
}
public Object index(String key, long index) {
return listOperations.index(key, index);
}
public List<Object> range(String key, long start, long end) {
return listOperations.range(key, start, end);
}
public Long indexOf(String key, Object var) {
return listOperations.indexOf(key, var);
}
public Long lastIndexOf(String key, Object var) {
return listOperations.lastIndexOf(key, var);
}
public Long size(String key) {
return listOperations.size(key);
}
public void set(String key, long index, Object var) {
listOperations.set(key, index, var);
}
public Long remove(String key, long count, Object var) {
return listOperations.remove(key, count, var);
}
public void trim(String key, long start, long end) {
listOperations.trim(key, start, end);
}
public Object leftPop(String key) {
return listOperations.leftPop(key);
}
public Object rightPop(String key) {
return listOperations.rightPop(key);
}
public Object rightPopAndLeftPush(String key1, String key2) {
return listOperations.rightPopAndLeftPush(key1, key2);
}
}
6.Hash
package com.yzm.redis02.utils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
public class RedisHashUtils {
private final HashOperations<String, String, Object> hashOperations;
public RedisHashUtils(HashOperations<String, String, Object> hashOperations) {
this.hashOperations = hashOperations;
}
public void put(String k, String hk, Object hv) {
hashOperations.put(k, hk, hv);
}
public void putAll(String k, Map<String, Object> map) {
hashOperations.putAll(k, map);
}
public Boolean putIfAbsent(String k, String hk, Object hv) {
return hashOperations.putIfAbsent(k, hk, hv);
}
public Object get(String k, String hk) {
return hashOperations.get(k, hk);
}
public List<Object> multiGet(String k, Collection<String> hk) {
return hashOperations.multiGet(k, hk);
}
public Set<String> keys(String k) {
return hashOperations.keys(k);
}
public List<Object> values(String k) {
return hashOperations.values(k);
}
public Map<String, Object> entries(String k) {
return hashOperations.entries(k);
}
public Long size(String k) {
return hashOperations.size(k);
}
public Long increment(String k, String hk, long inc) {
return hashOperations.increment(k, hk, inc);
}
public Double increment(String k, String hk, double inc) {
return hashOperations.increment(k, hk, inc);
}
public Long delete(String k, Object... hvs) {
return hashOperations.delete(k, hvs);
}
public Boolean hasKey(String k, Object hv) {
return hashOperations.hasKey(k, hv);
}
}
7.Set
package com.yzm.redis02.utils;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@Component
public class RedisSetUtils {
private final SetOperations<String, Object> setOperations;
public RedisSetUtils(SetOperations<String, Object> setOperations) {
this.setOperations = setOperations;
}
public Long add(String key, Object... vars) {
return setOperations.add(key, vars);
}
public Set<Object> members(String key) {
return setOperations.members(key);
}
public Object randomMember(String key) {
return setOperations.randomMember(key);
}
public List<Object> randomMembers(String key, long count) {
return setOperations.randomMembers(key, count);
}
public Set<Object> distinctRandomMembers(String key, long count) {
return setOperations.distinctRandomMembers(key, count);
}
public Object pop(String key) {
return setOperations.pop(key);
}
public List<Object> pop(String key, long count) {
return setOperations.pop(key, count);
}
public Long size(String key) {
return setOperations.size(key);
}
public Long remove(String key, Object... vars) {
return setOperations.remove(key, vars);
}
public Boolean move(String key1, Object var, String key2) {
return setOperations.move(key1, var, key2);
}
public Boolean isMember(String key, Object var) {
return setOperations.isMember(key, var);
}
public Set<Object> union(String key1, String key2) {
return setOperations.union(key1, key2);
}
public Set<Object> union(String key1, Collection<String> keys) {
return setOperations.union(key1, keys);
}
public Set<Object> union(Collection<String> keys) {
return setOperations.union(keys);
}
public Long unionAndStore(String key1, String key2, String dest) {
return setOperations.unionAndStore(key1, key2, dest);
}
public Long unionAndStore(String key1, Collection<String> keys, String dest) {
return setOperations.unionAndStore(key1, keys, dest);
}
public Long unionAndStore(Collection<String> keys, String dest) {
return setOperations.unionAndStore(keys, dest);
}
public Set<Object> intersect(String key1, String key2) {
return setOperations.intersect(key1, key2);
}
public Set<Object> intersect(String key1, Collection<String> keys) {
return setOperations.intersect(key1, keys);
}
public Set<Object> intersect(Collection<String> keys) {
return setOperations.intersect(keys);
}
public Long intersectAndStore(String key1, String key2, String dest) {
return setOperations.intersectAndStore(key1, key2, dest);
}
public Long intersectAndStore(String key1, Collection<String> keys, String dest) {
return setOperations.intersectAndStore(key1, keys, dest);
}
public Long intersectAndStore(Collection<String> keys, String dest) {
return setOperations.intersectAndStore(keys, dest);
}
public Set<Object> difference(String key1, String key2) {
return setOperations.difference(key1, key2);
}
public Set<Object> difference(String key1, Collection<String> keys) {
return setOperations.difference(key1, keys);
}
public Set<Object> difference(Collection<String> keys) {
return setOperations.difference(keys);
}
public Long differenceAndStore(String key1, String key2, String dest) {
return setOperations.differenceAndStore(key1, key2, dest);
}
public Long differenceAndStore(String key1, Collection<String> keys, String dest) {
return setOperations.differenceAndStore(key1, keys, dest);
}
public Long differenceAndStore(Collection<String> keys, String dest) {
return setOperations.differenceAndStore(keys, dest);
}
}
8.ZSet
package com.yzm.redis02.utils;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Set;
@Component
public class RedisZSetUtils {
private final ZSetOperations<String, Object> zSetOperations;
public RedisZSetUtils(ZSetOperations<String, Object> zSetOperations) {
this.zSetOperations = zSetOperations;
}
public Boolean add(String key, Object var, double score) {
return zSetOperations.add(key, var, score);
}
public Long add(String key, Set<ZSetOperations.TypedTuple<Object>> vars) {
return zSetOperations.add(key, vars);
}
public Long rank(String key, Object var) {
return zSetOperations.rank(key, var);
}
public Long reverseRank(String key, Object var) {
return zSetOperations.reverseRank(key, var);
}
public Set<Object> range(String key, long start, long end) {
return zSetOperations.range(key, start, end);
}
public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end) {
return zSetOperations.rangeWithScores(key, start, end);
}
public Set<Object> rangeByScore(String key, double min, double max) {
return zSetOperations.rangeByScore(key, min, max);
}
public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max) {
return zSetOperations.rangeByScoreWithScores(key, min, max);
}
public Set<Object> rangeByScore(String key, double min, double max, long offset, long count) {
return zSetOperations.rangeByScore(key, min, max, offset, count);
}
public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max, long offset, long count) {
return zSetOperations.rangeByScoreWithScores(key, min, max, offset, count);
}
public Set<Object> rangeByLex(String key, RedisZSetCommands.Range range) {
return zSetOperations.rangeByLex(key, range);
}
public Set<Object> rangeByLex(String key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) {
return zSetOperations.rangeByLex(key, range, limit);
}
public Set<Object> reverseRange(String key, long start, long end) {
return zSetOperations.reverseRange(key, start, end);
}
public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) {
return zSetOperations.reverseRangeWithScores(key, start, end);
}
public Set<Object> reverseRangeByScore(String key, double min, double max) {
return zSetOperations.reverseRangeByScore(key, min, max);
}
public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) {
return zSetOperations.reverseRangeByScoreWithScores(key, min, max);
}
public Set<Object> reverseRangeByScore(String key, double min, double max, long offset, long count) {
return zSetOperations.reverseRangeByScore(key, min, max, offset, count);
}
public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) {
return zSetOperations.reverseRangeByScoreWithScores(key, min, max, offset, count);
}
public Set<Object> reverseRangeByLex(String key, RedisZSetCommands.Range range) {
return zSetOperations.reverseRangeByLex(key, range);
}
public Set<Object> reverseRangeByLex(String key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) {
return zSetOperations.reverseRangeByLex(key, range, limit);
}
public Long count(String key, double min, double max) {
return zSetOperations.count(key, min, max);
}
public Long size(String key) {
return zSetOperations.size(key);
}
public Double score(String key, Object var) {
return zSetOperations.score(key, var);
}
public Double incrementScore(String key, Object var, double score) {
return zSetOperations.incrementScore(key, var, score);
}
public Long remove(String key, Object... vars) {
return zSetOperations.remove(key, vars);
}
public Long removeRange(String key, long start, long end) {
return zSetOperations.removeRange(key, start, end);
}
public Long removeRangeByScore(String key, double min, double max) {
return zSetOperations.removeRangeByScore(key, min, max);
}
public Long unionAndStore(String key1, String key2, String dest) {
return zSetOperations.unionAndStore(key1, key2, dest);
}
public Long unionAndStore(String key1, Collection<String> keys, String dest) {
return zSetOperations.unionAndStore(key1, keys, dest);
}
public Long unionAndStore(String key1, Collection<String> keys, String dest, RedisZSetCommands.Aggregate aggregate) {
return zSetOperations.unionAndStore(key1, keys, dest, aggregate);
}
public Long unionAndStore(String key1, Collection<String> keys, String dest, RedisZSetCommands.Aggregate aggregate, RedisZSetCommands.Weights weights) {
return zSetOperations.unionAndStore(key1, keys, dest, aggregate, weights);
}
public Long intersectAndStore(String key1, String key2, String dest) {
return zSetOperations.intersectAndStore(key1, key2, dest);
}
public Long intersectAndStore(String key1, Collection<String> keys, String dest) {
return zSetOperations.intersectAndStore(key1, keys, dest);
}
public Long intersectAndStore(String key1, Collection<String> keys, String dest, RedisZSetCommands.Aggregate aggregate) {
return zSetOperations.intersectAndStore(key1, keys, dest, aggregate);
}
public Long intersectAndStore(String key1, Collection<String> keys, String dest, RedisZSetCommands.Aggregate aggregate, RedisZSetCommands.Weights weights) {
return zSetOperations.intersectAndStore(key1, keys, dest, aggregate, weights);
}
}
相关链接
首页
上一篇:SQL缓存
下一篇:事务