spring使用RedisTemplate操作Redis数据库

2025-05-29 0 37

一.什么是redis

redis是一个非关系型数据库,具有很高的存取性能,一般用作缓存数据库,减少正常存储数据库的压力。

redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为string(字符串)、list(列表)、set(集合)、hash(散列)和 zset(有序集合)。

下面来对这5种数据结构类型作简单的介绍:

spring使用RedisTemplate操作Redis数据库

二.redistemplate及其相关方法

1.redistemplate

spring封装了redistemplate对象来进行对redis的各种操作,它支持所有的redis原生的api。redistemplate位于spring-data-redis包下。redistemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。redistemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。redistemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。redistemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

spring使用RedisTemplate操作Redis数据库

注意redistemplate是一个key和value都是泛型的模板类,一般情况下key为string类型,如:redistemplate<string,object>。

此外,如果没特殊情况,切勿定义成redistemplate<object, object>,否则根据里氏替换原则,使用的时候会造成类型错误 。

spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,提供了一个高度封装的“redistemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
valueoperations:简单k-v操作
setoperations:set类型数据操作
zsetoperations:zset类型数据操作
hashoperations:针对map类型的数据操作
listoperations:针对list类型的数据操作

2.redistemplate中定义了对5种数据结构操作

?

1

2

3

4

5
redistemplate.opsforvalue();//操作字符串

redistemplate.opsforhash();//操作hash

redistemplate.opsforlist();//操作list

redistemplate.opsforset();//操作set

redistemplate.opsforzset();//操作有序set

其实这里的ops相当于options, 是redistemplate对各种不同的redis数据类型进行操作。其实还有另外的方法:

?

1

2

3

4

5
redistempalate.boundvalueops

redistempalate.boundsetops

redistempalate.boundlistops

redistempalate.boundhashops

redistempalate.boundzsetops

opsforxxx和boundxxxops的区别?

xxx为value的类型,前者获取一个operator,但是没有指定操作的对象(key),可以在一个连接(事务)内操作多个key以及对应的value;后者获取了一个指定操作对象(key)的operator,在一个连接(事务)内只能操作这个key对应的value。

关于计数的api(increment)有一个bug,需要各位使用中注意,通过increment计数以后,通过get方式获取计数值的时候可能会抛出eof异常(和本地的jdk以及redis的编译版本有关),可以考虑使用boundvalueops(key).get(0,-1)获取计数值。

三.redistemplate操作redis数据库的具体例子

1.值类型操作:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
@runwith(springjunit4classrunner.class)

@contextconfiguration(locations="classpath:spring/applicationcontext-redis.xml")

public class testvalue {

@autowired

private redistemplate redistemplate;

@test

public void setvalue(){

//存值,针对值类型,ops相当于options

redistemplate.boundvalueops("name").set("itcast");

}

@test

public void getvalue(){

string str = (string) redistemplate.boundvalueops("name").get();

system.out.println(str);

}

@test

public void deletevalue(){

redistemplate.delete("name");

}

}

2.集合类型操作之set类型,无序,即存取顺序不一定相同

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42
@runwith(springjunit4classrunner.class)

@contextconfiguration(locations="classpath:spring/applicationcontext-redis.xml")

public class testset {

@autowired

private redistemplate redistemplate;

/**

* 存入值

*/

@test

public void setvalue(){

redistemplate.boundsetops("nameset").add("曹操");

redistemplate.boundsetops("nameset").add("刘备");

redistemplate.boundsetops("nameset").add("孙权");

}

/**

* 提取值

*/

@test

public void getvalue(){

set members = redistemplate.boundsetops("nameset").members();

system.out.println(members);

}

/**

* 删除集合中的某一个值

*/

@test

public void deletevalue(){

redistemplate.boundsetops("nameset").remove("孙权");

}

/**

* 删除整个集合

*/

@test

public void deleteallvalue(){

redistemplate.delete("nameset");

}

}

输出结果:[孙权, 刘备, 曹操],此外,set类型的元素也不可重复。当set没有值的时候,会返回一个[]

3.list类型操作

list类型分为两种,一种是左压栈,一种是右压栈

右压栈:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
/**

* 右压栈:后添加的对象排在后边,相当于队列,相当于先进先出

*/

@test

public void testsetvalue1(){

redistemplate.boundlistops("namelist1").rightpush("刘备");

redistemplate.boundlistops("namelist1").rightpush("关羽");

redistemplate.boundlistops("namelist1").rightpush("张飞");

}

/**

* 显示右压栈集合,range 表示查询的索引,从第几个查到第几个,如果想查询所有的数的话只能将第二个数写得大一点。

*/

@test

public void testgetvalue1(){

list list = redistemplate.boundlistops("namelist1").range(0, 10);

system.out.println(list);

}

运行结果:[刘备, 关羽, 张飞],元素可以重复

左压栈:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
/**

* 左压栈:后添加的对象排在前边,相当于栈,先进后出

*/

@test

public void testsetvalue2(){

redistemplate.boundlistops("namelist2").leftpush("刘备");

redistemplate.boundlistops("namelist2").leftpush("关羽");

redistemplate.boundlistops("namelist2").leftpush("张飞");

}

/**

* 显示左压栈集合

*/

@test

public void testgetvalue2(){

list list = redistemplate.boundlistops("namelist2").range(0, 10);

system.out.println(list);

}

运行结果:[张飞, 关羽, 刘备]

根据索引查询元素

?

1

2

3

4

5

6

7

8
/**

* 查询集合某个元素

*/

@test

public void testsearchbyindex(){

string s = (string) redistemplate.boundlistops("namelist1").index(1);

system.out.println(s);

}

运行结果:返回索引为1的元素移除某个元素的值

?

1

2

3

4

5

6

7
/**

* 移除集合某个元素,其中remove中第一个参数是移除的个数

*/

@test

public void testremovebyindex(){

redistemplate.boundlistops("namelist1").remove(1, "关羽");

}

这里表示移除一个“关羽”。

4.hash类型操作

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43
@runwith(springjunit4classrunner.class)

@contextconfiguration(locations = "classpath:spring/applicationcontext-redis.xml")

public class testhash {

@autowired

private redistemplate redistemplate;

// 存值

@test

public void testsetvalue() {

redistemplate.boundhashops("namehash").put("a", "唐僧");

redistemplate.boundhashops("namehash").put("b", "悟空");

redistemplate.boundhashops("namehash").put("c", "八戒");

redistemplate.boundhashops("namehash").put("d", "沙僧");

}

//获取所有的key

@test

public void testgetkeys() {

set s = redistemplate.boundhashops("namehash").keys();

system.out.println(s);

}

// 获取所有的value

@test

public void testgetvalues() {

list values = redistemplate.boundhashops("namehash").values();

system.out.println(values);

}

// 根据key获取值

@test

public void testgetvaluebykey() {

object object = redistemplate.boundhashops("namehash").get("b");

system.out.println(object);

}

//根据key移除值

@test

public void testremovevaluebykey() {

redistemplate.boundhashops("namehash").delete("c");

}

}

四.redistemplate和stringredistemplate的区别

1. 两者的关系是stringredistemplate继承redistemplate。

2. 两者的数据是不共通的;也就是说stringredistemplate只能管理stringredistemplate里面的数据,redistemplate只能管理redistemplate中的数据。

3. sdr默认采用的序列化策略有两种,一种是string的序列化策略,一种是jdk的序列化策略。

stringredistemplate默认采用的是string的序列化策略,保存的key和value都是采用此策略序列化保存的。

redistemplate默认采用的是jdk的序列化策略,保存的key和value都是采用此策略序列化保存的。

redistemplate使用的序列类在在操作数据的时候,比如说存入数据会将数据先序列化成字节数组然后在存入redis数据库,这个时候打开redis查看的时候,你会看到你的数据不是以可读的形式展现的,而是以字节数组显示,类似下面

spring使用RedisTemplate操作Redis数据库

当然从redis获取数据的时候也会默认将数据当做字节数组转化,这样就会导致一个问题,当需要获取的数据不是以字节数组存在redis当中而是正常的可读的字符串的时候,比如说下面这种形式的数据

spring使用RedisTemplate操作Redis数据库

相关连接:https://www.cnblogs.com/easonjim/p/7803067.html

到此这篇关于spring使用redistemplate操作redis数据库的文章就介绍到这了,更多相关spring使用redistemplate操作redis内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/striveb/article/details/83745680

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring使用RedisTemplate操作Redis数据库 https://www.kuaiidc.com/108101.html

相关文章

发表评论
暂无评论