Spring Data Redis的配置、连接及常用方法的使用

Spring Data Redis的概述

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

Spring Data Redis针对Jedis提供了如下功能:

  • 1连接池自动管理 提供了一个高度封装的RedisTemplate类
  • 2、针对Jedis客户端中大量的API进行了归类封装 将同一类操作封装为operation接口
    ValueOperation:简单的K-V操作
    HashOperation:针对Map(Hash)类型的数据操作
    ListOperation:针对List类型的数据操作
    SetOperation:Set类型数据操作
    ZSetOperation:ZSet/SortedSet类型数据操作

使用步骤:

1、导入依赖

必须导入Jedis 因为Spring Data Redis是在Jedis的基础上进行的再次封装

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

2、配置

①、创建一个配置文件redis-config.properties

# 服务器的IP
redis.host=127.0.0.1
# 服务器的端口号
redis.port=6379
# 要连接的Redis的密码 没密码则不填
redis.pass=123456
# 操作第几个数据库
redis.database=0
# 连接池最大空闲数
redis.maxIdle=300
# 连接池最大等待时间
redis.maxWait=3000
# 在borrow一个Jedis实例时 是否提前进行validate检测操作
# 若为true 则得到的Jedis实例均为可用的
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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.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/aop/spring-mvc.xsd
		http://www.springframework.org/schema/cache
		http://www.springframework.org/schema/tx/spring-cache.xsd">

    <!-- 加载外部的配置文件 -->
    <context:property-placeholder location="classpath*:*.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="${redis.testOnBorrow}"/>
    </bean>

    <!-- Jedis连接工厂 -->
    <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:poolConfig-ref="poolConfig" />

    <!-- RedisTemplate类 负责操作Redis服务器封装工具类 -->
    <bean id="redisTemplate" class=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值