spring 整合redis 簡單使用

來源:互聯網
上載者:User

標籤:spring   redis   

1.簡單介紹

redis 是基於C語言開發。

redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value類型相對更多,包括string(字串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(雜湊類型)。

redis 是一個 快取資料庫(片面的理解) 既可以做緩衝,也可以將資料持久化到磁碟中!

 2.pom.xml 引入相關jar (曾經因jar 版本問題出現報錯,請注意)

<dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-pool2</artifactId>    <version>2.2</version></dependency><dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-redis</artifactId>    <version>1.7.5.RELEASE</version></dependency><dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.9.0</version></dependency>


3.spring-redis.xml 設定檔,配置關鍵bean  redisTemplate

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  xmlns:aop="http://www.springframework.org/schema/aop"  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-3.0.xsd">    <!--    <context:property-placeholder location="classpath:redis-config.properties"/>          -->            <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">     <property name="maxIdle" value="${redis.maxIdle}" />     <property name="maxTotal" value="${redis.maxTotal}" />     <property name="blockWhenExhausted" value="true" />     <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />     <property name="testOnBorrow" value="${redis.testOnBorrow}" />     </bean>    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">     <property name="hostName" value="${redis.hostname}" />     <property name="port" value="${redis.port}"/>     <property name="poolConfig" ref="jedisPoolConfig" />     <property name="usePool" value="true"/>   </bean>     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">      <property name="connectionFactory"  ref="jedisConnectionFactory" />      <property name="keySerializer">        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />      </property>       <property name="valueSerializer">        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />      </property>      <property name="hashKeySerializer">         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>       </property>      <property name="hashValueSerializer">        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>       </property>    </bean>     </beans>



上文中使用到的設定檔 redis-config.properteis

redis.maxIdle=1redis.maxTotal=5redis.maxWaitMillis=30000redis.testOnBorrow=trueredis.hostname=127.0.0.1redis.port=6379


4.redis 有4個關鍵的介面如下


private ValueOperations<K, V> valueOps;

private ListOperations<K, V> listOps;

private SetOperations<K, V> setOps;

private ZSetOperations<K, V> zSetOps;



分別對應redis的資料類型:string(字串),hash(雜湊),list(列表),set(集合)及zset(sorted set:有序集合)


具體使用如下,上代碼:

//添加字串ValueOperations<String, String> value = this.redisTemplate.opsForValue();value.set("hello", "討厭");System.out.println(value.get("hello"));//添加 一個 hash集合HashOperations<String, Object, Object>  hash =redisTemplate.opsForHash();hash.put("沃爾瑪","水果", "蘋果");hash.put("沃爾瑪","飲料", "紅牛");System.out.println(hash.entries("沃爾瑪"));//添加一個list 集合ListOperations<String, Object> list = redisTemplate.opsForList();list.rightPush("課程", "chinese");list.rightPush("課程", "englise");System.out.println(list.range("lpList", 0, 1));//添加 一個 set 集合SetOperations<String, Object> set = redisTemplate.opsForSet();set.add("lpSet", "lp");set.add("lpSet", "26");set.add("lpSet", "178cm");//輸出 set 集合System.out.println(set.members("lpSet"));//添加有序的 set 集合ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();zset.add("lpZset", "lp", 0);zset.add("lpZset", "26", 2);zset.add("lpZset", "178cm", 1);//輸出有序 set 集合System.out.println(zset.rangeByScore("lpZset", 0, 2));




本文出自 “布拉君君” 部落格,請務必保留此出處http://5148737.blog.51cto.com/5138737/1976501

spring 整合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.