欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

RedisTemplate使用详解

时间:2023-06-13

目录

一、Spring Data Redis

​1、Spring-data-redis简介

2、Spring-data-redis 针对 Jedis 提供的功能

二、SpringBoot整合Redis加入Redis依赖及序列化依赖

三、增加Redis配置类,为key、value配置序列化方式

四、配置Redis工具类,将RedisTemplate的API统一管理

五、RedisTemplate针对Redis五种数据类型的操作

1、RedisTemplate操作string类型数据

2、RedisTemplate操作hash类型数据

3、RedisTemplate操作list类型数据

4、RedisTemplate操作set类型数据

5、RedisTemplate操作zset类型数据


一、Spring Data Redis

​1、Spring-data-redis简介

​Spring-data-redis提供了在Spring应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,封装了 RedisTemplate 对象来进行对Redis的各种操作、异常处理及序列化,支持发布订阅,并对Spring 3.1 cache进行了实现,它支持所有的Redis原生的 API。

2、Spring-data-redis 针对 Jedis 提供的功能

1、连接池自动管理,提供了一个高度封装的“RedisTemplate”类;
2、针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

ValueOperations:简单的K-V操作SetOperations:set类型数据操作ZSetOperations:zset类型数据操作HashOperations:针对map类型的数据操作ListOperations:针对list类型的数据操作

3、提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:

BoundValueOperationsBoundSetOperationsBoundListOperationsBoundSetOperationsBoundHashOperations

4、将事务操作封装,有容器控制。
5、针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)

JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。

在项目中,我们经常会用到RedisTemplate来操作Redis,本文详细罗列了RedisTemplate操作Redis的方法。

二、SpringBoot整合Redis加入Redis依赖及序列化依赖

org.springframework.boot spring-boot-starter-data-redis com.alibaba fastjson 1.2.76

三、增加Redis配置类,为key、value配置序列化方式

package com.test.redis.redis;import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;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.core.StringRedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); // fastjson序列化 FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); // key的序列化采用StringRedisSerializer redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // value的序列化采用FastJsonRedisSerializer redisTemplate.setValueSerializer(fastJsonRedisSerializer); redisTemplate.setHashValueSerializer(fastJsonRedisSerializer); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(connectionFactory); return stringRedisTemplate; }}

四、配置Redis工具类,将RedisTemplate的API统一管理

public class RedisUtil { @Autowired private RedisTemplate redisTemplate;}

五、RedisTemplate针对Redis五种数据类型的操作

1、RedisTemplate操作string类型数据

public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public void setWithExpire(String key, Object value, Long seconds, TimeUnit timeUnit) { redisTemplate.opsForValue().set(key, value, seconds, timeUnit); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public Boolean hasKey(String key) { return redisTemplate.hasKey(key); } public Boolean del(String key) { return redisTemplate.delete(key); } public Long batchDel(Collection keys) { return redisTemplate.delete(keys); } public byte[] dump(String key) { return redisTemplate.dump(key); } public Boolean expire(String key, long seconds) { return redisTemplate.expire(key, seconds, TimeUnit.SECONDS); } public Boolean expireAt(String key, Date date) { return redisTemplate.expireAt(key, date); } public Set getStringKeys(String key) { return redisTemplate.keys(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 DataType type(String key) { return redisTemplate.type(key); } public String randomKey() { return redisTemplate.randomKey(); } public Long getExpire(String key) { return redisTemplate.getExpire(key); } public Long getExpire(String key, TimeUnit timeUnit) { return redisTemplate.getExpire(key, timeUnit); } public Boolean persist(String key) { return redisTemplate.persist(key); } public Boolean move(String key, int dbIndex) { return redisTemplate.move(key, dbIndex); } public String subString(String key, long start, long end) { return redisTemplate.opsForValue().get(key, start, end); } public Object getAndSet(String key, Object value) { return redisTemplate.opsForValue().getAndSet(key, value); } public List multiGetSet(List keys) { return redisTemplate.opsForValue().multiGet(keys); } public Integer append(String key, String value) { return redisTemplate.opsForValue().append(key, value); } public Long increment(String key) { return redisTemplate.opsForValue().increment(key); } public Long incrementLong(String key, long increment) { return redisTemplate.opsForValue().increment(key, increment); } public void incrementDouble(String key, double increment) { redisTemplate.opsForValue().increment(key, increment); } public Boolean multiSetIfAbsent(Map<? extends String, ?> map) { return redisTemplate.opsForValue().multiSetIfAbsent(map); } public void multiSet(Map map) { redisTemplate.opsForValue().multiSet(map); } public List multiGet(List keys) { return redisTemplate.opsForValue().multiGet(keys); } public Long sizeString(String key) { return redisTemplate.opsForValue().size(key); } public void offsetValue(String key, Object value, Long offset) { redisTemplate.opsForValue().set(key, value, offset); } public Boolean getOffsetValue(String key, Long offset) { return redisTemplate.opsForValue().getBit(key, offset); } public Boolean setIfAbsent(String key, Object value) { return redisTemplate.opsForValue().setIfAbsent(key, value); }

2、RedisTemplate操作hash类型数据

public void put(String key, Object field, Object value) { redisTemplate.opsForHash().put(key, field, value); } public void putAll(String key, Map map) { redisTemplate.opsForHash().putAll(key, map); } public Object getMapValue(String key, String field) { return redisTemplate.opsForHash().get(key, field); } public Map getMap(String key) { return redisTemplate.opsForHash().entries(key); } public Boolean putIfAbsent(String key, Object hashKey, Object value) { return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value); } public Long del(String key, List fields) { return redisTemplate.opsForHash().delete(key, fields); } public Boolean hasKey(String key, Object field) { return redisTemplate.opsForHash().hasKey(key, field); } public Long incrementLong(String key, Object field, long increment) { return redisTemplate.opsForHash().increment(key, field, increment); } public Double incrementDouble(String key, Object field, double increment) { return redisTemplate.opsForHash().increment(key, field, increment); } public Set keys(String key) { return redisTemplate.opsForHash().keys(key); } public Long sizeHash(String key) { return redisTemplate.opsForHash().size(key); } public List values(String key) { return redisTemplate.opsForHash().values(key); } public Cursor> scan(String key, ScanOptions scanOptions) { return redisTemplate.opsForHash().scan(key, scanOptions); }

3、RedisTemplate操作list类型数据

public Long leftPush(String key, Object value) { return redisTemplate.opsForList().leftPush(key, value); } public Long leftPush(String key, Object..、values) { return redisTemplate.opsForList().leftPushAll(key, values); } public Long leftPushAll(String key, List value) { return redisTemplate.opsForList().leftPushAll(key, value); } public long leftPushIfPresent(String key, Object value) { return redisTemplate.opsForList().leftPushIfPresent(key, value); } public long rightPush(String key, Object value) { return redisTemplate.opsForList().rightPush(key, value); } public long rightPushAll(String key, Object..、values) { return redisTemplate.opsForList().rightPushAll(key, values); } public long rightPushAll(String key, List values) { return redisTemplate.opsForList().rightPushAll(key, values); } public Object index(String key, long index) { return redisTemplate.opsForList().index(key, index); } public List range(String key, long start, long end) { return redisTemplate.opsForList().range(key, start, end); } public Object leftPop(String key) { return redisTemplate.opsForList().leftPop(key); } public Object leftPop(String key, Long seconds, TimeUnit timeUnit) { return redisTemplate.opsForList().leftPop(key, seconds, timeUnit); } public Object rightPop(String key) { return redisTemplate.opsForList().rightPop(key); } public Object rightPop(String key, Long seconds, TimeUnit timeUnit) { return redisTemplate.opsForList().rightPop(key, seconds, timeUnit); } public Object rightPopAndLeftPush(String sourceKey, String destinationKey) { return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey); } public Object rightPopAndLeftPush(String sourceKey, String destinationKey, long seconds, TimeUnit timeUnit) { return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, seconds, timeUnit); } public Long sizeList(String key) { return redisTemplate.opsForList().size(key); } public void trim(String key, long start, long end) { redisTemplate.opsForList().trim(key, start, end); } public Long remove(String key, long index, Object value) { return redisTemplate.opsForList().remove(key, index, value); }

4、RedisTemplate操作set类型数据

public Long add(String key, Collection values) { return redisTemplate.opsForSet().add(key, values); } public Object pop(String key) { return redisTemplate.opsForSet().pop(key); } public Long sizeSet(String key) { return redisTemplate.opsForSet().size(key); } public Boolean isMember(String key, Object value) { return redisTemplate.opsForSet().isMember(key, value); } public Set intersect(String key, String otherKey) { return redisTemplate.opsForSet().intersect(key, otherKey); } public Set intersect(String key, Collection collection) { return redisTemplate.opsForSet().intersect(key, collection); } public Long intersectAndStore(String key1, String key2, String destKey) { return redisTemplate.opsForSet().intersectAndStore(key1, key2, destKey); } public Set union(String key, String key1) { return redisTemplate.opsForSet().union(key, key1); } public Set union(String key, Collection collection) { return redisTemplate.opsForSet().union(key, collection); } public Long unionAndStore(String key, String key1, String destKey) { return redisTemplate.opsForSet().unionAndStore(key, key1, destKey); } public Set difference(String key, String key1) { return redisTemplate.opsForSet().difference(key, key1); } public Set difference(String key, Collection collection) { return redisTemplate.opsForSet().difference(key, collection); } public Long differenceAndStore(String key, String key1, String destKey) { return redisTemplate.opsForSet().differenceAndStore(key, key1, destKey); } public Set members(String key) { return redisTemplate.opsForSet().members(key); } public Object randomMember(String key) { return redisTemplate.opsForSet().randomMember(key); } public List randomMembers(String key, long count) { return redisTemplate.opsForSet().randomMembers(key, count); } public Set distinctRandomMembers(String key, long count) { return redisTemplate.opsForSet().distinctRandomMembers(key, count); } public Cursor scanSet(String key, ScanOptions scanOptions) { return redisTemplate.opsForSet().scan(key, scanOptions); } public Long remove(String key, Collection objects) { return redisTemplate.opsForSet().remove(key, objects); }

5、RedisTemplate操作zset类型数据

public Boolean add(String key, Object value, double score) { return redisTemplate.opsForZSet().add(key, value, score); } public Double incrementScore(String key, Object value, double score) { return redisTemplate.opsForZSet().incrementScore(key, value, score); } public Long rank(String key, Object object) { return redisTemplate.opsForZSet().rank(key, object); } public Long reverseRank(String key, Object object) { return redisTemplate.opsForZSet().reverseRank(key, object); } public Set> reverseRangeWithScores(String key, long start, long end) { return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end); } public Set reverseRangeByScore(String key, double min, double max) { return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max); } public Set reverseRangeByScore(String key, double min, double max, long start, long end) { return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, start, end); } public Set> reverseRangeByScoreWithScores(String key, double min, double max) { return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max); } public Long count(String key, double min, double max) { return redisTemplate.opsForZSet().count(key, min, max); } public Long sizeZset(String key) { return redisTemplate.opsForZSet().size(key); } public Long zCard(String key) { return redisTemplate.opsForZSet().zCard(key); } public Double score(String key, Object value) { return redisTemplate.opsForZSet().score(key, value); } public Long removeRange(String key, long start, long end) { return redisTemplate.opsForZSet().removeRange(key, start, end); } public Long removeRangeByScore(String key, double min, double max) { return redisTemplate.opsForZSet().removeRangeByScore(key, min, max); } public Long zSetUnionAndStore(String key, String key1, String destKey) { return redisTemplate.opsForZSet().unionAndStore(key, key1, destKey); } public Long zSetUnionAndStore(String key, Collection collection, String destKey) { return redisTemplate.opsForZSet().unionAndStore(key, collection, destKey); } public Long zSetIntersectAndStore(String key, String key1, String destKey) { return redisTemplate.opsForZSet().intersectAndStore(key, key1, destKey); } public Long zSetIntersectAndStore(String key, Collection collection, String destKey) { return redisTemplate.opsForZSet().intersectAndStore(key, collection, destKey); } public Long remove(String key, Object..、values) { return redisTemplate.opsForZSet().remove(key, values); }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。