歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Redis Java Client

Redis Java Client

日期:2017/2/27 16:00:39   编辑:Linux教程
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>
Copyright © Linux教程網 All Rights Reserved