Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
*.exe
*.out
*.app

.vscode/
build/
90 changes: 90 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
cmake_minimum_required (VERSION 3.15)
project(xredis CXX)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
endif()

find_package(fmt CONFIG REQUIRED)
find_package(hiredis)

if (NOT TARGET hiredis)
message("Dependency 'hiredis' is not within CMake package sources, attempting to add it manually")
find_path(hiredis_INCLUDE_DIR hiredis/hiredis.h)
if (hiredis_INCLUDE_DIR)
find_library(hiredis_LIBRARY hiredis)
add_library(hiredis STATIC IMPORTED)
set_target_properties(hiredis PROPERTIES
IMPORTED_LOCATION ${hiredis_LIBRARY}
)
target_include_directories(
hiredis
INTERFACE ${hiredis_INCLUDE_DIR}
)
message("Successfully found dependency 'hiredis' on ${hiredis_LIBRARY}")
else()
message(FATAL_ERROR "Dependency 'hiredis' not found, aborting")
endif()
endif()


file(GLOB xredis_SOURCES
${xredis_SOURCE_DIR}/src/*.cpp
)

# Static
add_library (xredis-static ${xredis_SOURCES})

target_include_directories(
xredis-static
PUBLIC ${xredis_SOURCE_DIR}/include
PRIVATE ${xredis_SOURCE_DIR}/include/xredis
)

target_link_libraries(
xredis-static
PRIVATE hiredis fmt::fmt fmt::fmt-header-only
)

# Shared
add_library (xredis-shared ${xredis_SOURCES})

target_include_directories(
xredis-shared
PUBLIC ${xredis_SOURCE_DIR}/include
PRIVATE ${xredis_SOURCE_DIR}/include/xredis
)

target_link_libraries(
xredis-shared
PRIVATE hiredis fmt::fmt
)

if (MSVC)
target_link_libraries(
xredis-static
PUBLIC ws2_32
)

target_link_libraries(
xredis-shared
PUBLIC ws2_32
)
endif()

add_library(xredis::static ALIAS xredis-static)
add_library(xredis::shared ALIAS xredis-shared)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_executable(xredis_test EXCLUDE_FROM_ALL test/xredis-test.cpp)

target_link_libraries(
xredis_test
PRIVATE xredis::static hiredis
)

add_test(
NAME xredis_test
COMMAND $<TARGET_FILE:xredis_test>
)
endif()
58 changes: 34 additions & 24 deletions examples/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,70 @@
using namespace xrc;

// AP Hash Function
unsigned int APHash(const char *str) {
unsigned int APHash(const char *str)
{
unsigned int hash = 0;
int i;
for (i=0; *str; i++) {
if ((i& 1) == 0) {
for (i = 0; *str; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
} else {
}
else
{
hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
}
}
return (hash& 0x7FFFFFFF);
return (hash & 0x7FFFFFFF);
}

enum {
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
enum
{
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
};

/* 配置单个redis存储节点 */
RedisNode RedisList1[1]=
{
{0,"127.0.0.1", 7000, "", 8, 5, 0}
};
RedisNode RedisList1[1] =
{
{0, "127.0.0.1", 7000, "", 8, 5, 0}};

int main(int argc, char **argv)
{
(void)argc;
(void)argv;

int main(int argc, char **argv) {
(void)argc;(void)argv;

xRedisClient xRedis;
xRedis.Init(CACHE_TYPE_MAX);
xRedis.ConnectRedisCache(RedisList1, sizeof(RedisList1) / sizeof(RedisNode), 1, CACHE_TYPE_1);

const char *key = "test";
const char *value = "test value";
RedisDBIdx dbi(&xRedis);
dbi.CreateDBIndex(key, APHash, CACHE_TYPE_1);

bool bRet = xRedis.set(dbi, key, value);
if(bRet){
bool bRet = xRedis.set(dbi, key, value);
if (bRet)
{
printf("success \r\n");
} else {
}
else
{
printf("error [%s] \r\n", dbi.GetErrInfo());
}

std::string strValue;
bRet = xRedis.get(dbi, key, strValue);
if (bRet) {
if (bRet)
{
printf("%s \r\n", strValue.c_str());
} else {
}
else
{
printf("error [%s] \r\n", dbi.GetErrInfo());
}

return 0;
}


66 changes: 39 additions & 27 deletions examples/demo_cluster.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

/** \example demo_cluster.cpp
/** \example demo_cluster.cpp
* This is an example of how to use the xRedis.
* <br>Connect to a redis cluster which contains three redis node
* <br>More details about this example.
Expand All @@ -11,36 +11,43 @@
using namespace xrc;

// AP Hash Function
unsigned int APHash(const char *str) {
unsigned int APHash(const char *str)
{
unsigned int hash = 0;
int i;
for (i=0; *str; i++) {
if ((i& 1) == 0) {
for (i = 0; *str; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
} else {
}
else
{
hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
}
}
return (hash& 0x7FFFFFFF);
return (hash & 0x7FFFFFFF);
}

enum {
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
enum
{
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
};

/* 配置一个redis分片存储集群:共三个存储主节点 */
RedisNode RedisList1[3]=
RedisNode RedisList1[3] =
{
{0, "127.0.0.1", 7000, "", 2, 5, 0},
{1, "127.0.0.1", 7000, "", 2, 5, 0},
{2, "127.0.0.1", 7000, "", 2, 5, 0}};

int main(int argc, char **argv)
{
{0,"127.0.0.1", 7000, "", 2, 5, 0},
{1,"127.0.0.1", 7000, "", 2, 5, 0},
{2,"127.0.0.1", 7000, "", 2, 5, 0}
};
(void)argc;
(void)argv;

int main(int argc, char **argv) {
(void)argc;(void)argv;

xRedisClient xRedis;
xRedis.Init(CACHE_TYPE_MAX);
xRedis.ConnectRedisCache(RedisList1, sizeof(RedisList1) / sizeof(RedisNode), 3, CACHE_TYPE_1);
Expand All @@ -50,26 +57,31 @@ int main(int argc, char **argv) {

RedisDBIdx dbi(&xRedis);
bool bRet = dbi.CreateDBIndex(key, APHash, CACHE_TYPE_1);
if (!bRet) {
if (!bRet)
{
return 0;
}

bRet = xRedis.set(dbi, key, value);
if (bRet){
printf("success \r\n");
} else {
if (bRet)
{
printf("success \r\n");
}
else
{
printf("error [%s] \r\n", dbi.GetErrInfo());
}
}

std::string strValue;
bRet = xRedis.get(dbi, key, strValue);
if (bRet) {
if (bRet)
{
printf("%s \r\n", strValue.c_str());
} else {
}
else
{
printf("error [%s] \r\n", dbi.GetErrInfo());
}

return 0;
}


Loading