spring、spring-data-redis整合使用


一、Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日志型、Key-Value數據庫,並提供多種語言的API。

  從2010年3月15日起,Redis的開發工作由VMware主持。從2013年5月開始,Redis的開發由Pivotal贊助。

  目前:相信很多人都知道redis的應用和普遍性吧,因為它的靈活性,讓我們在使用的時候就更加得心應手了。

  這里介紹spring-data-redis的基本用法,這里需要依賴包jedis的支持

二、redis的部署

1、具體的部署配置查看:http://www.cnblogs.com/ll409546297/p/6993778.html

2、這里需要注意幾個地方(主要是配置redis.conf)

  1)因為我是守護線程啟動,后台自啟。需要修改daemonize 為yes 

daemonize yes

  2)因為緩存服務器,一般都是外部服務器,在使用的時候需要設置密碼和注釋掉bind 127.0.0.1。不然在使用的時候會拋出pool 和 password異常

  密碼設置:requirepass 123456 (找到相關注解解除,並修改密碼)

  注釋掉bind 127.0.0.1

3、基本測試一下就可以了(可以重啟測試一下)

4、這里redis的部署基本完成了。

三、pom.xml(導包)

   <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

四、配置spring-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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
   
   <!-- 配置連接池 -->
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
           <property name="maxTotal" value="10"></property>  
        <property name="maxIdle" value="10"></property>  
        <property name="minIdle" value="2"></property>  
        <property name="maxWaitMillis" value="15000"></property>  
        <property name="minEvictableIdleTimeMillis" value="300000"></property>  
        <property name="numTestsPerEvictionRun" value="3"></property>  
        <property name="timeBetweenEvictionRunsMillis" value="60000"></property>  
        <property name="testOnBorrow" value="true"></property>  
        <property name="testOnReturn" value="true"></property>  
        <property name="testWhileIdle" value="true"></property>      
   </bean>
   <!-- 連接工廠 --> 
   <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
           <property name="hostName" value="192.168.5.100"/>
           <property name="port" value="6379"/>
           <property name="password" value="123456"/>
           <property name="usePool" value="true"/>
           <property name="poolConfig" ref="jedisPoolConfig"/>
   </bean>
   <!-- 用於數據交互 -->
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
           <property name="connectionFactory" ref="jedisConnFactory"/>
   </bean>
</beans>

五、測試

     JedisShardInfo  jedisShardInfo = new JedisShardInfo("192.168.5.100", 6379);
        jedisShardInfo.setPassword("123456");//密碼認證
        Jedis jedis = new Jedis(jedisShardInfo);
        jedis.set("fucking", "fucking");
        System.out.println(jedis.get("fucking"));

六、使用

@Autowired
private RedisTemplate<String, String> redisTemplate;

redisTemplate.opsForValue().set("test", "test");
String obj = redisTemplate.opsForValue().get("test");
System.out.println(obj);

注意:這里注入不一定只有一個,可以是不同類型

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM