Spring Redis Jedis簡單用法
阿新 • • 發佈:2018-11-11
maven依賴
- <properties>
- <spring.version>4.3.10.RELEASE</spring.version>
- <!-- redis 版本 -->
- <redis.version>2.9.0</redis.version>
- <
- </properties>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>${redis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis
- <version>${spring.redis.version}</version>
- </dependency>
- <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
- redis.host=127.0.0.1
- redis.port=6379
- redis.password=""
- redis.maxIdle=400
- redis.maxTotal=6000
- redis.maxWaitMillis=1000
- redis.blockWhenExhausted=true
- redis.testOnBorrow=true
- redis.timeout=100000
- defaultCacheExpireTime=60
- <?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"
- 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"
- default-lazy-init="false">
- <!-- 載入配置檔案 -->
- <context:property-placeholder location="classpath:redis.properties" />
- <!-- redis資料來源 -->
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <!-- 最大空閒數 -->
- <property name="maxIdle" value="${redis.maxIdle}" />
- <!-- 最大空連線數 -->
- <property name="maxTotal" value="${redis.maxTotal}" />
- <!-- 最大等待時間 -->
- <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
- <!-- 連線超時時是否阻塞,false時報異常,ture阻塞直到超時, 預設true -->
- <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />
- <!-- 返回連線時,檢測連線是否成功 -->
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean>
- <!-- Spring-redis連線池管理工廠 -->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <!-- IP地址 -->
- <property name="hostName" value="${redis.host}" />
- <!-- 埠號 -->
- <property name="port" value="${redis.port}" />
- <!-- 超時時間 預設2000-->
- <property name="timeout" value="${redis.timeout}" />
- <!-- 連線池配置引用 -->
- <property name="poolConfig" ref="poolConfig" />
- <!-- usePool:是否使用連線池 -->
- <property name="usePool" value="true"/>
- </bean>
- <!-- redis template definition -->
- <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>
- <!--開啟事務 -->
- <property name="enableTransactionSupport" value="true"></property>
- </bean>
- <!--自定義redis工具類,在需要快取的地方注入此類 -->
-
- <bean id="redisrCacheManager" class="redis.RedisCacheManager">
</bean> - <bean id="redisrCacheManager" class="redis.RedisCacheManager">
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