Redis/C + +, Java Client connection

Source: Internet
Author: User

About Redis

Redis This presumably everybody knows, about Redis installation reference here, Redis uses the documentation to see here, the English document.

Usage of Redis cclient

The cclient Hiredis of Redis is widely used, the following mainly introduces it.

1,hiredis installation, configuration

Hiredisclient has the corresponding file under Deps/hiredis after Redis decompression. Assume that your installation package does not have a corresponding file to download here.

CD Deps/hiredis (download Hiredis to extract the extracted folder).

Make

Finally add the corresponding files to the system folder, so that the system can be searched.

Mkdir/usr/lib/hiredis

CP Libhiredis.so/usr/lib/hiredis #将动态连接库libhiredis. So to/usr/lib/hiredis

Mkdir/usr/include/hiredis

CP Hiredis.h/usr/include/hiredis

Last note :

Need to update library cache, Run command : sudo ldconfig/usr/lib/

As the program starts, it will go to/etc/ld.so.cache to find the library you want to use, or it will appear such as the following error : Error while loading shared libraries:libhiredis.so.0.10: Cannot open shared object File:no such file or directory. Here are some references to this knowledge.

2,cclient Interview with Redis

Upon completion of the above work, you will be able to connect Hiredisserver. Code such as the following (compilation environment for the Ubuntu system on the GCC)

Redis_test.c#include <stdio.h> #include <string.h> #include 

Under the Linux system. O is equivalent to the obj file in Windows. A is a good number of. O Together for static connection. So is a shared object that is used for dynamic connections, and DLLs are almost identical.
So explain the two methods of compiling:

One, using static link method

Copy the Hiredis compiled LIBREDIS.A to the current program folder and run the following command:

Gcc-o Test redis_test.c LIBREDIS.A

Execute the program:./test, can see success.

Two, dynamic link

Run such as the following command: Gcc-o test redis_test.c libredis.so (note sudo ldconfig/usr/lib/before using shared libraries)

Execute the program:./test, can see success.


Usage of Redis javaclient

1,jedis Download

Jedis is a relatively wide range of javaclient used in Redis Javaclient,redis, the following describes how Java links and experimental redis.

Jedis here to download Jedis source code via URL Https://github.com/xetorthio/jedis.

2, compiling the Jedis jar package

The source code found in step 1 is not a jar package, and it is more troublesome to use the source codes directly for programming, and now it can be used directly in the next use of the package as a jar file.

Create a new jedisproject with Eclipse put the extracted Redis file under the SRC folder of the new project and find the missing corresponding Commons Pool 2.2 package, need to be here

Download the executable package Commons-pool2-2.2-bin.zip, unzip and add the Commons-pool2-2.2.jar and other dependent packages via the Add External in Build path Jars.

To the current project, the final export jar package, named: Jedis-2.4.2.jar, is able to download http://download.csdn.net/detail/gfsfg8545/7357837 here.

3, use Jedis to access Redis

New project, after the introduction of the relevant jar package, only a new Jedis object, you can do Redis-related operations. Here's a simple Jedis example:

Package cn.daniel.test;/** * * @author Daniel * Redis java Client test. * Time 2014-05-16 */import java.util.list;import java.util.map;import java.util.map.entry;import Redis.clients.jedis.jedis;public class Jredistest {public void Redistest () {Jedis Redis = new Jedis ("172.16.0.126", 6379) ;//Connect Server:ip port//redis.auth ("user");//String operator//set Key-valueredis.set ("Key1", "1"); Redis.set (" Key2 "," 2 ");//Mset Key-valueredis.mset (" Key3 "," 3 "," Key4 "," 4 ");//Get Key-valuesystem.out.println (" Key:key1 Value: "+ Redis.get ("Key1"));//MGET key [key ...] list<string> list = Redis.mget ("Key1", "Key2", "Key3", "Key4"); for (String s:list) {System.out.println (s);} Hset key field Valueredis.hset ("website", "CSDN", "http://csdn.net/"), Redis.hset ("website", "Daniel", "http:// BLOG.CSDN.NET/DANIEL_USTC ");//Hgetall, Get all the fields and values in the hash map<string, string> Map = REDIS.HG Etall ("website"); for (entry<string, string> entry:map.entrySet ()) {SYSTEM.OUT.PRintln ("key:" + entry.getkey () + "value:" + entry.getvalue ());} Quitredis.quit ();} Redistestpublic static void Main (string[] args) {jredistest test = new Jredistest (); Test.redistest ();}}

Follow the procedure above to access Redis. can also be used to manage jar dependencies with Maven, which is said to be more useful.

In practice, the connection pooling is usually used, and no connection is used throughout the project. The Jedis pool is implemented based on the Apache common pool, so the main project is to import the corresponding Commons-pool2 package, code such as the following:

Package cn.ustc.daniel.test;/** * @author Daniel * Redis java Client test. * Jredispool * Time 2014-05-18 */import redis.clients.jedis.jedis;import redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;public class Jedispooltest {private static Jedispool pool=null;/** * Create Pool */ private static Jedispool Createjedispool () {if (pool = = null) {//Establish connection pool configuration parameters jedispoolconfig Config = new Jedispoolconfig (); /Set Jedis maximum number of connections config.setmaxtotal (100);//Set maximum clogging time, number of milliseconds config.setmaxwaitmillis (1000),//Set maximum spare connections Config.setmaxidle (10); Create connection pool IP Port pool = new Jedispool (config, "172.16.0.11", 6379);} return pool;} /** * Synchronous initialization in multithreaded environment */private static synchronized void Poolinit () {if (pool = = null) pool=cre    Atejedispool ();            }/** * Gets a Jedis object * * @return */public static Jedis Getjedis () {if (pool = = null)        Poolinit ();    return Pool.getresource (); }/** * Returns a connection */public staticvoid Returnres (Jedis Jedis) {pool.returnresource (Jedis); }//main public static void main (string[] args) {Jedis Jedis = Jedispooltest.getjedis ();   & nbsp;    jedis.set ("name", "Jedispool");                 string value = Jedis.get ("name");        jedispooltest.returnres (  Jedis);        system.out.println (value);  }}



References:

Http://www.cnitblog.com/yunshichen/archive/2009/08/28/61065.html

http://flyingsnail.blog.51cto.com/5341669/1371650

Redis/C + +, Java Client connection

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.