脚本宝典收集整理的这篇文章主要介绍了SpringBoot整合Redis详细笔记,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<dePEndency>
<groupId>org.sPRingframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 127.0.0.1 #主机地址
port: 6379 #端口号
password: 123456 #密码(没有则不写)
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0
package com.hq.redis.config;
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.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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
/*
* @description redis序列化方式
* @author xianping
* @date 2020/9/25
* @param redisConnectionFactory
* @return RedisTemplate
**/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/*
* @description Redis缓存的序列化方式使用redisTemplate.getValueSerializer(),不在使用JDK默认的序列化方式
* @author xianping
* @date 2020/9/25
* @param redisTemplate
* @return RedisCacheManager
**/
@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.FromSerializer(redisTemplate.getValueSerializer()));
return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
}
}
import org.springframework.data.redis.core.RedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
使用方式:redisTemplate.方法名();
方法如下
public Boolean hasKey(K key);
key:key名
public Boolean expire(K key, long timeout, TimeUnit unit);
key:key名
timeout:时间
unit:时间单位
public Long getExpire(K key);
key:key名
返回值:还有多少秒过期,如果为-2,则key不存在,如果为-1,则key永久生效。
public Long getExpire(K key, final TimeUnit timeUnit);
key:key名
timeUnit:时间单位
返回值:还有多久过期,如果为-2,则key不存在,如果为-1,则key永久生效。
//参数为String
Boolean delete(K key);
返回值:是否删除成功
//参数为集合
Long delete(Collection<K> keys);
返回值:删除成功的数量
public Boolean expireAt(K key, Date date)
key:key名
date:具体时间
public Boolean move(K key, int dbIndex);
key:key名
dbIndex:库序号,从0开始
public Boolean persist(K key);
public K randomKey();
返回值:key名
public Set<K> keys(K pattern);
pattern:正则表达式
返回值:满足条件的Set集合
public void rename(K oldKey, K newKey);
oldKey:要修改的key名
newKey:新的key名
public Boolean renameifAbsent(K oldKey, K newKey);
oldKey:要修改的key名
newKey:新的key名
public DataType type(K key);
返回值:key的类型
使用方式:redisTemplate.opsForValue().方法名();
void set(K VAR1, V var2);
var1:key名
var2:值
V get(Object var1);
void multiSet(Map<? extends K, ? extends V> var1);
参数说明:map的每一对键值对将设置为redis的键值
List<V> multiGet(Collection<K> var1);
var1:key集合
返回值:值集合
V getAndSet(K var1, V var2);
var1:key名
var2:新值
返回值:旧值
V getAndDelete(K key);
Boolean setIfAbsent(K var1, V var2);
var1:key名
var2:值
Boolean setIfPresent(K var1, V var2);
var1:key名
var2:值s
Boolean multiSetIfAbsent(Map<? extends K, ? extends V> var1);
参数说明:map的每一对键值对将设置为redis的键值
Long increment(K var1, long var2);
var1:key名
var2:需要自增或自减的值
返回值:自增或自减后的值
Double increment(K var1, double var2);
var1:key名
var2:需要自增或自减的值
返回值:自增或自减后的值
Integer append(K var1, String var2);
var1:key名
var2:需要追加的内容
返回值:追加值后新值的长度
String get(K key, long start, long end);
key:key名
start:开始索引
end:结束索引
void set(K key, V value, long offset);
key:key名
value:值
offset:索引
Long size(K key);
使用方式:redisTemplate.opsForHash().方法名();
HV get(H var1, Object var2);
var1:key名
var2:字段名
返回值:值
Map<HK, HV> entries(H var1);
var1:key名
返回值:键值对
List<HV> multiGet(H var1, Collection<HK> var2);
var1:key名
var2:字段名集合
返回值:对应的值集合
void put(H var1, HK var2, HV var3);
var1:key名
var2:字段名
var3:值
void putAll(H var1, Map<? extends HK, ? extends HV> var2);
var1:key名
var2:多个键值对
Boolean putIfAbsent(H var1, HK var2, HV var3);
var1:key名
var2:字段名
var3:值
Long delete(H var1, Object... var2);
var1:key名
var2:多个字段名
返回值:删除的字段个数
Boolean hasKey(H var1, Object var2);
var1:key名
var2:字段名
Long increment(H var1, HK var2, long var3);
var1:key名
var2:字段名
var3:自增或自减值
返回值:自增或自减后的值
Double increment(H var1, HK var2, double var3);
var1:key名
var2:字段名
var3:自增或自减值
返回值:自增或自减后的值
Set<HK> keys(H var1);
var1:key名
返回值:字段名集合
Long size(H var1);
var1:key名
返回值:字段数量
List<HV> values(H var1);
var1:key名
返回值:值集合
使用方式:redisTemplate.opsForList().方法名();
Long rightPush(K key, V value);
key:key名
value:值
返回值:集合的总数量
//参数为可变参数
Long rightPushAll(K key, V... values);
//参数为集合
Long rightPushAll(K key, Collection<V> values)
返回值:均为list的总数量
Long leftPush(K key, V pivot, V value);
key:key名
value:值
返回值:集合的总数量
//参数为可变几何
Long rightPushAll(K key, V... values);
//参数为集合
Long rightPushAll(K key, Collection<V> values);
返回值:均为list的总数量
V index(K key, long index);
key:key名
index:索引,当index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
Long size(K key);
返回值:list的长度
List<V> range(K key, long start, long end);
key:key名
start:起始索引
end:结束索引 0到-1代表所有值
void set(K key, long index, V value);
key:key名
index:索引
value:值
Long remove(K key, long count, Object value);
key:key名
count:需要移除的数量 count > 0 从表头删除,count < 0 从表尾删除,count=0全部删除
value:需要移除的值
返回值:成功移除的数量
void trim(K key, long start, long end);
key:key名
start:开始索引
end:结束索引
V leftPop(K key);
List<V> leftPop(K key, long count);
V leftPop(K var1, long var2, TimeUnit var4);
var1:key名
var2:数值
var4:时间单位
V rightPop(K key);
List<V> rightPop(K key, long count);
V rightPop(K var1, long var2, TimeUnit var4);
var1:key名
var2:数值
var4:时间单位
V rightPopAndLeftPush(K var1, K var2);
var1:需要移除的key
var2:需要添加的key
返回值:被移除的元素
V rightPopAndLeftPush(K var1, K var2, long var3, TimeUnit var5);
var1:需要移除的key
var2:需要添加的key
var3:数值
var5:时间单位
返回值:被移除的元素
使用方式:redisTemplate.opsForSet().方法名();
Long add(K key, V... values);
返回值:添加成功的元素总数
Long remove(K key, Object... values);
返回值:移除成功的元素总数
V pop(K key);
List<V> pop(K key, long count);
key:key名
count:数量
Boolean move(K key, V value, K destKey);
key:需要从哪个key移动值
value:需要移动的值
destKey:需要移动到哪个key
Long size(K key);
Boolean isMember(K key, Object o);
Set<V> intersect(K key, K otherKey);
Set<V> intersect(K key, Collection<K> otherKeys);
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
Long intersectAndStore(K var1, Collection<K> var2, K var3);
Set<V> union(K key, K otherKey);
Set<V> union(K key, Collection<K> otherKey);
Long unionAndStore(K key, K otherKey, K destKey);
Long unionAndStore(K key, Collection<K> var2, K destKey);
Set<V> difference(K key, K otherKey);
返回值:key-otherKey的差集
Set<V> difference(K key, Collection<K> otherKeys);
Long differenceAndStore(K key, K otherKey, K destKey);
Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);
返回值:destKey集合的长度
Set<V> members(K key);
V randomMember(K key);
List<V> randomMembers(K key, long count);
使用方式:redisTemplate.opsForZSet().方法名();
Boolean add(K key, V value, double score);
key:key名
value:值
score:排序值
返回值:是否添加成功
Long add(K key, Set<ZSetoperations.TypedTuple<V>> tuples);
Long remove(K key, Object... values);
Double incrementScore(K key, V value, double delta);
key:key名
value:值
delta:需要增加的score值
返回值:增加后的值
Long rank(K key, Object o);
Long reverseRank(K key, Object o);
Set<V> range(K key, long start, long end);
key:key名
start:开始索引
end:结束索引 -1 表示从开始位置到后面的所有元素
返回值:元素集合
Set<ZSetOperations.TypedTuple<V>> rangeWithScores(K key, long start, long end);
key:key名
start:开始索引
end:结束索引 -1 表示从开始位置到后面的所有元素
返回值:元素集合
Set<V> rangeByScore(K key, double min, double max);
key:key名
min:最小值
max:最大值
返回值:在最小值与最大值之间的元素集合
Set<ZSetOperations.TypedTuple<V>> rangeByScoreWithScores(K key, double min, double max);
key:key名
min:最小值
max:最大值
返回值:在最小值与最大值之间的元素集合
Set<V> reverseRange(K key, long start, long end);
key:key名
start:开始索引
end:结束索引
Set<ZSetOperations.TypedTuple<V>> reverseRangeWithScores(K key, long start, long end);
key:key名
start:开始索引
end:结束索引
Set<V> reverseRangeByScore(K key, double min, double max);
key:key名
min:最小值
max:最大值
返回值:分数在 min 与 max 之间的元素的集合, 按分数倒序
Set<ZSetOperations.TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max);
key:key名
min:最小值
max:最大值
返回值:分数在 min 与 max 之间的元素的集合, 按分数倒序
Long count(K key, double min, double max);
key:key名
min:最小值
max:最大值
返回值:分数在 min 与 max 之间的元素数量
Long size(K key);
Double score(K key, Object o);
Long removeRange(K key, long start, long end);
key:key名
start:开始索引
end:结束索引
返回值:成功移除的元素个数
Long removeRangeByScore(K key, double min, double max);
key:key名
start:开始索引
end:结束索引
返回值:成功移除的元素个数
Long intersectAndStore(K key, K otherKey, K destKey);
key:key1
otherKey:key2
destKey:用于保存结果的集合
返回值:新集合的长度
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
key:key1
otherKeys:其他集合
destKey:用于保存结果的集合
返回值:新集合的长度
Long unionAndStore(K key, K otherKey, K destKey);
key:key1
otherKey:key2
destKey:用于保存结果的集合
返回值:新集合的长度
Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
key:key1
otherKeys:其他集合
destKey:用于保存结果的集合
返回值:新集合的长度
Long differenceAndStore(K key, K otherKey, K destKey);
key:key1
otherKey:key2
destKey:用于保存结果的集合
返回值:新集合的长度
Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);
key:key1
otherKeys:其他集合
destKey:用于保存结果的集合
返回值:新集合的长度
使用方式:redisTemplate.opsForGeo().方法名();
Long add(K key, Point point, M member);
key:key名
point:位置(new Point(double x, double y)) x:经度,y:纬度
member:值
Distance distance(K key, M member1, M member2);
key:key名
member1:成员1
member2:成员2
返回值:距离,通过.getValue()获取
Distance distance(K key, M member1, M member2, Metric metric);
key:key名
member1:成员1
member2:成员2
metric:单位 可选值:
RedisGeoCommands.DistanceUnit.KILOMETERS:千米
RedisGeoCommands.DistanceUnit.METERS:米
RedisGeoCommands.DistanceUnit.MILES:英里
RedisGeoCommands.DistanceUnit.FEET:英尺
返回值:距离,通过.getValue()获取
List<Point> position(K key, M... members);
GeoResults<GeoLocation<m>> radius(K key, Circle within);
key:key名
within: //经纬度信息
Point center = new Point(longitude, latitude);
//距离不超过140KM
Distance radius = new Distance(140, Metrics.KILOMETERS);
Circle within = new Circle(center, radius);
返回值:符合条件的元素
GeoResults<GeoLocation<M>> radius(K key, Circle within, GeoRadiusCommandargs args);
key:key名
within: //经纬度信息
Point center = new Point(longitude, latitude);
//距离不超过140KM
Distance radius = new Distance(140, Metrics.KILOMETERS);
Circle within = new Circle(center, radius);
args:参考RedisGeoCommands.GeoRadiusCommandArgs args =
RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending();
sortDescending()是降序
返回值:符合条件的元素
使用方式:opsForHyperLogLog().方法名();
Long add(K key, V... values);
返回值:至少添加成功1个就返回1,否则为0
Long size(K... keys);
void delete(K key);
Long union(K destination,K... SourceKeys);
返回值:数量
使用方式:redisTemplate.opsForValue().setBit(key, offset, value);
方法:Boolean setBit(K key, long offset, boolean value);
key:key名
offset:偏移量
value:true为增加,false为抹去标记
使用方式:redisTemplate.opsForValue().getBit(key, offest);
方法:Boolean getBit(K key, long offset);
key:key名
offset:偏移量
返回值:是否已标记
redisTemplate.execute(new RedisCallback<Long>() {
@override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.bitCount("key".getBytes());
}
})
以上是脚本宝典为你收集整理的SpringBoot整合Redis详细笔记全部内容,希望文章能够帮你解决SpringBoot整合Redis详细笔记所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。