十. redis java client

來源:互聯網
上載者:User

    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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.