The redis module in Acl is a powerful redis client library with higth performance, rich interface and easy to use. There are more than 13 C++ classes and over 150 commands in Acl redis, including STRING, HASH, LIST, SET, ZSET, HyperLogLog, PUBSUB, STREAM, TRANSACTION, SCRIPT, CONNECTION, SERVER, etc. User using Acl redis doesn't need care about network comminucation, redis protocol, hash slots caching, etc., just like using C++ STL standard interface.
Acl redis is a part of lib_acl_cpp, so users only need to build the Acl project.
Users can use VS2003/VS2008/VS2010/VS2012/VS2015/VS2017/VS1019
to compile all Acl libraries by opening the Acl projects(acl_cpp_vc2003.sln, acl_cpp_vc2008.sln, acl_cpp_vc2010.sln, acl_cpp_vc2012.sln, acl_cpp_vc2015.sln, acl_cpp_vc2017.sln, acl_cpp_vc2019.sln) with the corresponding VS tools. Due to the dependency in Acl, lib_acl should be compiled first, then lib_protocol, and finally lib_acl_cpp.
#include <stdlib.h>
#include <stdio.h>
#include "acl_cpp/lib_acl.hpp"
static void test_redis_string(acl::redis& cmd, const char* key) {
acl::string val("test_value");
// call redis-server: SET key value
if (!cmd.set(key, val.c_str())) {
printf("redis set error\r\n");
return;
}
// clear the string buf space
val.clear();
// reset the redis command object for reusing it
cmd.clear();
// call redis-server: GET key
if (!cmd.get(key, val)) {
printf("get key error\r\n");
}
}
static void test_redis_key(acl::redis& cmd, const char* key) {
if (cmd.exists(key)) {
printf("key exists\r\n");
} else {
printf("key not exists\r\n");
}
}
int main(void) {
// init socket module for windows
acl::acl_cpp_init();
const char* redis_addr = "127.0.0.1:6379";
int conn_timeout = 10, rw_timeout = 10;
// the redis client connection
acl::redis_client conn(redis_addr, conn_timeout, rw_timeout);
const char* key = "test_key";
// Bind redis_string command with redis connection.
acl::redis cmd(&conn);
test_redis_string(cmd, key);
cmd.clear(); // Clear the temp memory.
// Test redis KEY command with the same redis connection.
test_redis_key(cmd, key);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include "acl_cpp/lib_acl.hpp"
int main(void) {
// Init socket module for windows
acl::acl_cpp_init();
const char* redis_addr = "127.0.0.1:6379";
int conn_timeout = 10, rw_timeout = 10, max_conns = 100;
// Declare redis connection cluster ojbect.
acl::redis_client_cluster cluster;
cluster.set(redis_addr, max_conns, conn_timeout, rw_timeout);
// Redis operation command.
acl::redis cmd;
// Bind redis command with redis connection cluster.
cmd.set_cluster(&cluster);
const char* key = "test_key";
// Call redis server
test_redis_string(cmd, key);
cmd.clear();
test_redis_key(cmd, key);
return 0;
}
The redis cluster module of Acl supports caching the redis hash slots on client to improve performance, and can dynamically change local hash slots at runtime.
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "acl_cpp/lib_acl.hpp"
static int __max_conns = 100;
static void* thread_main(void* arg) {
acl::redis_client_cluster* cluster = (acl::redis_client_cluster*) arg;
acl::redis cmd;
cmd.set_cluster(cluster);
const char* key = "test_key";
for (int i = 0; i < 100000; i++) {
test_redis_string(cmd, key);
test_redis_key(cmd, key);
cmd.clear(); // Clear temporary meory to avoid meory overflow.
}
return NULL;
}
int main(void) {
// init socket module for windows
acl::acl_cpp_init();
const char* redis_addr = "127.0.0.1:6379";
int conn_timeout = 10, rw_timeout = 10;
// declare redis cluster ojbect
acl::redis_client_cluster cluster;
cluster.set(redis_addr, __max_conns, conn_timeout, rw_timeout);
pthread_attr_t attr;
pthread_attr_init(&attr);
// create first thread
pthread_t id1;
pthread_create(&id1, &attr, thread_main, &cluster);
// create second thread
pthread_t id2;
pthread_create(&id2, &attr, thread_main, &cluster);
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "acl_cpp/lib_acl.hpp"
static void* thread_main(void* arg) {
acl::redis_client_pipeline* pipeline = (acl::redis_client_pipeline*) arg;
acl::redis cmd;
cmd.set_pipeline(pipeline);
acl::string key;
for (int i = 0; i < 100000; i++) {
key.format("test-key-%d", i);
test_redis_string(cmd, key);
test_redis_key(cmd, key);
}
return NULL;
}
int main(void) {
// Init socket module for windows
acl::acl_cpp_init();
const char* redis_addr = "127.0.0.1:6379";
// Declare redis pipeline ojbect
acl::redis_client_pipeline pipeline(redis_addr);
// Start the pipeline thread backend.
pipeline.start_thread();
pthread_attr_t attr;
pthread_attr_init(&attr);
std::vector<pthread_t> threads;
// start some threads to execute redis operations
for (size_t i = 0; i < 100; i++) {
pthread_t id;
int ret = pthread_create(&id, &attr, thread_main, &pipeline);
if (ret == 0) {
threads.push_back(id);
}
}
// wait for all threads to exit
for (std::vector<pthread_t>::iterator it = threads.begin();
it != threads.end(); ++it) {
pthread_join(*it, NULL);
}
// stop the pipeline thread
pipeline.stop_thread();
return 0;
}
Before using Acl redis, you should compile the three basic libraries. Enter the lib_acl, lib_protocol, lib_acl_cpp, and build the lib_acl.a
, lib_protocol.a
and libacl_cpp.a
.
$cd lib_acl; make
$cd lib_protocol; make
$cd lib_acl_cpp; make
You should add the following compilation options in your Makefile:
-I
specify the lib_acl.hpp's parent path, for exmaple: -I./lib_acl_cpp/include
;One Makefile as below:
ACL_PATH=./acl
CFLAGS = -c -g -W -O3 -Wall -Werror -Wshadow \
-Wno-long-long -Wpointer-arith -D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS \
-I $(ACL_PATH)/lib_acl_cpp/include
LDFLAGS = -L$(ACL_PATH)/lib_acl_cpp/lib -lacl_cpp \
-L$(ACL_PATH)/lib_protocol/lib -lprotocol \
-L$(ACL_PATH)/lib_acl/lib -lacl \
-lpthread
test: main.o
g++ -o main.o $(LDFLAGS)
main.o: main.cpp
g++ $(CFLAGS) main.cpp -o main.o
Open the VS projects, such as acl_cpp_vc2003.sln, acl_cpp_vc2008.sln, acl_cpp_vc2010.sln, acl_cpp_vc2012.sln, acl_cpp_vc2013.sln, acl_cpp_vc2015.sln, acl_cpp_vc2017.sln, or acl_cpp_vc2019.sln to look at the redis samples project option setting.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )