RedisTemplate的API方法解析

StringRedisTemplate
由于存储在Redis中的键和值非常普遍java.lang.String,因此Redis模块提供了RedisConnection和的
两个扩展RedisTemplate,分别是StringRedisConnection(及其DefaultStringRedisConnection实现),
并且StringRedisTemplate是用于密集型String操作的便捷的一站式解决方案。除了绑定到String键之
外,模板和连接还使用StringRedisSerializer下方,这意味着存储的键和值是人类可读的(假设Redis
和您的代码使用相同的编码)。
StringRedisTemplate继承了RedisTemplate类,只是所有的KV序列化都设置为
StringRedisSerializer.UTF_8。
publicclassStringRedisTemplateextendsRedisTemplate<String,String>{
publicStringRedisTemplate(){
this.setKeySerializer(RedisSerializer.string());
this.setValueSerializer(RedisSerializer.string());
this.setHashKeySerializer(RedisSerializer.string());
this.setHashValueSerializer(RedisSerializer.string());
}
publicStringRedisTemplate(RedisConnectionFactoryconnectionFactory){
this();
this.setConnectionFactory(connectionFactory);
this.afterPropertiesSet();
}
protectedRedisConnectionpreProcessConnection(RedisConnectionconnection,boolean
existingConnection){
returnnewDefaultStringRedisConnection(connection);
}
}
测试案例:
@Test
voidstringRedisTemplateTest(){
stringRedisTemplate.boundValueOps("kkk").set("vvv");
Stringvalue=stringRedisTemplate.boundValueOps("kkk").get();
stringRedisTemplate.boundHashOps("hashkkk").putIfAbsent("kkk","vvv");
System.out.println(value);
}
查看redis中的数据,发现都为String,很方便阅读:
RedisTemplate
Operations
RedisTemplate对Redis的每种数据类型都提供了响应的Operation对象,对数据进行操作。
Bound开头需要绑定Key,其对应的操作API都是一样的
@Test
voidoperationsTest(){
//Key-Value
BoundValueOperations<String,Object>boundValueOperations=
redisTemplate.boundValueOps("key");
ValueOperations<String,Object>valueOperations=redisTemplate.opsForValue();
//List
BoundListOperations<String,Object>boundListOperations=
redisTemplate.boundListOps("list:key");
ListOperations<String,Object>listOperations=redisTemplate.opsForList();
//Set
BoundSetOperations<String,Object>boundSetOperations=
redisTemplate.boundSetOps("set:key");
SetOperations<String,Object>setOperations=redisTemplate.opsForSet();
//Zset
BoundZSetOperations<String,Object>boundZSetOps=
redisTemplate.boundZSetOps("zset:key");
ZSetOperations<String,Object>zSetOperations=redisTemplate.opsForZSet();
//Hash
BoundHashOperations<String,Object,Object>boundHashOperations=
redisTemplate.boundHashOps("hash:key");
HashOperations<String,Object,Object>hashOperations=redisTemplate.opsForHash();
//BitMaps(无)
//HyperLogLog
HyperLogLogOperations<String,Object>hyperLogLogOperations=
redisTemplate.opsForHyperLogLog();
//Stream
BoundStreamOperations<String,Object,Object>boundStreamOperations=
redisTemplate.boundStreamOps("stream:key");
StreamOperations<String,Object,Object>streamOperations=redisTemplate.opsForStream();
//Geo
BoundGeoOperations<String,Object>boundGeoOperations=
redisTemplate.boundGeoOps("geo:key");
GeoOperations<String,Object>geoOperations=redisTemplate.opsForGeo();
}
BoundValueOperations
//Key-Value
BoundValueOperations<String,Object>boundValueOperations=
redisTemplate.boundValueOps("key");
ValueOperations<String,Object>valueOperations=redisTemplate.opsForValue();
//设定key对应的vlaue值
boundValueOperations.set("set");
//将value值从第offset位开始替换
boundValueOperations.set("oop",1);
//设置value并设置过期时间1小时
boundValueOperations.set("ooo",Duration.ofHours(1));
//设置value并设置过期时间3600秒
boundValueOperations.set("opo",3600,TimeUnit.SECONDS);
//返回key对应的value
Objectvalue=boundValueOperations.get();
System.out.println("value:"+value);
//从0开始,到1结束,截取value的值
StringvalueOfSub=boundValueOperations.get(0,1);
System.out.println("valueOfSub:"+valueOfSub);
//替换value的值,并且返回value的旧值
ObjectoldValue=boundValueOperations.getAndSet("new");
System.out.println("oldValue"+oldValue);
//判断key是否有对应的value,如果有,则返回false,如果没有,添加,返回true
BooleansetIfAbsent=boundValueOperations.setIfAbsent("setIfAbsent");
System.out.println("setIfAbsent"+setIfAbsent);
//判断当前的键的值是否为v,是的话不作操作,不实的话进行替换。如果没有这个键也不
会做任何操作。
BooleansetIfPresent=boundValueOperations.setIfPresent("setIfPresent");
System.err.println("setIfPresent"+setIfPresent);
//在value值后面进行添加,并且返回新value的长度
Integerappend=boundValueOperations.append("append");
System.out.println("append"+append);
//返回value的长度
Longsize=boundValueOperations.size();
System.out.println("size"+size);
//返回key的剩余缓存时间,单位:秒
Longexpire=boundValueOperations.getExpire();
System.out.println("expire"+expire);
//返回key的名称
Stringkey=boundValueOperations.getKey();
System.out.println("key"+key);
//返回数据的类型=》string、list。。。
DataTypetype=boundValueOperations.getType();
System.out.println("type"+type.name());
//以下会报错,需要配置:redisTemplate.setValueSerializer(new
GenericJackson2JsonRedisSerializer());
//INCR:value加1
boundValueOperations.set(1);
DataTypeboundValueOperationsType=boundValueOperations.getType();
System.out.println("type"+boundValueOperationsType.name());
Longincrement=boundValueOperations.increment();
System.out.println("increment"+increment);
//INCR:value加100
Longincrement1=boundValueOperations.increment(100);
System.out.println("increment1"+increment1);
//DECR:value-1
Longdecrement=boundValueOperations.decrement();
System.out.println("decrement"+decrement);
//DECR:value-1000
Longdecrement1=boundValueOperations.decrement(1000);
System.out.println("decrement1"+decrement1);
//INCR:value加3.14
Doubleincrement2=boundValueOperations.increment(3.14);
System.out.println("increment2"+increment2);
BoundListOperations
//List
BoundListOperations<String,Object>boundListOperations=
redisTemplate.boundListOps("list:key");
ListOperations<String,Object>listOperations=redisTemplate.opsForList();
//在List左边添加一个或多个值,返回List的长度
Longaaa=boundListOperations.leftPush("aaa");
System.out.println(aaa);
LongaLong1=boundListOperations.leftPushAll("ddd","eee","fff");
System.out.println(aLong1);
//从左边弹出值
ObjectleftPop=boundListOperations.leftPop();
System.out.println(leftPop);
ObjectleftPop1=boundListOperations.leftPop(Duration.ofMinutes(5L));
System.out.println(leftPop1);
ObjectleftPop2=boundListOperations.leftPop(1000,TimeUnit.SECONDS);
System.out.println(leftPop2);
//返回List长度
Longsize=boundListOperations.size();
System.out.println(size);
//返回索引为2的值
Objectindex=boundListOperations.index(2);
System.out.println(index);
//获取绑定键中给定的区间值,从下标0开始,end可以为-1表示最后一位
List<Object>range=boundListOperations.range(0,2);
BoundSetOperations
//Set
BoundSetOperations<String,Object>boundSetOperations=
redisTemplate.boundSetOps("set:key");
SetOperations<String,Object>setOperations=redisTemplate.opsForSet();
//添加值,可以是集合、数组、多参数
Longaaa1=boundSetOperations.add("aaa","bbb");
//获取所有值
Set<Object>members=boundSetOperations.members();
//返回第一个集合与其他集合之间的差异,也可以认为说第一个集合中独有的元素
Set<Object>diff=boundSetOperations.diff("set:diff");
//找出两个集合的相同部分
Set<Object>intersect=boundSetOperations.intersect("set:diff");
//找出兩個集合的并集
Set<Object>union=boundSetOperations.union("union");
//返回1位置的值
Objectindex1=boundListOperations.index(1);
//移除某个元素
Longaaa2=boundSetOperations.remove("aaa");
ZSetOperations
publicinterfaceZSetOperations<K,V>{
@Nullable
Booleanadd(Kvar1,Vvar2,doublevar3);
@Nullable
Longadd(Kvar1,Set<ZSetOperations.TypedTuple<V>>var2);
@Nullable
Longremove(Kvar1,Object...var2);
@Nullable
DoubleincrementScore(Kvar1,Vvar2,doublevar3);
@Nullable
Longrank(Kvar1,Objectvar2);
@Nullable
LongreverseRank(Kvar1,Objectvar2);
@Nullable
Set<V>range(Kvar1,longvar2,longvar4);
@Nullable
Set<ZSetOperations.TypedTuple<V>>rangeWithScores(Kvar1,longvar2,longvar4);
@Nullable
Set<V>rangeByScore(Kvar1,doublevar2,doublevar4);
@Nullable
Set<ZSetOperations.TypedTuple<V>>rangeByScoreWithScores(Kvar1,doublevar2,doublevar4);
@Nullable
Set<V>rangeByScore(Kvar1,doublevar2,doublevar4,longvar6,longvar8);
@Nullable
Set<ZSetOperations.TypedTuple<V>>rangeByScoreWithScores(Kvar1,doublevar2,doublevar4,
longvar6,longvar8);
@Nullable
Set<V>reverseRange(Kvar1,longvar2,longvar4);
@Nullable
Set<ZSetOperations.TypedTuple<V>>reverseRangeWithScores(Kvar1,longvar2,longvar4);
@Nullable
Set<V>reverseRangeByScore(Kvar1,doublevar2,doublevar4);
@Nullable
Set<ZSetOperations.TypedTuple<V>>reverseRangeByScoreWithScores(Kvar1,doublevar2,double
var4);
@Nullable
Set<V>reverseRangeByScore(Kvar1,doublevar2,doublevar4,longvar6,longvar8);
@Nullable
Set<ZSetOperations.TypedTuple<V>>reverseRangeByScoreWithScores(Kvar1,doublevar2,double
var4,longvar6,longvar8);
@Nullable
Longcount(Kvar1,doublevar2,doublevar4);
@Nullable
LonglexCount(Kvar1,Rangevar2);
@Nullable
Longsize(Kvar1);
@Nullable
LongzCard(Kvar1);
@Nullable
Doublescore(Kvar1,Objectvar2);
@Nullable
LongremoveRange(Kvar1,longvar2,longvar4);
@Nullable
LongremoveRangeByScore(Kvar1,doublevar2,doublevar4);
@Nullable
LongunionAndStore(Kvar1,Kvar2,Kvar3);
@Nullable
LongunionAndStore(Kvar1,Collection<K>var2,Kvar3);
@Nullable
defaultLongunionAndStore(Kkey,Collection<K>otherKeys,KdestKey,Aggregateaggregate){
returnthis.unionAndStore(key,otherKeys,destKey,aggregate,Weights.fromSetCount(1+
otherKeys.size()));
}
@Nullable
LongunionAndStore(Kvar1,Collection<K>var2,Kvar3,Aggregatevar4,Weightsvar5);
@Nullable
LongintersectAndStore(Kvar1,Kvar2,Kvar3);
@Nullable
LongintersectAndStore(Kvar1,Collection<K>var2,Kvar3);
@Nullable
defaultLongintersectAndStore(Kkey,Collection<K>otherKeys,KdestKey,Aggregateaggregate){
returnthis.intersectAndStore(key,otherKeys,destKey,aggregate,Weights.fromSetCount(1+
otherKeys.size()));
}
@Nullable
LongintersectAndStore(Kvar1,Collection<K>var2,Kvar3,Aggregatevar4,Weightsvar5);
Cursor<ZSetOperations.TypedTuple<V>>scan(Kvar1,ScanOptionsvar2);
@Nullable
defaultSet<V>rangeByLex(Kkey,Rangerange){
returnthis.rangeByLex(key,range,Limit.unlimited());
}
@Nullable
Set<V>rangeByLex(Kvar1,Rangevar2,Limitvar3);
@Nullable
defaultSet<V>reverseRangeByLex(Kkey,Rangerange){
returnthis.reverseRangeByLex(key,range,Limit.unlimited());
}
@Nullable
Set<V>reverseRangeByLex(Kvar1,Rangevar2,Limitvar3);
RedisOperations<K,V>getOperations();
publicinterfaceTypedTuple<V>extendsComparable<ZSetOperations.TypedTuple<V>>{
@Nullable
VgetValue();
@Nullable
DoublegetScore();
}
}
BoundHashOperations
publicinterfaceBoundHashOperations<H,HK,HV>extendsBoundKeyOperations<H>{
@Nullable
Longdelete(Object...var1);
@Nullable
BooleanhasKey(Objectvar1);
@Nullable
HVget(Objectvar1);
@Nullable
List<HV>multiGet(Collection<HK>var1);
@Nullable
Longincrement(HKvar1,longvar2);
@Nullable
Doubleincrement(HKvar1,doublevar2);
@Nullable
Set<HK>keys();
@Nullable
LonglengthOfValue(HKvar1);
@Nullable
Longsize();
voidputAll(Map<?extendsHK,?extendsHV>var1);
voidput(HKvar1,HVvar2);
@Nullable
BooleanputIfAbsent(HKvar1,HVvar2);
@Nullable
List<HV>values();
@Nullable
Map<HK,HV>entries();
Cursor<Entry<HK,HV>>scan(ScanOptionsvar1);
RedisOperations<H,?>getOperations();
}
HyperLogLogOperations
publicinterfaceHyperLogLogOperations<K,V>{
Longadd(Kvar1,V...var2);
Longsize(K...var1);
Longunion(Kvar1,K...var2);
voiddelete(Kvar1);
BoundStreamOperations
publicinterfaceBoundStreamOperations<K,HK,HV>{
@Nullable
Longacknowledge(Stringvar1,String...var2);
@Nullable
RecordIdadd(Map<HK,HV>var1);
@Nullable
Longdelete(String...var1);
@Nullable
StringcreateGroup(ReadOffsetvar1,Stringvar2);
@Nullable
BooleandeleteConsumer(Consumervar1);
@Nullable
BooleandestroyGroup(Stringvar1);
@Nullable
Longsize();
@Nullable
defaultList<MapRecord<K,HK,HV>>range(Range<String>range){
returnthis.range(range,Limit.unlimited());
}
@Nullable
List<MapRecord<K,HK,HV>>range(Range<String>var1,Limitvar2);
@Nullable
defaultList<MapRecord<K,HK,HV>>read(ReadOffsetreadOffset){
returnthis.read(StreamReadOptions.empty(),readOffset);
}
@Nullable
List<MapRecord<K,HK,HV>>read(StreamReadOptionsvar1,ReadOffsetvar2);
@Nullable
defaultList<MapRecord<K,HK,HV>>read(Consumerconsumer,ReadOffsetreadOffset){
returnthis.read(consumer,StreamReadOptions.empty(),readOffset);
}
@Nullable
List<MapRecord<K,HK,HV>>read(Consumervar1,StreamReadOptionsvar2,ReadOffsetvar3);
@Nullable
defaultList<MapRecord<K,HK,HV>>reverseRange(Range<String>range){
returnthis.reverseRange(range,Limit.unlimited());
}
@Nullable
List<MapRecord<K,HK,HV>>reverseRange(Range<String>var1,Limitvar2);
@Nullable
Longtrim(longvar1);
@Nullable
Longtrim(longvar1,booleanvar3);
}
BoundGeoOperations
publicinterfaceBoundGeoOperations<K,M>extendsBoundKeyOperations<K>{
@Nullable
Longadd(Pointvar1,Mvar2);
/**@deprecated*/
@Deprecated
@Nullable
defaultLonggeoAdd(Pointpoint,Mmember){
returnthis.add(point,member);
}
@Nullable
Longadd(GeoLocation<M>var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultLonggeoAdd(GeoLocation<M>location){
returnthis.add(location);
}
@Nullable
Longadd(Map<M,Point>var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultLonggeoAdd(Map<M,Point>memberCoordinateMap){
returnthis.add(memberCoordinateMap);
}
@Nullable
Longadd(Iterable<GeoLocation<M>>var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultLonggeoAdd(Iterable<GeoLocation<M>>locations){
returnthis.add(locations);
}
@Nullable
Distancedistance(Mvar1,Mvar2);
/**@deprecated*/
@Deprecated
@Nullable
defaultDistancegeoDist(Mmember1,Mmember2){
returnthis.distance(member1,member2);
}
@Nullable
Distancedistance(Mvar1,Mvar2,Metricvar3);
/**@deprecated*/
@Deprecated
@Nullable
defaultDistancegeoDist(Mmember1,Mmember2,Metricmetric){
returnthis.distance(member1,member2,metric);
}
@Nullable
List<String>hash(M...var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultList<String>geoHash(M...members){
returnthis.hash(members);
}
@Nullable
List<Point>position(M...var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultList<Point>geoPos(M...members){
returnthis.position(members);
}
@Nullable
GeoResults<GeoLocation<M>>radius(Circlevar1);
/**@deprecated*/
@Deprecated
@Nullable
defaultGeoResults<GeoLocation<M>>geoRadius(Circlewithin){
returnthis.radius(within);
}
@Nullable
GeoResults<GeoLocation<M>>radius(Circlevar1,GeoRadiusCommandArgsvar2);
/**@deprecated*/
@Deprecated
@Nullable
defaultGeoResults<GeoLocation<M>>geoRadius(Circlewithin,GeoRadiusCommandArgsargs){
returnthis.radius(within,args);
}
@Nullable
GeoResults<GeoLocation<M>>radius(Kvar1,Mvar2,doublevar3);
/**@deprecated*/
@Deprecated
@Nullable
defaultGeoResults<GeoLocation<M>>geoRadiusByMember(Kkey,Mmember,doubleradius){
returnthis.radius(key,member,radius);
}
@Nullable
GeoResults<GeoLocation<M>>radius(Mvar1,Distancevar2);
/**@deprecated*/
@Deprecated
@Nullable
defaultGeoResults<GeoLocation<M>>geoRadiusByMember(Mmember,Distancedistance){
returnthis.radius(member,distance);
}
@Nullable
GeoResults<GeoLocation<M>>radius(Mvar1,Distancevar2,GeoRadiusCommandArgsvar3);
/**@deprecated*/
@Deprecated
@Nullable
defaultGeoResults<GeoLocation<M>>geoRadiusByMember(Mmember,Distancedistance,
GeoRadiusCommandArgsargs){
returnthis.radius(member,distance,args);
}
@Nullable
Longremove(M...var1);
/**@deprecated*/
@Deprecated
@Nullable
defaultLonggeoRemove(M...members){
returnthis.remove(members);
}
}
总结
其实和Redis命令差不多,基本上提供了很多操作方法,实在太多了,后面的就懒得写的。。。可以对
照Redis命令进行实践。一般实际项目中会自己封装RedisTemplate为工具了,配置一些常用的
set\get\expire等操作就行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值