Redis(二)——SpringDataRedis

Redis

redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 、MongoDB等

Jedis

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

Spring Data Redis

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

spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作

案例

在这里插入图片描述

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wjl.demo</groupId>
	<artifactId>SpringDataRedisDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<spring.version>4.2.4.RELEASE</spring.version>
	</properties>
	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
		</dependency>

		<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>

	</dependencies>
</project>

redis-config.properties

# Redis settings 
# server IP 
redis.host=192.168.21.135
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

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">  
        <!-- 最大空闲 -->
     <property name="maxIdle" value="${redis.maxIdle}" /> 
       <!--  连接时的最大等待毫秒数   -->
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
      <!-- 在提取一个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>  

TestValue.java

package com.wjl.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 TestValue {
	
	@Autowired
	private RedisTemplate redisTemplate;
	@Test
	public void testSetValue() {
		redisTemplate.boundValueOps("name").set("wjl");;
	}
	
	@Test
	public void testgetValue() {
		String name = (String) redisTemplate.boundValueOps("name").get();
		System.out.println(name);
	}
	
	@Test
	public void deleteValue() {
		redisTemplate.delete("name");
	}
}

TestSet.java

package com.wjl.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.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 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 set = redisTemplate.boundSetOps("nameset").members();
		System.out.println(set);//  [张良, 陈平, 刘邦]
	}
	
	@Test
	public void removeValue() {
		Long num = redisTemplate.boundSetOps("nameset").remove("张良");
		System.out.println(num);
	}
	@Test
	public void deleteValue() {
		redisTemplate.delete("nameset");
	}
}

TestList.java

package com.wjl.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 testSetValue1() {
		redisTemplate.boundListOps("nameList1").rightPush("刘邦");
		redisTemplate.boundListOps("nameList1").rightPush("萧何");
		redisTemplate.boundListOps("nameList1").rightPush("韩信");
	}
	
	@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 searchByIndex(){
		String str = (String) redisTemplate.boundListOps("nameList2").index(1);
		System.out.println(str);
	}
	
	//移除值
	@Test
	public void removeValue() {
		Long num = redisTemplate.boundListOps("nameList1").remove(2, "韩信");
		System.out.println(num);//删除几个
	}
}

TestHash.java

package com.wjl.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 testSetValue() {
		redisTemplate.boundHashOps("nameHash").put("num1", "刘邦");
		redisTemplate.boundHashOps("nameHash").put("num2", "项羽");
		redisTemplate.boundHashOps("nameHash").put("num3", "韩信");
	}
	
	//获取所有的keys
	@Test
	public void getKeys() {
		Set keys = redisTemplate.boundHashOps("nameHash").keys();
		System.out.println(keys);//[num1, num2, num3]
	}
	
	//获取所有的值
	@Test
	public void getValues() {
		List values = redisTemplate.boundHashOps("nameHash").values();
		System.out.println(values);//[刘邦, 项羽, 韩信]
	}
	
	//根据KEY获取值
	@Test
	public void searchValueByKey() {
		String str = (String) redisTemplate.boundHashOps("nameHash").get("num1");
		System.out.println(str);
	}
	
	@Test
	public void removeValue() {
		Long delete = redisTemplate.boundHashOps("nameHash").delete("num2");
		System.out.println(delete);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值