Simple redis instructions and Performance Testing

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/jmppok/article/details/18085181

Redis is a memory-type key-value database that provides extremely fast access and is easy to use and supports multiple languages. This article provides a brief description of its usage and a simple test.

1. Download and compile

You can download the latest source code package from the redis Official Website: http://www.redis.io/

Make compilation is simple.

2. redis installation and configuration

You do not need to install it. After redis is compiled, the redis-server is generated in the src directory. It is an executable file that starts the redis service. However, it requires a configuration file. The configuration file has been written on the Internet. An example is provided here:

daemonize yespidfile /tmp/redis/var/redis.pidport 6379timeout 300loglevel debuglogfile /tmp/redis/var/redis.logdatabases 16save 900 1save 300 10save 60 10000rdbcompression yesdbfilename dump.rdbdir /tmp/redis/var/appendonly noappendfsync always#glueoutputbuf yes#shareobjects no#shareobjectspoolsize 1024
Save it as redis. conf
Then run./redis-server redis. conf to start the redis service. Is it very convenient?

3. C/C ++ access redis

There is a deps directory under the redis source code directory, and there is a hiredis directory below. During redis compilation, the directory will be automatically compiled to generate libhiredis. a. By referencing hiredis. h and libhiredis. a, you can access redis. The procedure is as follows:

1) create a redisContext

2) run the command through redisContext

3) obtain the required data from the returned redisReply.

The Code is as follows:

RedisContext * c = redisConnect (char *) "192.168.150.135", 6379); const char * pData = "this is a test ";
RedisReply * reply1 = (redisReply *) redisCommand (c, "SET 100% s", pData );
freeReplyObject(reply1);


redisReply *reply2 = (redisReply *)redisCommand(c,"GET 100");
Printf ("% s \ n", reply2-> str );
FreeReplyObject (reply2 );
Is it very simple?

However, it should be noted that redis accepts strings and binary data can be encoded using base64. For details, refer to my other article.

4. Access redis using Java

Redis supports multiple languages, and of course Java.

First, you need to download the redis java package. Jedis. jar. Here we provide a: redis Java client jedis

Use:

Jedis jedis = new Jedis("192.168.150.135");jedis.set("100","this is a test");String data = jedis.get("100");


5. Performance Testing

Test method: Write 1 m data to redis for 10 times and 10 times respectively, and calculate the time consumption. It is tested in C ++ and Java versions.

C ++ test code

#include <stdio.h>#include "hiredis.h"#include <string.h>#include <time.h>int main(int argc, char **argv){printf("CLOCKS_PER_SEC:%d\n",CLOCKS_PER_SEC);redisContext *c;    redisReply *reply;    c = redisConnect((char *)"one-60",6379);char * pData;        reply = (redisReply *)redisCommand(c,"GET 0");int size = strlen(reply->str);pData = new char[size+1];strcpy(pData,reply->str);    freeReplyObject(reply);clock_t start, finish;start = clock();for(int i=0;i<10; i++){reply = (redisReply *)redisCommand(c,"GET %d",i);freeReplyObject(reply);}finish = clock();double duration = (double)(finish - start) / CLOCKS_PER_SEC*1000;printf("GET Time used:%f ms.\n",duration);start = clock();for(int i=0;i<10; i++){reply = (redisReply *)redisCommand(c,"SET %d %s",i,pData);freeReplyObject(reply);}finish = clock();duration = (double)(finish - start) / CLOCKS_PER_SEC*1000;printf("SET Time used:%f ms.\n",duration);delete []pData;redisFree(c);}
Test Results

CLOCKS_PER_SEC:1000000GET Time used:190.000000 ms.SET Time used:70.000000 ms.


Java test code

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Date;import redis.clients.jedis.Jedis;public class JedisTest {public static void main(String[] args){Jedis jedis = new Jedis("10.100.211.232");String f = "/tmp/e2.txt.backup";try{File file = new File(f);BufferedReader reader = new BufferedReader(new FileReader(file));String data = reader.readLine();reader.close();Date start = new Date();for(int i=0; i<10; i++){jedis.set(i+"", data);}Date end = new Date();System.out.println("Set used(ms):"+(end.getTime()-start.getTime()));start = new Date();for(int i=0; i<10; i++){String v = jedis.get(i+"");}end = new Date();System.out.println("Get used(ms):"+(end.getTime()-start.getTime()));}catch (Exception e){e.printStackTrace();}jedis.disconnect();}}

Test Results

Set used(ms):1212Get used(ms):1437

6. Summary

Redis is still very efficient. It takes about 10 ms to read and write 1 MB of data.


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.