標籤:
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:
| 1234 |
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 wrapperbean 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中擷取,解決方案:
設定
| 1234567891011121314151617181920212223 |
<!-- 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
Redis資料匯總(十) java client