微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring使用RedisTemplate操作Redis数据库

这篇文章主要介绍了spring使用Redistemplate操作Redis数据库,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一.什么是Redis

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

Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。

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

二.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进行了实现。

注意 Redistemplate是一个key和value都是泛型的模板类,一般情况下key为String类型,如:Redistemplate。

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

spring-data-redis针对jedis提供了如下功能

           1.连接池自动管理,提供了一个高度封装的“Redistemplate”类

           2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

           ValueOperations:简单K-V操作

           Setoperations:set类型数据操作

           ZSetoperations:zset类型数据操作

           HashOperations:针对map类型的数据操作

           ListOperations:针对list类型的数据操作

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

redistemplate.opsForValue();//操作字符串 redistemplate.opsForHash();//操作hash redistemplate.opsForList();//操作list redistemplate.opsForSet();//操作set redistemplate.opsForZSet();//操作有序set

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

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.值类型操作:

@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类型,无序,即存取顺序不一定相同

@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类型分为两种,一种是左压栈,一种是右压栈

右压栈:

/** * 右压栈:后添加的对象排在后边,相当于队列,相当于先进先出 */ @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); }

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

左压栈:

/** * 左压栈:后添加的对象排在前边,相当于栈,先进后出 */ @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); }

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

根据索引查询元素

/** * 查询集合某个元素 */ @Test public void testSearchByIndex(){ String s = (String) redistemplate.boundListOps("namelist1").index(1); System.out.println(s); }

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

/** * 移除集合某个元素,其中remove中第一个参数是移除的个数 */ @Test public void testRemoveByIndex(){ redistemplate.boundListOps("namelist1").remove(1, "关羽"); }

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

4.Hash类型操作

@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查看的时候,你会看到你的数据不是以可读的形式展现的,而是以字节数组显示,类似下面

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐