redis首頁上列出的java 用戶端有JDBC-Redis JRedis Jedis三種,下面分別介紹三種用戶端的優缺點及其他相關的工具.
|
支援redis版本 |
效能 |
維護 |
推薦 |
JDBC-Redis |
|
not good |
|
|
JRedis |
1.2.n release 2.0.0 尚未release版本 |
fast |
|
|
Jedis |
2.0.0 release |
fast |
actively developed |
推薦 |
JDBC-Redis
JDBC-Redis is just a JDBC wrapper for JRedis database.
If you plan on using your code with different back-ends then JDBC is a good way to go. NOTE: It is not a complete JDBC implementation and the NOSQL will bleed through.
If you are going to stay with Redis then I would suggest using the API, which will give you more flexibility. Use a DAO layer pattern to encapsulate your DB Access and down the road that is all you will need to change.
- Romain Hippeau
Redis syntax is completely different from standard SQL so using JDBC doesn't help encapsulating different back-ends as you suggest: I would have to write new queries anyway... – muriloq Jun 16 '10 at 14:00
@muriloq - but the mechanical acquiring and releasing resources is standard. – Romain Hippeau
spring wrapper
Spring provides a wrapper around both implementations(Jredis Jedis) and they're providing serialization/deserialization, amongst other things:
Person p = new Person("Joe", "Trader", 33);
template.convertAndSet("trader:1", p);
Person samePerson = template.getAndConvert("trader:1", Person.class);
Assert.assertEquals(p, samePerson);
上面的方法可能已經調整,請參見最新的 http://static.springsource.org/spring-data/data-keyvalue/docs/1.0.0.M2/reference/html/#redis
放棄spring wrapper
項目中本來打算使用spring wrapper,出於以下原因最終還是放棄,直接使用Jedis,等有時間在把:
1.spring wrapper的版本是1.0.0.M2,裡面有些bug (*)
2.對shard的支援沒有jedis好
3.依賴spring3.0(主要是spring3.0 core中的convert及serializer),我們目前大多項目還是採用spring2.5.6(主要)
4.經過多層封裝後效能還是會有損耗
spring nosql/cross-store
prototype implementation allowing entities to be stored in multiple types of data stores (i.e. JPA and Neo4j or JPA and Redis etc.)
JOhm
JOhm is a blazingly fast Object-Hash Mapping library for Java inspired by the awesome Ohm. The JOhm OHM is a modern-day avatar of the old ORM's like Hibernate with the difference being that we are not dealing with an RDBMS here but with a NoSQL rockstar.
homepage:https://github.com/xetorthio/johm
jedis pool的問題
在使用jedis pool時遇到了這個問題:It seems like server has closed the connection
原因分析:
1.redis server 關閉了此用戶端的串連:server端設定了maxidletime(預設是5分鐘),服務端會不斷迴圈檢測clinet的最後一次通訊時間(lastinteraction),如果大於maxidletime,則關閉串連,並回收相關資源。client在向該串連中寫資料後就會由於server端已經關閉而出現 broken pipe的問題。
2.pool的設定錯誤:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="20" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="1000" />
</bean>
<!-- jedis shard資訊配置 -->
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="*.*.*.*" />
<constructor-arg index="1" value="6379" />
</bean>
<!-- jedis shard pool配置 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo" />
</list>
</constructor-arg>
</bean>
<bean id="jedisCommands" factory-bean="shardedJedisPool"
factory-method="getResource" />
上面的這種配法在spring初始化時擷取一次執行個體化jedisCommands,而後每次的redis的調用時並未從pool中擷取
解決方案:
設定
<!-- POOL配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="20" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="1000" />
<property name="testOnBorrow" value="true"/>
</bean>
<!-- jedis shard資訊配置 -->
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="*.*.*.*" />
<constructor-arg index="1" value="6379" />
</bean>
<!-- jedis shard pool配置 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo" />
</list>
</constructor-arg>
</bean>
參考:
http://stackoverflow.com/questions/3047010/best-redis-library-for-java
https://github.com/xetorthio/johm
https://github.com/xetorthio/jedis/issues/closed#issue/76