Redis Learning Notes Introduction

Source: Internet
Author: User
Tags config configuration settings redis value store
I. Introduction of REDIS:

Redis (Http://redis.io) is an open-source, high-performance key-value store (Key-value store) that is written in ANSI C. The Redis project name is an abbreviation for remote Dictionary server, but it is often referred to as a data structure server (data structureserver). The key values for Redis can include data types such as strings (strings), hashes (hashes), lists (lists), collections (sets), and ordered collections (sorted sets). For these data types, you can perform atomic operations. For example: attaching an operation to a string (append), incrementing a value in a hash, adding an element to a list, calculating the intersection of a set, a set and a difference set, and so on.

To achieve superior performance, Redis uses an in-memory (in-memory) DataSet (DataSet) approach. Depending on the scenario, you can dump the dataset to disk at intervals, persist the data, or append each action command at the end of the log.

Redis also supports master-slave replication (Master-slave replication) and features very fast non-blocking first-time synchronization (Non-blockingfirst synchronization), network disconnection, and so on. Redis also has other features, including a simple check-and-set mechanism, pub/sub, and configuration settings, so that Redis can behave more like a cache.

Redis also provides a rich client to support most of the programming languages that are prevalent at this stage. Second, Redis installation:

2.4.15 is currently the latest stable version. Download Address: http://redis.googlecode.com/files/redis-2.4.15.tar.gz

Under Linux, run the following command to install (GCC is already installed on Linux):

$ tar xzf redis-2.4.15.tar.gz
$ CD redis-2.4.15
$ make

After the REDIS-2.4.15/SRC directory, the compiled Redis service redis-server will appear, as well as the client program REDIS-CLI for testing.

Start the Redis service below:

$./redis-server

This way of starting Redis is using the default configuration. You can also tell Redis to start using the specified configuration file using the following command via the startup parameters:

$./redis-server. /redis.conf

The redis.conf in the redis-2.4.15 directory is a default configuration file. We can use our own configuration files as needed.

Once the Redis service process is started, you can use the test client program REDIS-CLI to interact with the Redis service:

$./redis-cli
Redis 127.0.0.1:6379> set foo bar
Ok
Redis 127.0.0.1:6379> get foo
"Bar"

An example of the Get and set command operation simple Type value is shown above. Foo is a key and bar is a string type of value.

To stop the Redis command:

./redis-cli-p 6379 shutdown where 6379 is the port number of the Redis. Redis Client:

There are a lot of Redis clients, C, C + +, C #, Java, PHP, Perl, Python, Ruby and so on, to support most of the popular programming languages at this stage, see Redis official website: http://redis.io/clients

The following is an example of the Redis client in Java version:

Client Jar Package Address Https://github.com/xetorthio/jedis/downloads

Package com.jd.redis.client;

import Redis.clients.jedis.Jedis;

Publicclass App {

publicstaticvoid Main (string[] args) {

Jedis JR = null;

Try {

Redis service address and port number

JR = new Jedis ("192.168.157.128", 6379);

String key = "Mkey";

Jr.set (Key, "hello,redis!");

String v = jr.get (key);

String K2 = "Count";

JR.INCR (K2);

JR.INCR (K2);

System.out.println (v);

System.out.println (Jr.get (K2));

} catch (Exception e) {

E.printstacktrace ();

}

finally {

if (jr!=null) {

Jr.disconnect ();

}

}

}

}

Jedis client supports object pooling, You can get the Jedis client object from the pool through the Jedispool.getresource method, release the Jedis object to the pool through the Jedispool.returnresource method, and we can save a lot of reconnecting redis with the object pool The recommended time for the server to connect, below is a performance comparison with the Jedis object pool without the Jedis object pool:

Test method: The method of using direct new Jedis and getting Jedis object from pool, starting 200 concurrency, having a concurrent loop 1000 times. Each concurrent thread uses a Jedis object.

The test code is as follows:

Package com.jd.redis.client;

import Java.util.concurrent.CountDownLatch;

import Redis.clients.jedis.Jedis;

import Redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

Publicclass jedispooltest {

privatestatic Jedispoolconfigconfig;//jedis Client Pool Configuration

privatestatic Jedispoolpool;//jedis Client Pool

Static {

Config =new jedispoolconfig ();

Config.setmaxactive (60000);

Config.setmaxidle (1000);

Config.setmaxwait (10000);

Config.settestonborrow (true);

Pool =new jedispool (config, "192.168.157.128", 6380);

}

/**

* Single pen Test (without pool)

* @param Count

*/

publicstaticvoid testnopool (int count) {

for (int i=0;i<count;i++) {

Jedis JR = null;

Try {

JR = new Jedis ("10.10.224.44", 6379);

Testonce (JR);

} catch (Exception e) {

E.printstacktrace ();

}

finally {

if (jr!=null) Jr.disconnect ();

}

}

}

/**

* Single pen Test (with pool)

* @param Count

*/

publicstaticvoid testwithpool (int count) {

for (int i=0;i<count;i++) {

Jedis JR = null;

Try {

JR = Pool.getresource ();

Testonce (JR);

} catch (Exception e) {

E.printstacktrace ();

}

finally {

if (jr!=null) Pool.returnresource (JR);

}

}

}

/**

* Concurrent Testing (no pool)

* @param paiallel concurrency

* @param count each concurrent loop count

*/

publicstaticvoid paialleltestnopool (int paiallel, int count) {

thread[] ts = new Thread[paiallel];

Use this object to ensure that all threads complete the main line friend exit

Countdownlatch cd = new countdownlatch (Paiallel);

long start = System.currenttimemillis ();

for (int i=0; i < paiallel; i++) {

Ts[i] = new Thread (new workernopool (CD, Count));

Ts[i].start ();

}

Try {

Cd.await ();//wait for all child threads to complete

} catch (Interruptedexception e) {

E.printstacktrace ();

}

System.out.println ("Nopool usetime:" + (System.currenttimemillis ()-start));

}

/**

* Concurrent testing (with pool)

* @param paiallel concurrency

* @param count each concurrent loop count

*/

publicstaticvoid paialleltestwithpool (int paiallel, int count) {

Use this object to ensure that all threads complete the main line friend exit

Countdownlatch cd = new countdownlatch (Paiallel);

long start = System.currenttimemillis ();

thread[] ts = new Thread[paiallel];

for (int i=0; i < paiallel; i++) {

Ts[i] = new Thread (new workerwithpool (CD, Count));

Ts[i].start ();

}

Try {

Cd.await ();//wait for all child threads to complete

} catch (Interruptedexception e) {

E.printstacktrace ();

}

System.out.println ("Pool usetime:" + (System.currenttimemillis ()-start));

Pool.destroy ();

}

privatestaticvoid Testonce (Jedis Jr) {

System.out.println (JR.INCR ("incrtest"));

}

Publicstaticclass Workernopoolimplements runnable{

Private COUNTDOWNLATCHCD;

PrivateintCount;

Public Workernopool (countdownlatch CD,int count) {

this. cd = CD;

this. Count = Count;

}

publicvoid Run () {

Try {

Testnopool (this. count);

} catch (Exception e) {

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.