10. redis Java client

Source: Internet
Author: User

the Java clients listed on the redis homepage are JDBC-redis Jedis. The following describes the advantages and disadvantages of the three clients and other related tools.

support for redis Performance maintenance recommended
JDBC-redis not good
jredis 1.2.n release
2.0.0 not release
fast
Jedis 2.0.0 release fast actively developed recommended

 

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 wocould 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 wowould have to write new queries anyway... -muriloq Jun 16' 10

@ 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 =NewPerson ("Joe","Trader", 33 );
Template. convertandset ("TRADER: 1", P );
Person sameperson = template. getandconvert ("TRADER: 1", Person.Class);
Assert. assertequals (p, sameperson );
The method above may have been adjusted, see the latest http://static.springsource.org/spring-data/data-keyvalue/docs/1.0.0.M2/reference/html/#redis

Discard springWrapper

The project was originally intended to use spring wrapper. In the end, it gave up and directly used Jedis for the following reasons:

1. The spring wrapper version is 1.0.0.m2, which contains some bugs (*)

2. Jedis is not good for Shard support

3. dependent on spring3.0 (mainly convert and serializer in spring3.0 core). Currently, most of our projects still use spring2.5.6 (mainly)

4. After multi-layer encapsulation, performance will still suffer losses.

SpringNosql/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 identified red 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 problems

This problem occurs when using Jedis pool: It seems like server has closed the connection

Cause analysis:

1. redis server closes the connection to this client: the server sets maxidletime (5 minutes by default), and the server continuously checks the last communication time (lastinteraction) of clinet. If the connection is greater than maxidletime, closes the connection and recycles related resources. After the client writes data to the connection, the broken pipe problem occurs because the server has been closed.

2. Pool setting error:

<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 information configuration -- >
< Bean id = "Jedis . Shardinfo "Class =" redis . Clients . Jedis . Jedisshardinfo " >
< Constructor-Arg Index = "0" value = "*.*.*.*"/ >
< Constructor-Arg Index = "1" value = "6379 "/ >
< /Bean >

< ! -- Jedis shard pool configuration -- >
< 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 "/>

The above configuration method obtains an instantiated jediscommands at Spring initialization, and does not obtain the jediscommands from the pool each time redis is called.

Solution:

Set

 < ! -- Pool configuration -- > 
< 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 information configuration -- >
< Bean id = "Jedis . Shardinfo "Class =" redis. Clients . Jedis . Jedisshardinfo " >
< Constructor-Arg Index = "0" value = "*.*.*.*"/ >
< Constructor-Arg Index = "1" value = "6379 "/ >
< /Bean >

< ! -- Jedis shard pool configuration -- >
< 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 >

Refer:
Http://stackoverflow.com/questions/3047010/best-redis-library-for-java
Https://github.com/xetorthio/johm

Https://github.com/xetorthio/jedis/issues/closed#issue/76

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.