Simple use and introduction of redis Linux (centos 5.4) redis install

Source: Internet
Author: User

Redis is a high-performance Key-value database. The emergence of redis largely compensates for the shortage of keyValue storage such as memcached, and can play a very good complementary role in relational databases. It provides python, Ruby, Erlang, PHP, and Java clients for ease of use.
Redis uses a single-thread Io multiplexing model and encapsulates a simple aeevent event processing framework, which mainly implements epoll, kqueue, and select. For Io operations alone, A single thread can maximize the speed advantage, but redis also provides some simple computing functions, such as sorting and aggregation. For these operations, the single-threaded model will seriously affect the overall throughput. During CPU computing, the entire Io scheduling is blocked.

In addition to storage, redis also provides some other functions, such as aggregate computing, pubsub, and scripting. For such functions, you need to understand their implementation principles, you can use pubsub correctly only after you understand its limitations. For example, the pubsub function does not support persistence, all messages sent from the consumer's transient disconnection or reconnection will be lost. For example, functions such as aggregate computing and scripting are limited by the redis single-thread model, it is impossible to achieve a high throughput, so use it with caution.

In this example, centos5.4 is used in Linux.

The following describes how to install apsaradb for redis.

wget  http://redis.googlecode.com/files/redis-2.0.4.tar.gztar zxvf redis-2.0.4.tar.gzcd  redis-2.0.4make

After make, the compiled redis service program redis-server will appear in the redis-2.0.4 directory, as well as the client program redis-cli for testing.

Installed successfully

Start the service

./Redis-Server

You can also use the following command to tell redis to start it using the specified configuration file through the startup parameter:

./Redis-server redis. conf

Redis. conf is a default configuration file. We can use our own configuration files as needed.

After starting the redis service process, you can use the test client redis-CLI to interact with the redis service.

Note that

Warning overcommit_memory is set to 0! Background save may fail under

Low memory condition. To fix this issue add 'vm. overcommit_memory = 1' to/etc/sysctl. conf and 

[6020] 10 Aug 20:58:21 * the server is nowready to accept connections on port 6379

[6020] 10 Aug 20:58:21-0 clientsconnected (0 slaves), 533432 bytes in use

[6020] 10 Aug 20:58:30-0 clientsconnected (0 slaves), 533432 bytes in use

Also, run sysctl VM. overcommit_memory = 1.

For details about redis, visit http://www.cnblogs.com/xhan/archive/2011/02/08/1949867.html.
, Comprehensive

Next we will introduce a simple Java client Jedis. You can download it from https://github.com/xetorthio/jedis.

Here we provide you with a simple Jedis encapsulation class for your reference.

Redis. Java

Package COM. ajun. redis; import Java. util. arraylist; import Java. util. list; import Java. util. set; import Java. util. treeset; import redis. clients. jedis. jedis; import redis. clients. jedis. jedispool; import redis. clients. jedis. jedispoolconfig;/***** @ author ajun **/public class redis {Private Static jedispool pool; Private Static int dbindex = 1; Private Static string host = "192.168.1.200 "; private Static int Port = 6379; Private Static int timeout = 60*1000; static {jedispoolconfig Config = new jedispoolconfig (); config. setmaxactive (100); config. setmaxidle (20); config. setmaxwafit (long) 1000); config. settestonborrow (false); pool = new jedispool (config, host, port, timeout); // thread quantity limit, IP address, port, timeout}/*** note: * As a key value, many developers naturally use redis in the Set/get mode. In fact, this is not the optimal method. * Especially when the VM is not enabled, all redis data needs to be stored in the memory, which is especially important to save memory. Assume that a key-value unit occupies a minimum of 512 bytes, even if only one byte is saved, the unit occupies 512 bytes. At this time, there is a design mode that can reuse keys, put several key-values into a key, and store values as a set, so that 512 bytes will store 10-times of capacity. It is used to store the values of multiple key-values. For example, you can store many person objects. For example, you can store redis-CLI: redis 127.0.0.1: 6379> hset personhash personid personobject to get: redis 127.0.0.1: 6379> hget personhash personid (you can obtain the person object corresponding to the current personid) * @ Param key hashset key * @ Param field is equivalent to personid * @ Param value person object */public static void hsetitem (string key, string field, byte [] value) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. CO Nnect (); Jedis. select (dbindex); Jedis. hset (key. getbytes (), field. getbytes (), value);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static byte [] hgetitem (string key, string field) {Jedis = NULL; byte [] value = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); value = Jedis. hget (key. getbytes (), field. getbytes (); // Jedis. hgetall (key);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis);} return value ;} /*** @ Param key * @ Param value * @ Param seconds valid time in seconds. Unit: 0: permanent. */public static void setitem (string key, byte [] value, int seconds) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); If (seconds = 0) {Jedis. set (key. getbytes (), value);} else {Jedis. setex (key. getbytes (), seconds, value) ;}} catch (exception e) {e. printsta Cktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}}/*** Delete * @ Param keys */public static void del (string... keys) {Jedis = NULL; If (Keys! = NULL) {try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); Jedis. del (KEYS);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}}/ *** add element in the header * @ Param key * @ Param value */public static void lpushtolist (string key, byte [] value) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); Jedis. lpush (key. getbytes (), value);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}}/*** returns list * @ Param key * @ Param value */public static list <byte []> lrangefromlist (string key, int start, int end) {Jedis = NULL; List <byte []> List = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); List = Jedis. lrange (key. getbytes (), start, end);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis);} return list ;} /***** @ Param key * @ Param member stored value * @ Param score the sorting field is generally objecid */public static void additemtosortset (string key, byte [] member, double score) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); Jedis. zadd (key. getbytes (), score, member);} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static void addlisttosortset (string key, list <byte []> list, list <double> scores) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); If (list! = NULL &&! List. isempty () & scores! = NULL &&! Scores. isempty () & list. size () = scores. size () {for (INT I = 0; I <list. size (); I ++) {Jedis. zadd (key. getbytes (), scores. get (I), list. get (I) ;}} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static list <byte []> getfromsortset (string key, int start, int end, orderstatus) {Jedis = NULL; list <byte []> List = new arraylist <byte []> (); set <byte []> set = new treeset <byte []> (); try {Jedis = pool. getresource (); Jedis. connect (); Jedis. select (dbindex); If (orderstatus. equals (orderstatus. DESC) {set = Jedis. zrevrange (key. getbytes (), start, end);} else {se T = Jedis. zrange (key. getbytes (), start, end);} If (set! = NULL &&! Set. isempty () {for (byte [] B: Set) {list. add (B) ;}} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis);} return list;} public static byte [] getitem (string key) {Jedis = NULL; byte [] S = NULL; try {Jedis = pool. getresource (); Jedis. select (dbindex); s = Jedis. get (key. getbytes (); Return s;} catch (exception e) {e. printstacktrace (); Return s;} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static void delitem (string key) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. select (dbindex); Jedis. del (key. getbytes ();} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static long getincrement (string key) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. select (dbindex); Return Jedis. incr (key);} catch (exception e) {e. printstacktrace (); Return 0l;} finally {If (Jedis! = NULL) pool. returnresource (Jedis) ;}} public static void getkeys (string pattern) {Jedis = NULL; try {Jedis = pool. getresource (); Jedis. select (dbindex); set <string> keys = Jedis. keys (pattern); For (string B: Keys) {system. out. println ("keys =>" + B) ;}} catch (exception e) {e. printstacktrace ();} finally {If (Jedis! = NULL) pool. returnresource (Jedis );}}}

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.