第七弹——超高效率的数据库redis

来源:互联网 发布:中控矩阵 编辑:程序博客网 时间:2024/06/05 19:45

提到数据库,大家想到的一般都是关系型数据库,像Oracle、SQLServer以及上篇讲的mysql啦都是关系型数据库,因为它们的使用量非常大,但是这篇介绍的redis就不算是关系型数据库。


redis数据库:

redis数据库是啥呢,百科上是这样介绍的:

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。

长时间贮存在内存上,可以想象出它的速度有多快。

redis有五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

string类型是二进制安全的,意思是可以存任何的东西,因为计算机上所有的东西都是以二进制形势存在的,不过它每个键最大只能存512MB,这想必是足够用的…………
Redis hash 是一个键名对集合。Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。每个 hash 可以存储 232 -1 键值对(40多亿),我想用它存用户表,登录岂不超级快。

列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。列表最多可存储 232 - 1 元素 (4294967295, 每个列表可存储40多亿)。

Set是string类型的无序集合。笔者没怎么用过……

zset是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。


用C/C++连接:

笔者使用的是阿里云的运redis数据库,速度飞快,下面粘一段阿里云给出的样例代码,不过我根据我的需求改了一下。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis.h>int main(int argc, char **argv) {     unsigned int j;     redisContext *c;     redisReply *reply;     const char *hostname = "你的云服务器地址";     const int port = 6379;     const char *instance_id = "账号";     const char *password = "密码";     struct timeval timeout = { 1, 500000 }; // 1.5 seconds     c = redisConnectWithTimeout(hostname, port, timeout);     if (c == NULL || c->err) {          if (c)           {               printf("Connection error: %s\n", c->errstr);               redisFree(c);          }           else           {               printf("Connection error: can't allocate redis context\n");          }          exit(1);     }     /* AUTH */     reply = redisCommand(c, "AUTH %s", password);     printf("AUTH: %s\n", reply->str);     freeReplyObject(reply);     /* PING server */     reply = redisCommand(c,"PING");     printf("PING: %s\n", reply->str);     freeReplyObject(reply);     /* Set a key */     reply = redisCommand(c,"SET %s %s", "foo", "hello world");     printf("SET: %s\n", reply->str);     freeReplyObject(reply);     /* Set a key using binary safe API */     reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);     printf("SET (binary API): %s\n", reply->str);     freeReplyObject(reply);     /* Try a GET and two INCR */     reply = redisCommand(c,"GET foo");     printf("GET foo: %s\n", reply->str);     freeReplyObject(reply);     reply = redisCommand(c,"INCR counter");     printf("INCR counter: %lld\n", reply->integer);     freeReplyObject(reply);     /* again ... */     reply = redisCommand(c,"INCR counter");     printf("INCR counter: %lld\n", reply->integer);     freeReplyObject(reply);     /* Create a list of numbers, from 0 to 9 */     reply = redisCommand(c,"DEL mylist");     freeReplyObject(reply);     for (j = 0; j < 10; j++)      {          char buf[64];          snprintf(buf,64,"%d",j);          reply = redisCommand(c,"LPUSH mylist element-%s", buf);          freeReplyObject(reply);     }     /* Let's check what we have inside the list */     reply = redisCommand(c,"LRANGE mylist 0 -1");     if (reply->type == REDIS_REPLY_ARRAY)      {          for (j = 0; j < reply->elements; j++)           {               printf("%u) %s\n", j, reply->element[j]->str);          }     }     freeReplyObject(reply);     /* Disconnects and frees the context */     redisFree(c);     return 0;}

编译的时候要注意

gcc -o test -g test.c -I /usr/local/include/hiredis -lhiredis

不过会出现hiredis.h不存在的错误,你只能自己在网上下载一个用啦。
编译成功后

./test

就能在云redis上写入数据啦。

阅读全文
0 0