Spring Data Redis框架使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhengzhaoyang122/article/details/82931288

一、Spring-Data-Redis:是Spring家族的一部分,提供了在Spring应用中通过简单的配置访问Redis服务,对Redis底层开发包(Jedis、JRedis、and JRC)进行了高度封装,RedisTemplate提供了Redis各种操作、异常处理及序列化,发布订阅,并对Spring 3.1 Cache进行了实现。

二、Spring-Data-Redis针对Jedis提供了如下功能:
   1)、连接池自动管理,提供了一个高度封装的“RedisTemplate”类。
   2)、针对Jedis客户端中大量API进行了归类封装,将同一类操作封装成了operation接口。
     ● ValueOperations:简单K-V操作。
     ● SetOperations:set类型数据操作。
     ● ZSetOperations:zset类型数据操作。
     ● HashOperations:map类型的数据操作。
     ● ListOperations:list类型的数据操作。
三、需要依赖的jar包如下:

	<!-- 缓存 -->
	<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>

四、在src/main/resource中创建properties文件夹,建立redis-config.properties

redis.host=192.168.0.104
redis.port=6379 
redis.pass= 
redis.database=0 
redis.maxIdle=300 
redis.maxWait=3000 
redis.testOnBorrow=true 

五、在src/main/resource中创建spring文件夹,建立applicationContext-redis.xml

<?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:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <context:property-placeholder location="classpath*:properties/*.properties" />   
  
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <!-- maxIdle :最大空闲数 -->
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <!-- maxWaitMillis:连接时的最大等待毫秒数 -->
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <!-- testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的; -->
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </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>  
      
</beans>  

六、String值类型操作

package com.yintong.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:spring/applicationContext-redis.xml")
public class StringDemo {
	
	@Autowired
	private RedisTemplate redisTemplate;
	@Test
	public void setValue() {//redis中设置值
		redisTemplate.boundValueOps("name").set("zzx");
	}
	@Test
	public void getValue() {
		String name = (String) redisTemplate.boundValueOps("name").get();
		System.out.println(name);
	}
	@Test
	public void deleteValue(){
		redisTemplate.delete("name");;
	}	

}

结果显示:

七 、Set数据类型操作

package com.yintong.test;

import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
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:spring/applicationContext-redis.xml")
public class SetDemo {
	@Autowired
	private RedisTemplate redisTemplate;
	/**
	 * 存入值:Bound系列操作示例,Bound系列操作的优势在于只需要绑定一次,然后可以进行一个系列的操作,代码十分精炼。
	 */
	@Test
	public void setValue() {//redis中设置值
		BoundSetOperations boundSetOps = redisTemplate.boundSetOps("nameSet");
		boundSetOps.add("zzx");
		boundSetOps.add("love");
		boundSetOps.add("fj");
	}
	/**
	 * 提取值
	 */
	@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");
	}
}

结果展示:

八 、List类型操作:

package com.yintong.test;

import java.util.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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {
	@Autowired
	private RedisTemplate redisTemplate;
	/**
	 * 右压栈:后添加的对象排在后边
	 */
	@Test
	public void setValueList() {
		redisTemplate.boundListOps("nameList").rightPush("zzx");
		redisTemplate.boundListOps("nameList").rightPush("fj");
	}
	/**
	 * 显示右压栈集合
	 */
	@Test
	public void getValueList() {
		List range = redisTemplate.boundListOps("nameList").range(0, -1);
		System.out.println(range);
	}
	/**
	 * 左压栈:后添加的对象排在前边
	 */
	@Test
	public void testSetValue2(){	
		redisTemplate.boundListOps("nameList").leftPush("love");
	}
	/**
	 * 查询集合某个元素
	 */
	@Test
	public void testSearchByIndex(){
		Object index = redisTemplate.boundListOps("nameList").index(1);
		System.out.println(index);
	}
	/**
	 * 移除集合某个元素
	 */
	@Test
	public void testRemoveByIndex(){
		redisTemplate.boundListOps("nameList").remove(1, "zzx");
	}
}

九、Hash类型操作:

package com.yintong.test;

import java.util.List;
import java.util.Set;

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:spring/applicationContext-redis.xml")
public class TestHash {
	@Autowired
	private RedisTemplate redisTemplate;
	//插入值
	@Test
	public void setValue() {
		redisTemplate.boundHashOps("nameHash").put("zzx", "boy");
		redisTemplate.boundHashOps("nameHash").put("fj", "girl");
	}
	//提取所有的KEY
	@Test
	public void getKey() {
		Set keys = redisTemplate.boundHashOps("nameHash").keys();
		System.out.println(keys);
	}
	//获取所有值
	@Test
	public void getValues() {
		List values = redisTemplate.boundHashOps("nameHash").values();
		System.out.println(values);
	}
	@Test
	//根据key获取值(常用)
	public void getValueByKey() {
		Object nameValue = redisTemplate.boundHashOps("nameHash").get("zzx");
		System.out.println(nameValue);
	}
	@Test
	//根据key溢出值
	public void deleteKey() {
		redisTemplate.boundHashOps("nameHash").delete("zzx");
	}
}

猜你喜欢

转载自blog.csdn.net/zhengzhaoyang122/article/details/82931288