Spring Data Redis快速入门

mac2024-03-24  34

Spring Data Redis快速入门

1、准备工作

1.1 构建Maven工程 SpringDataRedisDemo 引入Spring相关依赖、JUnit依赖、Jedis和SpringDataRedis依赖

<!‐‐缓存‐‐> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring‐data‐redis</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring‐test</artifactId> <version>5.0.5.RELEASE</version> </dependency>

1.2 在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1 redis.port=6379 redis.pass= redis.database=0 redis.maxIdle=300 redis.maxWait=3000

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

1.3 在src/main/resources下创建spring文件夹,创建applicationContext-redis.xml

<context:property‐placeholder location="classpath:redis‐config.properties" /> <!‐‐ redis 相关配置 ‐‐> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host‐name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool‐config‐ref="poolConfig"/> <bean id="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean>

2、Redis的操作

2.1 值类型的操作

package com.cui.test; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-redis.xml") public class ValueTest { @Autowired private RedisTemplate redisTemplate; /** * 存值类型的数据 */ @Test public void setValue(){ redisTemplate.boundValueOps("name").set("王五"); } /** * 取值类型的数据 */ @Test public void getValue(){ String name = (String)redisTemplate.boundValueOps("name").get(); System.out.println(name); } /** * 删除值类型的数据 */ @Test public void testDelete(){ redisTemplate.delete("name");//删除 } }

2.2 Set类型操作

package com.cui.test; 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.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-redis.xml") public class SetTest { @Autowired private RedisTemplate redisTemplate; /** * 存入Set类型的数据 */ @Test public void setValue(){ redisTemplate.boundSetOps("nameSet").add("张三"); redisTemplate.boundSetOps("nameSet").add("李四"); redisTemplate.boundSetOps("nameSet").add("王五"); redisTemplate.boundSetOps("nameSet").add("赵六"); } /** * 取Set类型的数据 */ @Test public void getValue(){ Set nameSet = redisTemplate.boundSetOps("nameSet").members(); System.out.println(nameSet); } /** * 删除Set集合中的某一个值 */ @Test public void testDelete(){ redisTemplate.boundSetOps("nameSet").remove("张三"); } /** * 删除整个集合 */ @Test public void testDeleteAllSet(){ redisTemplate.delete("nameSet"); } }

2.3 List类型操作

2.3.1 左压栈 新加入的数据排在前面
package com.cui.test; 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:applicationContext-redis.xml") public class ListTest { @Autowired private RedisTemplate redisTemplate; /** * 左压栈:后添加的对象排在前边 */ @Test public void setValue1(){ redisTemplate.boundListOps("nameList1").leftPush("张飞"); redisTemplate.boundListOps("nameList1").leftPush("刘备"); redisTemplate.boundListOps("nameList1").leftPush("关羽"); } /** * 显示左压栈集合 */ @Test public void getValue(){ //range(开始索引,查询个数) 如果查询个数为-1,不限制查询个数 List nameList1 = redisTemplate.boundListOps("nameList1").range(0, -1); System.out.println(nameList1); }
2.3.2 右压栈 新加入的数据加到后面
/** * 右压栈 新加入的数据放到后面 */ @Test public void setValue2(){ redisTemplate.boundListOps("nameLit2").rightPush("德玛西亚之力"); redisTemplate.boundListOps("nameLit2").rightPush("无极剑圣"); redisTemplate.boundListOps("nameLit2").rightPush("诺克萨斯之手"); } /** * 取右压栈集合数据 */ @Test public void getValue2(){ List nameList2 = redisTemplate.boundListOps("nameLit2").range(0, -1); System.out.println(nameList2); }
2.3.3 根据索引查询某个元素
/** * 根据索引查询某个元素 */ @Test public void testSearchByIndex(){ String s =(String) redisTemplate.boundListOps("nameList1").index(1); System.out.println(s); }
2.3.4 移除指定个数的值
/** * 添加四个无极剑圣 */ @Test public void setAdd2(){ redisTemplate.boundListOps("nameLit2").rightPush("无极剑圣"); redisTemplate.boundListOps("nameLit2").rightPush("无极剑圣"); redisTemplate.boundListOps("nameLit2").rightPush("无极剑圣"); redisTemplate.boundListOps("nameLit2").rightPush("无极剑圣"); } /** * 移除指定个数的值 */ @Test public void testRemoveByIndex(){ redisTemplate.boundListOps("nameLit2").remove(2, "无极剑圣"); }

2.4 Hash类型操作

package com.cui.test; 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:applicationContext-redis.xml") public class HashTest { @Autowired private RedisTemplate redisTemplate; /** * 存Hash类型的数据 */ @Test public void setValue(){ redisTemplate.boundHashOps("nameHash").put("a", "张无忌"); redisTemplate.boundHashOps("nameHash").put("b", "周芷若"); redisTemplate.boundHashOps("nameHash").put("c", "赵敏"); } /** * 取所有的key值 */ @Test public void getKeys(){ Set keys = redisTemplate.boundHashOps("nameHash").keys(); System.out.println(keys); } /** * 取所有的的value */ @Test public void getValues(){ List values = redisTemplate.boundHashOps("nameHash").values(); System.out.println(values); } /** * 根据key取值 */ @Test public void testGetValueByKey(){ Object o = redisTemplate.boundHashOps("nameHash").get("a"); System.out.println(o); } /** * 根据key移除value */ @Test public void testRemoveVaalueByKey(){ redisTemplate.boundHashOps("nameHash").delete("a"); } /** * 删除Hash类型的数据 */ @Test public void testDelete(){ redisTemplate.delete("nameHash");//删除 } }

2.5 Zset类型操作

zset是set的升级版本,它在set的基础上增加了一格顺序属性,这一属性在添加元素的同时可以指定,每次指定后,zset会自动重新按照新的值调整顺序。可以理解为有两列的mysql表,一列存储value,一列存储分值。

package com.cui.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.DefaultTypedTuple; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-redis.xml") public class ZsetTest { @Autowired private RedisTemplate redisTemplate; /** * 存值,指定分值 */ @Test public void setValue(){ redisTemplate.boundZSetOps("nameZset").add("小明", 35); redisTemplate.boundZSetOps("nameZset").add("小红", 99); redisTemplate.boundZSetOps("nameZset").add("小刚", 61); } /** * 查询,由低到高 */ @Test public void getValue1(){ Set nameZset = redisTemplate.boundZSetOps("nameZset").range(0, -1); System.out.println(nameZset); } /** * 查询,由高到低 */ @Test public void getValue2(){ Set nameZset = redisTemplate.boundZSetOps("nameZset").reverseRange(0,-1 ); System.out.println(nameZset); } /** * 增加分数 */ @Test public void testAddGrade(){ redisTemplate.boundZSetOps("nameZset").incrementScore("小明", 30); } /** * 查询值和分数 */ @Test public void getValueAndScore(){ Set<ZSetOperations.TypedTuple> namezset = redisTemplate.boundZSetOps("namezset").reverseRangeWithScores(0,1); for(ZSetOperations.TypedTuple typedTuple:namezset){ System.out.print("姓名:"+typedTuple.getValue()); System.out.println("金币:"+typedTuple.getScore()); } } /** * 删除值类型的数据 */ @Test public void testDelete(){ redisTemplate.delete("nameZset");//删除 } }

TypedTuple是值与分数的封装

2.6 过期时间的设置

以值类型为例:存值时指定过期时间和时间单位

@Test public void setValue(){ redisTemplate.boundValueOps("name").set("王五"); //第一个参数为时间大小,第二个参数为时间类型,如:秒,分,时,天 redisTemplate.boundValueOps("name").expire(5, TimeUnit.MINUTES); }
最新回复(0)