spring-data-redis

spring-data-redis

  (1) 创建工程,引入相关依赖

        <!-- redis缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!-- spring与redis整合 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>

  (2) 编写配置文件和属性文件

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

    <!-- 加载properties配置文件 -->
    <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}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    
    <!-- spring管理redis的连接工厂 -->
    <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" />
    
    <!-- 配置springRedis模板 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory" />
    </bean>

</beans>  

    redis-config.properties

# Redis settings 
# server IP 
redis.host=127.0.0.1
# server port 
redis.port=6379
# server pass 
redis.pass= 
# use dbIndex 
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;  
redis.maxWait=3000
# 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的  
redis.testOnBorrow=true 

  (3) 测试

    1) 引入相关依赖

    2) 编写类,注入redisTemplate

    3) 使用redisTemplate操作redis CRUD

附:redisTemplate各种数据类型操作

  (1) 值类型

  redisTemplate.boundValueOps("name").set("value");

  String str = (String) redisTemplate.boundValueOps("name").get();

  redisTemplate.delete("name");

  (2) Set类型

  redisTemplate.boundSetOps("nameset").add("value1");

  redisTemplate.boundSetOps("nameset").add("value2");

  Set members = redisTemplate.boundSetOps("nameset").members();

  redisTemplate.boundSetOps("nameset").remove("value1");

  redisTemplate.delete("nameset");

   (3) List类型

  redisTemplate.boundListOps("namelist1").rightPush("listValue1");

  redisTemplate.boundListOps("namelist1").rightPush("listValue2");// 右压栈:["listValue1", "listValue2"]

  List list = redisTemplate.boundListOps("namelist1").range(0, 10);// 无法定义刚好获取整个集合,只能定义足够大的数来获取

  redisTemplate.boundListOps("namelist2").leftPush("listValue3");// 左压栈:["listValue3", "listValue1", "listValue2"]

  String s = (String) redisTemplate.boundListOps("namelist1").index(1);// 根据索引移除元素

  redisTemplate.boundListOps("namelist1").remove(1, "listValue2");// 移除某个元素,如果有多个相同元素,只移除一个

  (4) Hash类型

  redisTemplate.boundHashOps("namehash").put("a", "value1");

  redisTemplate.boundHashOps("namehash").put("b", "value2");

  Set s = redisTemplate.boundHashOps("namehash").keys();// 获取所有key

  List values = redisTemplate.boundHashOps("namehash").values();// 获取所有值

  Object object = redisTemplate.boundHashOps("namehash").get("b");// 根据key取值

  redisTemplate.boundHashOps("namehash").delete("c");// 根据key移除值

posted @ 2019-01-25 15:13  指北的司南  阅读(217)  评论(0编辑  收藏  举报