1. 程式人生 > >Spring Redis Jedis簡單用法

Spring Redis Jedis簡單用法

maven依賴

  1. <properties>  
  2.     <spring.version>4.3.10.RELEASE</spring.version>
  3.     <!-- redis 版本 -->  
  4.     <redis.version>2.9.0</redis.version>  
  5.     <
    spring.redis.version>1.8.4.RELEASE</spring.redis.version>  
  6. </properties>  
  7. <dependency>  
  8.     <groupId>redis.clients</groupId>  
  9.     <artifactId>jedis</artifactId>  
  10.     <version>${redis.version}</version>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>org.springframework.data</groupId>  
  14.     <artifactId>spring-data-redis
    </artifactId>  
  15.     <version>${spring.redis.version}</version>  
  16. </dependency>  
  17. <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.2</version>
    </dependency>
redis.properties
  1. redis.host=127.0.0.1  
  2. redis.port=6379  
  3. redis.password=""  
  4. redis.maxIdle=400  
  5. redis.maxTotal=6000  
  6. redis.maxWaitMillis=1000  
  7. redis.blockWhenExhausted=true  
  8. redis.testOnBorrow=true  
  9. redis.timeout=100000  
  10. defaultCacheExpireTime=60  
spring-redis.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd     
  6.     http://www.springframework.org/schema/context     
  7.     http://www.springframework.org/schema/context/spring-context-4.3.xsd"  
  8.     default-lazy-init="false">  
  9.       
  10.     <!-- 載入配置檔案 -->    
  11.     <context:property-placeholder location="classpath:redis.properties" />    
  12.     <!-- redis資料來源 -->  
  13.     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  14.         <!-- 最大空閒數 -->  
  15.         <property name="maxIdle" value="${redis.maxIdle}" />  
  16.         <!-- 最大空連線數 -->  
  17.         <property name="maxTotal" value="${redis.maxTotal}" />  
  18.         <!-- 最大等待時間 -->  
  19.         <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />  
  20.         <!-- 連線超時時是否阻塞,false時報異常,ture阻塞直到超時, 預設true -->  
  21.          <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />   
  22.         <!-- 返回連線時,檢測連線是否成功 -->  
  23.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  24.     </bean>  
  25.   
  26.     <!-- Spring-redis連線池管理工廠 -->  
  27.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  28.         <!-- IP地址 -->  
  29.         <property name="hostName" value="${redis.host}" />  
  30.         <!-- 埠號 -->  
  31.         <property name="port" value="${redis.port}" />  
  32.         <!-- 超時時間 預設2000-->  
  33.         <property name="timeout" value="${redis.timeout}" />  
  34.         <!-- 連線池配置引用 -->  
  35.         <property name="poolConfig" ref="poolConfig" />  
  36.         <!-- usePool:是否使用連線池 -->  
  37.         <property name="usePool" value="true"/>  
  38.     </bean>  
  39.   
  40.     <!-- redis template definition -->  
  41.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  42.         <property name="connectionFactory" ref="jedisConnectionFactory" />  
  43.         <property name="keySerializer">  
  44.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  45.         </property>  
  46.         <property name="valueSerializer">  
  47.             <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  48.         </property>  
  49.         <property name="hashKeySerializer">  
  50.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  51.         </property>  
  52.         <property name="hashValueSerializer">  
  53.             <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  54.         </property>  
  55.          <!--開啟事務  -->    
  56.         <property name="enableTransactionSupport" value="true"></property>    
  57.     </bean>  
  58.       
  59.     <!--自定義redis工具類,在需要快取的地方注入此類  -->    
  60.    
    1. <bean id="redisrCacheManager" class="redis.RedisCacheManager">    
         
    <constructor-arg> <ref bean="redisTemplate" /> </constructor-arg>
      </bean>  
</beans>   一、spring-data-redis功能介紹
jedis客戶端在程式設計實施方面存在如下不足:
1)connection管理缺乏自動化,connection-pool的設計缺少必要的容器支援。
2)資料操作需要關注“序列化”/“反序列化”,因為jedis的客戶端API接受的資料型別為string和byte,對結構化資料(json,xml,pojo等)操作需要額外的支援。
3)事務操作純粹為硬編碼。
4)pub/sub功能,缺乏必要的設計模式支援,對於開發者而言需要關注的太多。
spring-data-redis針對jedis提供瞭如下功能:
1.連線池自動管理,提供了一個高度封裝的“RedisTemplate”類
2.針對jedis客戶端中大量api進行了歸類封裝,將同一型別操作封裝為operation介面
ValueOperations:簡單K-V操作
SetOperations:set型別資料操作
ZSetOperations:zset型別資料操作
HashOperations:針對map型別的資料操作
ListOperations:針對list型別的資料操作
3.提供了對key的“bound”(繫結)便捷化操作API,可以通過bound封裝指定的key,然後進行一系列的操作而無須“顯式”的再次指定Key,即BoundKeyOperations:
BoundValueOperations
BoundSetOperations
BoundListOperations
BoundSetOperations
BoundHashOperations
4.將事務操作封裝,有容器控制。
5.針對資料的“序列化/反序列化”,提供了多種可選擇策略(RedisSerializer)
JdkSerializationRedisSerializer:POJO物件的存取場景,使用JDK本身序列化機制,將pojo類通過ObjectInputStream/ObjectOutputStream進行序列化操作,最終redis-server中將儲存位元組序列。是目前最常用的序列化策略。
StringRedisSerializer:Key或者value為字串的場景,根據指定的charset對資料的位元組序列編碼成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封裝。是最輕量級和高效的策略。
JacksonJsonRedisSerializer:jackson-json工具提供了javabean與json之間的轉換能力,可以將pojo例項序列化成json格式儲存在redis中,也可以將json格式的資料轉換成pojo例項。因為jackson工具在序列化和反序列化時,需要明確指定Class型別,因此此策略封裝起來稍微複雜。【需要jackson-mapper-asl工具支援】
OxmSerializer:提供了將javabean與xml之間的轉換能力,目前可用的三方支援包括jaxb,apache-xmlbeans;redis儲存的資料將是xml工具。不過使用此策略,程式設計將會有些難度,而且效率最低;不建議使用。【需要spring-oxm模組的支援】
針對“序列化和發序列化”中JdkSerializationRedisSerializer和StringRedisSerializer是最基礎的策略,原則上,我們可以將資料儲存為任何格式以便應用程式存取和解析(其中應用包括app,hadoop等其他工具),不過在設計時仍然不推薦直接使用“JacksonJsonRedisSerializer”和“OxmSerializer”,因為無論是json還是xml,他們本身仍然是String。如果你的資料需要被第三方工具解析,那麼資料應該使用StringRedisSerializer而不是JdkSerializationRedisSerializer。如果你的資料格式必須為json或者xml,那麼在程式設計級別,在redisTemplate配置中仍然使用StringRedisSerializer,在儲存之前或者讀取之後,使用“SerializationUtils”工具轉換轉換成json或者xml,請參見下文例項。
6.基於設計模式,和JMS開發思路,將pub/sub的API設計進行了封裝,使開發更加便捷。
7.spring-data-redis中,並沒有對sharding提供良好的封裝,如果你的架構是基於sharding,那麼你需要自己去實現,這也是sdr和jedis相比,唯一缺少的特性。
二、serializer策略
spring-data-redis提供了多種serializer策略,這對使用jedis的開發者而言,實在是非常便捷。sdr提供了4種內建的serializer:
JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable介面,ObjectInputStrean,ObjectOutputStream),資料以位元組流儲存
StringRedisSerializer:字串編碼,資料以string儲存
JacksonJsonRedisSerializer:json格式儲存
OxmSerializer:xm