RedisTemplate 相关的api使用。
http://www.45fan.com/article.php?aid=1D2ojkZ7gc23Gzro
RedisTemplateConfiguration.java
经过亲身测试,其实上无须加上这些配置,如果你加上的话,你用的是jackson的序列化,到时候获取到的对象就是Map类型,如果你要是用的fastjson的序列化,你获取到的就是jsonObject。反正是你获取不到原来的对象,还得想办法重新转化一下,如果你啥都不处理的话,那么你只需要强制转化下就可以了。
对应的配置 去上面的文章找
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
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.StringRedisSerializer;
@Configuration
public class RedisTemplateConfiguration {
/**
* redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
*
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置key和value的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
fastjson的序列化
https://blog.csdn.net/threelifeadv/article/details/122879037
给一个别人封装好的工具类,感觉挺全的,以后可以用这个工具类操作
工具类——RedisTemplateUtils
https://blog.csdn.net/threelifeadv/article/details/122879037
package com.example.demo.mideng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
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.stereotype.Component;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author: 公众号:干货食堂
* Project Name: demo
* File Name: RedisTemplateUtils
* Package Name: com.example.demo.mideng
* Date: 2022/7/2 17:29
* Copyright (c) 2022,All Rights Reserved.
* @Description
*/
@Component
public class RedisTemplateUtils {
@Autowired
private RedisTemplate redisTemplate;
//========================RedisTemplate操作string类型数据============================================
/**
* @param key
* @param value
* @return void
* @description string设置 key和 value的值
* @author chengjunyu
* @date 2022/2/11
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* @param key
* @param value
* @param seconds
* @param timeUnit
* @return void
* @description string设置 key和 value的值并设置过期时间和时间单位
* @author chengjunyu
* @date 2022/2/12
*/
public void setWithExpire(String key, Object value, Long seconds, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, seconds, timeUnit);
}
/**
* @param * @param key
* @return java.lang.Object
* @description string获取 key对应的 value值
* @author chengjunyu
* @date 2022/2/11
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* @param * @param key
* @return boolean
* @description 判断在 redis中是不是存在对应的 key值,有的话就返回 true,没有就返回 false
* @author chengjunyu
* @date 2022/2/11
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* @param key
* @return boolean
* @description 删除redis中对应的key值
* @author chengjunyu
* @date 2022/2/11
*/
public Boolean del(String key) {
return redisTemplate.delete(key);
}
/**
* @param keys
* @return long
* @description 批量删除 redis中对应的 key值,其中 keys是数组 keys:Collection<K> keys
* @author chengjunyu
* @date 2022/2/11
*/
public Long batchDel(Collection<String> keys) {
return redisTemplate.delete(keys);
}
/**
* @param key
* @return byte[]
* @description 把 key值序列化成 byte[]类型
* @author chengjunyu
* @date 2022/2/11
*/
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
/**
* @param * @param key
* @param seconds
* @return void
* @description 对传入的 key值设置过期时间
* @author chengjunyu
* @date 2022/2/11
*/
public Boolean expire(String key, long seconds) {
return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
/**
* @param * @param key
* @param date
* @return boolean
* @description 对传入的 key值设置过期日期
* @author chengjunyu
* @date 2022/2/11
*/
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
/**
* @param * @param key
* @return java.util.Set<java.lang.String>
* @description 模糊查询,返回一个没有重复的 Set类型
* @author chengjunyu
* @date 2022/2/11
*/
public Set<String> getStringKeys(String key) {
return redisTemplate.keys(key);
}
/**
* @param oldKey
* @param newKey
* @return void
* @description 根据新的 key的名称修改 redis中老的 key的名称
* @author chengjunyu
* @date 2022/2/12
*/
public void rename(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
/**
* @param oldKey
* @param newKey
* @return boolean
* @description 重命名旧的 key值
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean renameIfAbsent(String oldKey, String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
/**
* @param key
* @return org.springframework.data.redis.connection.DataType
* @description 获取 key值的类型
* @author chengjunyu
* @date 2022/2/12
*/
public DataType type(String key) {
return redisTemplate.type(key);
}
/**
* @param
* @return java.lang.String
* @description 随机从 redis中获取一个 key
* @author chengjunyu
* @date 2022/2/12
*/
public Object randomKey() {
return redisTemplate.randomKey();
}
/**
* @param key
* @return void
* @description 获取当前 key的剩下的过期时间
* @author chengjunyu
* @date 2022/2/12
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
/**
* @param key
* @param timeUnit
* @return java.lang.Long
* @description 获取 key剩余的过期时间,同时设置时间单位
* @author chengjunyu
* @date 2022/2/12
*/
public Long getExpire(String key, TimeUnit timeUnit) {
return redisTemplate.getExpire(key, timeUnit);
}
/**
* @param key
* @return java.lang.Boolean
* @description 将 key进行持久化
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean persist(String key) {
return redisTemplate.persist(key);
}
/**
* @param key
* @param dbIndex
* @return java.lang.Boolean
* @description 将当前数据库的 key移动到指定 redis中数据库当中
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean move(String key, int dbIndex) {
return redisTemplate.move(key, dbIndex);
}
/**
* @param key
* @param start
* @param end
* @return java.lang.String
* @description 截取 key的子字符串
* @author chengjunyu
* @date 2022/2/12
*/
public String subString(String key, long start, long end) {
return redisTemplate.opsForValue().get(key, start, end);
}
/**
* @param key
* @param value
* @return java.lang.Object
* @description 设置 key跟 value的值,同时获取 key的值
* @author chengjunyu
* @date 2022/2/12
*/
public Object getAndSet(String key, Object value) {
return redisTemplate.opsForValue().getAndSet(key, value);
}
/**
* @param keys
* @return java.util.List<java.lang.Object>
* @description 设置多个 key跟 value的值,同时获取 key的值
* @author chengjunyu
* @date 2022/2/12
*/
public List<Object> multiGetSet(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* @param * @param key
* @param value
* @return java.lang.Integer
* @description 获取原来的 key的值后在后面新增上新的字符串
* @author chengjunyu
* @date 2022/2/12
*/
public Integer append(String key, String value) {
return redisTemplate.opsForValue().append(key, value);
}
/**
* @param * @param key
* @return java.lang.Long
* @description value 值 +1
* @author chengjunyu
* @date 2022/2/12
*/
public Long increment(String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* @param key
* @param increment
* @return java.lang.Long
* @description 增量方式增加或减少 long值
* @author chengjunyu
* @date 2022/2/12
*/
public Long incrementLong(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
/**
* @param key
* @param increment
* @return void
* @description 增量方式增加double值
* @author chengjunyu
* @date 2022/2/12
*/
public void incrementDouble(String key, double increment) {
redisTemplate.opsForValue().increment(key, increment);
}
/**
* @param * @param map
* @return java.lang.Boolean
* @description 不存在即新增map的操作
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean multiSetIfAbsent(Map<? extends String, ?> map) {
return redisTemplate.opsForValue().multiSetIfAbsent(map);
}
/**
* @param * @param map
* @return void
* @description 保存 map集合
* @author chengjunyu
* @date 2022/2/12
*/
public void multiSet(Map<String, String> map) {
redisTemplate.opsForValue().multiSet(map);
}
/**
* @param * @param keys
* @return java.util.List<java.lang.Object>
* @description 获取 map集合
* @author chengjunyu
* @date 2022/2/12
*/
public List<Object> multiGet(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* @param key
* @return java.lang.Long
* @description 获取指定 key的字符串的长度
* @author chengjunyu
* @date 2022/2/12
*/
public Long sizeString(String key) {
return redisTemplate.opsForValue().size(key);
}
/**
* @param key
* @param value
* @param offset
* @return void
* @description 根据偏移量 offset 的值,覆盖重写 value的值
* @author chengjunyu
* @date 2022/2/12
*/
public void offsetValue(String key, Object value, Long offset) {
redisTemplate.opsForValue().set(key, value, offset);
}
/**
* @param key
* @param offset
* @return java.lang.Boolean
* @description 对 key所储存的字符串值,获取指定偏移量上的位(bit)
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean getOffsetValue(String key, Long offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
/**
* @param key
* @param value
* @return java.lang.Boolean
* @description 重新设置 key对应的值,如果存在返回 false,否则返回 true
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean setIfAbsent(String key, Object value) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
//========================RedisTemplate操作hash类型数据============================================
/**
* @param key
* @param field
* @param value
* @return void
* @description 新增map值
* @author chengjunyu
* @date 2022/2/12
*/
public void put(String key, Object field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
/**
* @param key
* @param map
* @return void
* @description 以map集合的形式添加键值对
* @author chengjunyu
* @date 2022/2/12
*/
public void putAll(String key, Map<Object, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* @param key
* @param field
* @return java.lang.Object
* @description 获取 map中指定的 key值,如果存在则返回值,没有就返回null
* @author chengjunyu
* @date 2022/2/12
*/
public Object getMapValue(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
/**
* @param * @param key
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @description 根据 key获取 Map对象
* @author chengjunyu
* @date 2022/2/12
*/
public Map<Object, Object> getMap(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* @param key
* @param hashKey
* @param value
* @return java.lang.Boolean
* @description 当 hashKey不存在的时候,进行设置 map的值
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean putIfAbsent(String key, Object hashKey, Object value) {
return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
}
/**
* @param key
* @param fields
* @return java.lang.Long
* @description 删除多个map的字段
* @author chengjunyu
* @date 2022/2/12
*/
public Long del(String key, List<Object> fields) {
return redisTemplate.opsForHash().delete(key, fields);
}
/**
* @param * @param key
* @param field
* @return java.lang.Boolean
* @description 查看 hash表中指定字段是否存在
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean hasKey(String key, Object field) {
return redisTemplate.opsForHash().hasKey(key, field);
}
/**
* @param key
* @param field
* @param increment
* @return java.lang.Long
* @description 给 map中指定字段的整数值加上 long型增量 increment
* @author chengjunyu
* @date 2022/2/12
*/
public Long incrementLong(String key, Object field, long increment) {
return redisTemplate.opsForHash().increment(key, field, increment);
}
/**
* @param key
* @param field
* @param increment
* @return java.lang.Double
* @description 给 map中指定字段的整数值加上 double型增量 increment
* @author chengjunyu
* @date 2022/2/12
*/
public Double incrementDouble(String key, Object field, double increment) {
return redisTemplate.opsForHash().increment(key, field, increment);
}
/**
* @param key
* @return java.util.Set<java.lang.Object>
* @description 获取 map中的所有字段
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> keys(String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* @param key
* @return java.util.Set<java.lang.Object>
* @description 获取 map中所有字段的数量
* @author chengjunyu
* @date 2022/2/12
*/
public Long sizeHash(String key) {
return redisTemplate.opsForHash().size(key);
}
/**
* @param key
* @return java.util.List<java.lang.Object>
* @description 获取hash表中存在的所有的值
* @author chengjunyu
* @date 2022/2/12
*/
public List<Object> values(String key) {
return redisTemplate.opsForHash().values(key);
}
/**
* @param key
* @param scanOptions
* @return org.springframework.data.redis.core.Cursor<java.util.Map.Entry < java.lang.Object, java.lang.Object>>
* @description 查看匹配的键值对
* @author chengjunyu
* @date 2022/2/12
*/
public Cursor<Map.Entry<Object, Object>> scan(String key, ScanOptions scanOptions) {
return redisTemplate.opsForHash().scan(key, scanOptions);
}
//========================RedisTemplate操作list类型数据============================================
/**
* @param key
* @param value
* @return java.lang.Long
* @description 把值添加在 list的最前面
* @author chengjunyu
* @date 2022/2/12
*/
public Long leftPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* @param key
* @param values
* @return java.lang.Long
* @description 把值添加在 list
* @author chengjunyu
* @date 2022/2/12
*/
public Long leftPush(String key, Object... values) {
return redisTemplate.opsForList().leftPushAll(key, values);
}
/**
* @param key
* @param value
* @return java.lang.Long
* @description 直接把一个新的 list添加到老的 list上面去
* @author chengjunyu
* @date 2022/2/12
*/
public Long leftPushAll(String key, List<Object> value) {
return redisTemplate.opsForList().leftPushAll(key, value);
}
/**
* @param key
* @param value
* @return long
* @description List存在的时候就加入新的值
* @author chengjunyu
* @date 2022/2/12
*/
public long leftPushIfPresent(String key, Object value) {
return redisTemplate.opsForList().leftPushIfPresent(key, value);
}
/**
* @param key
* @param value
* @return java.lang.Long
* @description 把值添加在 list的最后面
* @author chengjunyu
* @date 2022/2/12
*/
public long rightPush(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* @param key
* @param values
* @return long
* @description 把值添加在 list的最后面
* @author chengjunyu
* @date 2022/2/12
*/
public long rightPushAll(String key, Object... values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* @param key
* @param values
* @return long
* @description 把值添加在 list的最后面
* @author chengjunyu
* @date 2022/2/12
*/
public long rightPushAll(String key, List<Object> values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* @param key
* @param index
* @return java.lang.Object
* @description 根据索引获取 list中的值
* @author chengjunyu
* @date 2022/2/12
*/
public Object index(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
/**
* @param key
* @param start
* @param end
* @return java.util.List<java.lang.Object>
* @description 获取 list中开始索引到结束索引的所有值
* @author chengjunyu
* @date 2022/2/12
*/
public List<Object> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* @param key
* @return java.lang.Object
* @description 移除并获取列表中第一个元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object leftPop(String key) {
return redisTemplate.opsForList().leftPop(key);
}
/**
* @param key
* @param seconds
* @param timeUnit
* @return java.lang.Object
* @description 移除并获取列表中第一个元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object leftPop(String key, Long seconds, TimeUnit timeUnit) {
return redisTemplate.opsForList().leftPop(key, seconds, timeUnit);
}
/**
* @param key
* @return java.lang.Object
* @description 移除并获取列表中最后一个元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object rightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
/**
* @param key
* @param seconds
* @param timeUnit
* @return java.lang.Object
* @description 移除并获取列表中最后一个元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object rightPop(String key, Long seconds, TimeUnit timeUnit) {
return redisTemplate.opsForList().rightPop(key, seconds, timeUnit);
}
/**
* @param sourceKey
* @param destinationKey
* @return java.lang.Object
* @description 从一个队列的右边弹出一个元素并将这个元素放入另一个指定队列的最左边
* @author chengjunyu
* @date 2022/2/12
*/
public Object rightPopAndLeftPush(String sourceKey, String destinationKey) {
return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
}
/**
* @param sourceKey
* @param destinationKey
* @param seconds
* @param timeUnit
* @return java.lang.Object
* @description 从一个队列的右边弹出一个元素并将这个元素放入另一个指定队列的最左边
* @author chengjunyu
* @date 2022/2/12
*/
public Object rightPopAndLeftPush(String sourceKey, String destinationKey, long seconds, TimeUnit timeUnit) {
return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, seconds, timeUnit);
}
/**
* @param key
* @return java.lang.Long
* @description 获取 list的大小
* @author chengjunyu
* @date 2022/2/12
*/
public Long sizeList(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* @param key
* @param start
* @param end
* @return void
* @description 剪裁 List列表
* @author chengjunyu
* @date 2022/2/12
*/
public void trim(String key, long start, long end) {
redisTemplate.opsForList().trim(key, start, end);
}
/**
* @param key
* @param index
* @param value
* @return java.lang.Long
* @description 删除集合中值等于value的元素
* @author chengjunyu
* @date 2022/2/12
*/
public Long remove(String key, long index, Object value) {
return redisTemplate.opsForList().remove(key, index, value);
}
//========================RedisTemplate操作set类型数据============================================
/**
* @param key
* @param values
* @return java.lang.Long
* @description 添加元素到 set中
* @author chengjunyu
* @date 2022/2/12
*/
public Long add(String key, Collection<Object> values) {
return redisTemplate.opsForSet().add(key, values);
}
/**
* @param key
* @return java.lang.Object
* @description 从 set中删除一个随机元素,并返回该元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object pop(String key) {
return redisTemplate.opsForSet().pop(key);
}
/**
* @param key
* @return long
* @description 获取 set集合的大小
* @author chengjunyu
* @date 2022/2/12
*/
public Long sizeSet(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* @param key
* @param value
* @return java.lang.Boolean
* @description 判断 set集合中是否存在value值
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean isMember(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* @param key
* @param otherKey
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的交集并返回一个集合
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> intersect(String key, String otherKey) {
return redisTemplate.opsForSet().intersect(key, otherKey);
}
/**
* @param key
* @param collection
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的交集并返回一个集合
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> intersect(String key, Collection<String> collection) {
return redisTemplate.opsForSet().intersect(key, collection);
}
/**
* @param key1
* @param key2
* @param destKey
* @return java.lang.Long
* @description 获取两个集合交集,并存储到 destKey
* @author chengjunyu
* @date 2022/2/12
*/
public Long intersectAndStore(String key1, String key2, String destKey) {
return redisTemplate.opsForSet().intersectAndStore(key1, key2, destKey);
}
/**
* @param key
* @param key1
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的并集
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> union(String key, String key1) {
return redisTemplate.opsForSet().union(key, key1);
}
/**
* @param key
* @param collection
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的并集
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> union(String key, Collection<String> collection) {
return redisTemplate.opsForSet().union(key, collection);
}
/**
* @param key
* @param key1
* @param destKey
* @return java.lang.Long
* @description 获取两个集合的并集,并存储到 destKey
* @author chengjunyu
* @date 2022/2/12
*/
public Long unionAndStore(String key, String key1, String destKey) {
return redisTemplate.opsForSet().unionAndStore(key, key1, destKey);
}
/**
* @param key
* @param key1
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的差集
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> difference(String key, String key1) {
return redisTemplate.opsForSet().difference(key, key1);
}
/**
* @param key
* @param collection
* @return java.util.Set<java.lang.Object>
* @description 获取两个集合的差集
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> difference(String key, Collection<String> collection) {
return redisTemplate.opsForSet().difference(key, collection);
}
/**
* @param key
* @param key1
* @param destKey
* @return java.lang.Long
* @description 获取两个集合的差集,并存储到 destKey
* @author chengjunyu
* @date 2022/2/12
*/
public Long differenceAndStore(String key, String key1, String destKey) {
return redisTemplate.opsForSet().differenceAndStore(key, key1, destKey);
}
/**
* @param key
* @return java.util.Set<java.lang.Object>
* @description 获取集合中的所有元素
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> members(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* @param key
* @return java.lang.Object
* @description 随机获取集合中一个元素
* @author chengjunyu
* @date 2022/2/12
*/
public Object randomMember(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* @param key
* @param count
* @return java.util.List<java.lang.Object>
* @description 随机获取集合中 count个元素,返回一个 List集合
* @author chengjunyu
* @date 2022/2/12
*/
public List<Object> randomMembers(String key, long count) {
return redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* @param key
* @param count
* @return java.util.Set<java.lang.Object>
* @description 随机获取集合中 count个元素,去重后返回一个 Set集合
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> distinctRandomMembers(String key, long count) {
return redisTemplate.opsForSet().distinctRandomMembers(key, count);
}
/**
* @param key
* @param scanOptions
* @return org.springframework.data.redis.core.Cursor<java.lang.Object>
* @description 遍历 set
* @author chengjunyu
* @date 2022/2/12
*/
public Cursor<Object> scanSet(String key, ScanOptions scanOptions) {
return redisTemplate.opsForSet().scan(key, scanOptions);
}
/**
* @param key
* @param objects
* @return java.lang.Long
* @description 移除元素
* @author chengjunyu
* @date 2022/2/12
*/
public Long remove(String key, Collection<Object> objects) {
return redisTemplate.opsForSet().remove(key, objects);
}
//========================RedisTemplate操作zset类型数据============================================
/**
* @param key
* @param value
* @param score
* @return java.lang.Boolean
* @description 添加元素到 zset,从小到大排序
* @author chengjunyu
* @date 2022/2/12
*/
public Boolean add(String key, Object value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}
/**
* @param key
* @param value
* @param score
* @return java.lang.Double
* @description 增加元素的 score值同时返回增加后的 score值
* @author chengjunyu
* @date 2022/2/12
*/
public Double incrementScore(String key, Object value, double score) {
return redisTemplate.opsForZSet().incrementScore(key, value, score);
}
/**
* @param key
* @param object
* @return java.lang.Long
* @description 返回 zset元素在集合的从小到大排名
* @author chengjunyu
* @date 2022/2/12
*/
public Long rank(String key, Object object) {
return redisTemplate.opsForZSet().rank(key, object);
}
/**
* @param key
* @param object
* @return java.lang.Long
* @description 返回 zset元素在集合的由大到小排名
* @author chengjunyu
* @date 2022/2/12
*/
public Long reverseRank(String key, Object object) {
return redisTemplate.opsForZSet().reverseRank(key, object);
}
/**
* @param key
* @param start
* @param end
* @return java.util.Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple < java.lang.Object>>
* @description 获取 zset集合中指定区间的元素
* @author chengjunyu
* @date 2022/2/12
*/
public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}
/**
* @param key
* @param min
* @param max
* @return java.util.Set<java.lang.Object>
* @description 查询 zset集合中的元素并从小到大排序
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> reverseRangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
}
/**
* @param key
* @param min
* @param max
* @param start
* @param end
* @return java.util.Set<java.lang.Object>
* @description 从高到低进行排序,然后获取最小与最大值之间的值
* @author chengjunyu
* @date 2022/2/12
*/
public Set<Object> reverseRangeByScore(String key, double min, double max, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, start, end);
}
/**
* @param key
* @param min
* @param max
* @return java.util.Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple < java.lang.Object>>
* @description 查询 zset集合中的元素并从小到大排序
* @author chengjunyu
* @date 2022/2/12
*/
public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
}
/**
* @param key
* @param min
* @param max
* @return java.lang.Long
* @description 根据score值获取元素数量
* @author chengjunyu
* @date 2022/2/12
*/
public Long count(String key, double min, double max) {
return redisTemplate.opsForZSet().count(key, min, max);
}
/**
* @param key
* @return java.lang.Long
* @description 获取 zset集合的大小
* @author chengjunyu
* @date 2022/2/12
*/
public Long sizeZset(String key) {
return redisTemplate.opsForZSet().size(key);
}
/**
* @param key
* @return java.lang.Long
* @description 获取 zset集合的大小
* @author chengjunyu
* @date 2022/2/12
*/
public Long zCard(String key) {
return redisTemplate.opsForZSet().zCard(key);
}
/**
* @param key
* @param value
* @return java.lang.Double
* @description 获取集合中 key、value元素的 score值
* @author chengjunyu
* @date 2022/2/12
*/
public Double score(String key, Object value) {
return redisTemplate.opsForZSet().score(key, value);
}
/**
* @param key
* @param start
* @param end
* @return java.lang.Long
* @description 移除 zset中指定索引元素
* @author chengjunyu
* @date 2022/2/12
*/
public Long removeRange(String key, long start, long end) {
return redisTemplate.opsForZSet().removeRange(key, start, end);
}
/**
* @param key
* @param min
* @param max
* @return java.lang.Long
* @description 移除 zset中指定 score范围的集合成员
* @author chengjunyu
* @date 2022/2/12
*/
public Long removeRangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
}
/**
* @param key
* @param key1
* @param destKey
* @return java.lang.Long
* @description 获取 key和 key1的并集并存储在 destKey中
* @author chengjunyu
* @date 2022/2/12
*/
public Long zSetUnionAndStore(String key, String key1, String destKey) {
return redisTemplate.opsForZSet().unionAndStore(key, key1, destKey);
}
/**
* @param key
* @param collection
* @param destKey
* @return java.lang.Long
* @description 获取 key和 collection集合的并集并存储在 destKey中
* @author chengjunyu
* @date 2022/2/12
*/
public Long zSetUnionAndStore(String key, Collection<String> collection, String destKey) {
return redisTemplate.opsForZSet().unionAndStore(key, collection, destKey);
}
/**
* @param key
* @param key1
* @param destKey
* @return java.lang.Long
* @description 获取 key和 key1的交集并存储在 destKey中
* @author chengjunyu
* @date 2022/2/12
*/
public Long zSetIntersectAndStore(String key, String key1, String destKey) {
return redisTemplate.opsForZSet().intersectAndStore(key, key1, destKey);
}
/**
* @param key
* @param collection
* @param destKey
* @return java.lang.Long
* @description 获取 key和 collection集合的交集并存储在 destKey中
* @author chengjunyu
* @date 2022/2/12
*/
public Long zSetIntersectAndStore(String key, Collection<String> collection, String destKey) {
return redisTemplate.opsForZSet().intersectAndStore(key, collection, destKey);
}
/**
* @param key
* @param values
* @return java.lang.Long
* @description 删除多个values的值
* @author chengjunyu
* @date 2022/2/12
*/
public Long remove(String key, Object... values) {
return redisTemplate.opsForZSet().remove(key, values);
}
}
使用
@Autowired
private RedisTemplateUtils redisTemplateUtils;
Dog dog = new Dog();
dog.setName("zz");
dog.setColor("cc2");
redisTemplate.opsForValue().set("token",dog,30, TimeUnit.MINUTES);
Dog dd = (Dog)redisTemplateUtils.get("token");
System.out.println(dd);
API的使用
@Resource
private RedisTemplate redisTemplate;
set数据类型
获取set数据类型
Set<Integer> followers = redisTemplate.opsForSet()
.members(RedisKeyConstant.followers.getKey() + dinerId);
两个set类型key的交集
Set<Integer> dinerIds = redisTemplate.opsForSet().intersect(loginDinerKey, dinerKey);
添加数据
redisTemplate.opsForSet().add(RedisKeyConstant.following.getKey() + dinerId, followDinerId);
移除数据
redisTemplate.opsForSet().remove(RedisKeyConstant.following.getKey() + dinerId, followDinerId);
hash数据类型
保存
redisTemplate.opsForHash().putAll(key, restaurantMap);
list类型数据
获取list的前10条数据
// 从 Redis 取十条最新评论
List<LinkedHashMap> reviews = redisTemplate.opsForList().range(key, 0, NINE);
写入数据
最新的数据在前面。
redisTemplate.opsForList().leftPush(key, reviews);
管道技术
管道为什么快?平常的是 一个插入请求ok之后,再接着插入下一个,管道是等插入完了之后再统一获取响应。
批量插入hash类型
一个是逐行插入,一个是批量插入。
2w条数据,逐行插入大概是2分钟。批量插入30秒左右
package com.imooc.restaurants.service;
import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Maps;
import com.imooc.commons.constant.RedisKeyConstant;
import com.imooc.commons.model.pojo.Restaurant;
import com.imooc.restaurants.RestaurantApplicationTest;
import com.imooc.restaurants.mapper.RestaurantMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Slf4j
public class RestaurantTest extends RestaurantApplicationTest {
@Resource
private RedisTemplate redisTemplate;
@Resource
private RestaurantMapper restaurantMapper;
// 逐行插入
@Test
void testSyncForHash() {
List<Restaurant> restaurants = restaurantMapper.findAll();
long start = System.currentTimeMillis();
restaurants.forEach(restaurant -> {
Map<String, Object> restaurantMap = BeanUtil.beanToMap(restaurant);
String key = RedisKeyConstant.restaurants.getKey() + restaurant.getId();
redisTemplate.opsForHash().putAll(key, restaurantMap);
});
long end = System.currentTimeMillis();
log.info("执行时间:{}", end - start); // 执行时间:118957
}
// Pipeline 管道插入
@Test
void testSyncForHashPipeline() {
List<Restaurant> restaurants = restaurantMapper.findAll();
long start = System.currentTimeMillis();
List<Long> list = redisTemplate.executePipelined((RedisCallback<Long>) connection -> {
for (Restaurant restaurant : restaurants) {
try {
String key = RedisKeyConstant.restaurants.getKey() + restaurant.getId();
Map<String, Object> restaurantMap = BeanUtil.beanToMap(restaurant);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
Map<byte[], byte[]> restaurantStringMap = Maps.newHashMap();
restaurantMap.forEach((k, v) -> {
restaurantStringMap.put(stringRedisSerializer.serialize(k), jackson2JsonRedisSerializer.serialize(v));
});
connection.hMSet(stringRedisSerializer.serialize(key), restaurantStringMap);
} catch (Exception e) {
log.info(restaurant.toString());
continue;
}
}
return null;
});
long end = System.currentTimeMillis();
log.info("执行时间:{}", end - start); // 执行时间:35606
}
}