Skip to content

Commit 6f6375f

Browse files
committed
增加 Redis 及其客户端
1 parent 3190b9f commit 6f6375f

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

libs/cpp.wiki

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,25 @@ Wikipedia:[https://en.wikipedia.org/wiki/MongoDB 英文]
16021602

16031603
这是近几年兴起的 NoSQL 数据库的一员。它本身是基于 C++ 和 C 开发的。
16041604

1605+
<h4>Redis</h4>
1606+
1607+
Home:[http://redis.io/]
1608+
1609+
Wikipedia:[https://zh.wikipedia.org/wiki/Redis 中文]、[https://en.wikipedia.org/wiki/Redis 英文]
1610+
1611+
诞生于2009年,是目前(2014~2015)最流行的键值存储数据库,基于 C 语言开发。
1612+
1613+
以性能高而著称,很多大型网站用到它(Twitter、GitHub、Flickr、Instagram、百度、新浪、腾讯、搜狐)
1614+
16051615
<h4>Berkeley DB(BDB)</h4>
16061616

16071617
Home:[http://www.oracle.com/us/products/database/berkeley-db/]
16081618

16091619
Wikipedia:[https://en.wikipedia.org/wiki/Berkeley_DB 英文]、[https://zh.wikipedia.org/wiki/Berkeley_DB 中文]
16101620

1611-
诞生于1994年,是一个很老牌的嵌入式(进程内)数据库,基于 C 语言开发。开发 BDB 的公司于2006年被 Oracle 收购。
1621+
诞生于1994年,是一个很老牌的嵌入式(进程内)数据库,提供“键值存储”的功能,基于 C 语言开发。
1622+
1623+
开发 BDB 的公司于2006年被 Oracle 收购。
16121624

16131625
很多开源项目用到它。甚至 MySQL 也包含了一个基于 BDB 的存储后端。
16141626

@@ -1893,7 +1905,79 @@ Docs:[http://pocoproject.org/docs/package-SQLite.SQLite.html]
18931905

18941906
POCO 前面已经介绍过。它提供了 sqlite 的封装类
18951907

1896-
=== 8.2.7 MongoDB 封装库 ===
1908+
=== 8.2.7 Redis ===
1909+
1910+
<h4>Hiredis</h4>
1911+
1912+
Home:[https://github.com/redis/hiredis]
1913+
1914+
这是 Redis 官方提供的 C 客户端,很轻量级,支持“异步 API”。
1915+
1916+
代码示例——结合 libev 进行异步调用
1917+
<source lang="cpp">
1918+
&#35;include <stdio.h>
1919+
&#35;include <stdlib.h>
1920+
&#35;include <string.h>
1921+
&#35;include <signal.h>
1922+
1923+
&#35;include <hiredis.h>
1924+
&#35;include <async.h>
1925+
&#35;include <adapters/libev.h>
1926+
1927+
void getCallback(redisAsyncContext* context, void* r, void* privdata)
1928+
{
1929+
redisReply* reply = r;
1930+
if(reply == NULL)
1931+
{
1932+
return;
1933+
}
1934+
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
1935+
redisAsyncDisconnect(context); /* Disconnect after receiv reply */
1936+
}
1937+
1938+
void connectCallback(const redisAsyncContext* context, int status)
1939+
{
1940+
if(status != REDIS_OK)
1941+
{
1942+
printf("Error: %s\n", context->errstr);
1943+
return;
1944+
}
1945+
printf("Connected...\n");
1946+
}
1947+
1948+
void disconnectCallback(const redisAsyncContext* context, int status)
1949+
{
1950+
if(status != REDIS_OK)
1951+
{
1952+
printf("Error: %s\n", context->errstr);
1953+
return;
1954+
}
1955+
printf("Disconnected...\n");
1956+
}
1957+
1958+
int main(int argc, char* argv[])
1959+
{
1960+
signal(SIGPIPE, SIG_IGN);
1961+
1962+
redisAsyncContext* context = redisAsyncConnect("127.0.0.1", 6379);
1963+
if(context->err)
1964+
{
1965+
printf("Error: %s\n", context->errstr);
1966+
return 1; /* Let *context leak for now... */
1967+
}
1968+
1969+
redisLibevAttach(EV_DEFAULT_ context);
1970+
redisAsyncSetConnectCallback(context, connectCallback);
1971+
redisAsyncSetDisconnectCallback(context, disconnectCallback);
1972+
redisAsyncCommand(context, NULL, NULL,
1973+
"SET key %b", argv[argc-1], strlen(argv[argc-1]));
1974+
redisAsyncCommand(context, getCallback, (char*)"end-1", "GET key");
1975+
ev_loop(EV_DEFAULT_ 0);
1976+
return 0;
1977+
}
1978+
</source>
1979+
1980+
=== 8.2.8 MongoDB 封装库 ===
18971981

18981982
<h4>官方的 C API</h4>
18991983

0 commit comments

Comments
 (0)