Redis--Spring集成Redis

mac2022-06-30  27

Redis,算是java中最常用的非关系型数据库之一了,至于和MongoDB的比较这里不做讨论,由于运行在内存中实在是强大,打开就跑不用过多的麻烦配置.

  也有面向java的客户端Jedis可以使用.Spring也对其做了整合,大概打开Redis能找到1W+的帖子,但是笔记终归还是自己的好.Jedis不多说应用比较少,基本都是使用Spring集成: 

Maven坐标:

<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency>

配置文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath*:properties/redis-config.properties"/> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxIdle" value="${redis.maxIdle}"/> <property name="maxWaitMillis" value="${redis.maxWait}"/> <property name="testOnBorrow" value="true"/> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.pass}"/> <property name="poolConfig" ref="poolConfig"/> </bean> <bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory"/> </bean></beans>

说明:

maxIdle :最大空闲数

maxWaitMillis:连接时的最大等待毫秒数

testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

host:redis地址

port:端口号

 

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

 

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

  ValueOperations:简单K-V操作

// public void setValue(){ redisTemplate.boundValueOps("name").set("itcast"); } // public void getValue(){ String str = (String) redisTemplate.boundValueOps("name").get(); System.out.println(str); } //删除 public void deleteValue(){ redisTemplate.delete("name");; }

SetOperations:set类型数据操作

// public void setValue(){ redisTemplate.boundSetOps("name").add("111"); redisTemplate.boundSetOps("name").add("222"); redisTemplate.boundSetOps("name").add("333"); } // public void getValue(){ Set members = redisTemplate.boundSetOps("name").members(); System.out.println(members); } //删单个值 public void deleteValue(){ redisTemplate.boundSetOps("nameset").remove("111"); } //删集合 public void deleteAllValue(){ redisTemplate.delete("name"); }

ZSetOperations:zset类型数据操作

/** * 存 */ @Test public void setSet(){ Long add = redisTemplate.boundSetOps("gg").add("go"); System.out.println(add); } /** * 取 */ @Test public void getSet(){ // 取所有成员 Set set = redisTemplate.boundSetOps("gg").members(); System.out.println(set); }

 

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

Hash方式是开发中最常用的方式

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml") public class TestHash { @Autowired private RedisTemplate redisTemplate; /** * 存 */ @Test public void setHash(){ redisTemplate.boundHashOps("eva").put("初号机","碇真嗣"); redisTemplate.boundHashOps("eva").put("零号机","绫波丽"); redisTemplate.boundHashOps("eva").put("二号机","明日香"); } /** * 取key */ @Test public void getKey(){ Set eva = redisTemplate.boundHashOps("eva").keys(); System.out.println(eva); } /** * 取值 */ @Test public void getValue(){ String s = (String) redisTemplate.boundHashOps("eva").get("初号机"); System.out.println(s); } /** * 取所有值 */ @Test public void getAllValue(){ List<String> list = redisTemplate.boundHashOps("eva").values(); System.out.println(list); } /** * 删 */ @Test public void delete(){ redisTemplate.boundHashOps("eva").delete("二号机"); } }

 

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

右压栈方式是将后添加的元素添加在集合的尾部,而左压栈方式是将新添加的元素插入到集合的首索引位置.

import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")public class Testlist { @Autowired private RedisTemplate redisTemplate; /** * 压入右栈 */ @Test public void testSetRightValue(){ redisTemplate.boundListOps("namelist1").rightPush("仓木麻衣"); redisTemplate.boundListOps("namelist1").rightPush("新垣结衣"); redisTemplate.boundListOps("namelist1").rightPush("钉宫理惠"); } /** * 显示右栈集合 */ @Test public void showRight() { List range = redisTemplate.boundListOps("namelist1").range(0, 10); System.out.println(range); } /** * 左压栈 */ @Test public void testSetLeftValue(){ redisTemplate.boundListOps("namelist2").leftPush("花泽香菜"); redisTemplate.boundListOps("namelist2").leftPush("小仓唯"); redisTemplate.boundListOps("namelist2").leftPush("民民"); } /** * 显示左压栈 */ @Test public void showLeft() { String index1 = (String)redisTemplate.boundListOps("namelist2").index(1); System.out.println(index1); String index2 = (String)redisTemplate.boundListOps("namelist2").index(2); System.out.println(index2); String index3 = (String)redisTemplate.boundListOps("namelist2").index(3); System.out.println(index3); } /** * 移除元素 */ @Test public void testRemoveByIndex(){ redisTemplate.boundListOps("namelist2").remove(2,"花泽香菜"); } /** * 显示namelist2集合 */ @Test public void showLeftList(){ List namelist2 = redisTemplate.boundListOps("namelist2").range(0, 10); System.out.println(namelist2); }}

 

 

 

  

转载于:https://www.cnblogs.com/YuaoSun/p/9248258.html

相关资源:spring-redis-test---spring redis集成
最新回复(0)