Redis Introduction
For details about redis installation, refer to here. For more information about redis usage, see here.
How to Use the Redis C Client
Redis c client Hiredis is widely used. The following describes it.
1. install and configure Hiredis
The Hiredis client has corresponding files under deps/hiredis after redis decompression. If your installation package does not have the corresponding file, you can download it here.
Cd deps/hiredis (if you download hiredis, decompress it to the decompressed directory ).
Make
Finally, add the corresponding files to the system directory so that the system can search.
Mkdir/usr/lib/hiredis
Cp libhiredis. so/usr/lib/hiredis # dynamically connect libhiredis. so to/usr/lib/hiredis
Mkdir/usr/include/hiredis
Cp hiredis. h/usr/include/hiredis
Note:
Update the database cache and run the following command: sudo ldconfig/usr/lib/
Because the program will go to/etc/ld at startup. so. cache to find the library to use. Otherwise, the following error occurs: error while loading shared libraries: libhiredis. so.0.10: cannot open shared object file: No such file or directory. For more information, see here.
2. Client C accesses Redis
After completing the above work, you can connect to the hiredis server. The Code is as follows (the compiling environment is gcc on ubuntu)
// redis_test.c#include <stdio.h>#include <string.h>#include
In linux. o is equivalent to the obj file in windows. a is many. o is used together for static connections. so is shared object, used for dynamic connection, similar to dll.
Therefore, the two compilation methods are described as follows:
I. Static connection
Copy libredis. a compiled by hiredis to the current program directory and execute the following command:
Gcc-o test redis_test.c libredis.
Run the program:./test.
2. Dynamic Link
Run the following command: gcc-o test redis_test.c libredis. so (note that sudo ldconfig/usr/lib/is required before using the shared library /)
Run the program:./test.
Usage of Redis Java client
1. Download Jedis
Jedis is a Java client of Redis, which is widely used by Java clients in Redis. The following describes how to link Java and experiment Redis.
Jedis download the jedis source code through the Web site https://github.com/xetorthio/jedis here.
2. Compile the Jedis jar package.
The source code downloaded in step 1 finds that there is no jar package. It is troublesome to directly use the source code for programming. Now we can package the source code as a jar file and use it directly next time.
Use eclipse to create a Jedis project and place the decompressed redis file under the src directory of the new project. If the corresponding Commons Pool 2.2 package is missing, you need to go here.
Download the executable package commons-pool2-2.2-bin.zip, decompress the package and Add the dependency packages such as commons-pool2-2.2.jar to the Add External Jars in build path
To the current project, finally export jar package named: jedis-2.4.2.jar, you can download the http://download.csdn.net/detail/gfsfg8545/7357837 here.
3. Access Redis using Jedis
Create a new project. After the relevant jar package is introduced, only a new Jedis object can be used to perform redis-related operations. Here is a simple jedis instance:
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.hgetAll("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();}}
Run the above program to access Redis. You can also use maven to manage jar package dependencies, which is said to be easy to use.
In actual use, the Connection pool is generally used, and a Connection is not used for the entire project. Jedis pool is implemented based on apache common pool, so the main project imports the corresponding commons-pool2 package, the Code is as follows:
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 = null;/*** create pool */private static JedisPool createJedisPool () {if (pool = null) {// set the connection pool Configuration Parameter JedisPoolConfig = new JedisPoolConfig (); // set the maximum number of jedis connections config. setMaxTotal (100); // sets the maximum blocking time, in milliseconds. config. setMaxWaitMillis (1000); // you can specify the maximum number of idle connections. setMaxIdle (10); // create the connection pool ip port pool = new JedisPool (config, "172.16.0.11", 6379);} return pool ;} /*** initialize synchronization in a multi-threaded environment */private static synchronized void poolInit () {if (pool = null) pool = createJedisPool ();} /*** get a jedis object ** @ return */public static Jedis getJedis () {if (pool = null) poolInit (); return pool. getResource ();}/*** return a connection */public static void returnRes (Jedis jedis) {pool. returnResource (jedis);} // main public static void main (String [] args) {Jedis jedis = JedisPoolTest. getJedis (); 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