Spring Data Redis 整合

來源:互聯網
上載者:User

標籤:.com   connect   framework   serial   nbsp   操作   用戶端   直接   host   

  

    首先現在redis包,官方網址是 http://redis.io/download 

 

    下載好了後直接解壓Redis壓縮包到指定目錄:

    

    

    使用對應位元作業系統檔案夾下面redis-server.exe命令啟動redis (測試命令redis-server.exe 服務啟動程式redis-cli.exe 用戶端命令列工具redis.conf 服務組態檔通過redis-server.exe啟動服務,預設連接埠 6379通過redis-cli.exe 啟動用戶端工具)。

  

    1.在程式中通過maven座標,引入spring data redis  ,命令如下所示:

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.4.1.RELEASE</version>
  </dependency>

     2.配置applicationContext.xml代碼如下所示:   

  <!-- 配置redis串連池 -->

  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">

  <property name="maxIdle" value="300"/>

  <property name="maxWaitMillis" value="3000"/>

  <property name="testOnBorrow" value="true"/>

  </bean>

  <!-- 配置整合jedis工廠串連池 -->

  <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

  <property name="hostName" value="localhost"/>

  <property name="port" value="6379"/>

  <property name="poolConfig" ref="poolConfig"/>

  </bean>

  <!-- 配置整合redis模版 -->

  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

  <property name="connectionFactory" ref="redisConnectionFactory"/>

  <property name="keySerializer">

  <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

  </property>

  <property name="valueSerializer">

  <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

  </property>

  </bean>

     3.編寫測試代碼如下所示:    

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataRedis {
  @Resource
  private RedisTemplate<String,String> redistemplate;
  @Test
  public void testRedis(){
    redistemplate.opsForValue().set("testkey", "myTestvaule",1, TimeUnit.DAYS);
    }
}

 

 

      

第一次寫部落格有許多不足之處請大佬們見諒喲!

Spring Data Redis 整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.