Skip to content

Commit

Permalink
Add redis support
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Mar 29, 2024
1 parent 8e4ab84 commit c30dc01
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
166 changes: 166 additions & 0 deletions moxt/libredis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include "libredis.hpp"
#include <sw/redis++/redis++.h>

using namespace sw::redis;

typedef Redis *RedisPtr;

SEQ_FUNC void seq_test_redis() {
auto password = getenv("REDIS_PASSWORD");

ConnectionOptions connection_options;
connection_options.host = "1.94.26.93";
connection_options.port = 6379;
connection_options.password = password;
connection_options.db = 0;

// Optional. Timeout before we successfully send request to or receive
// response from redis. By default, the timeout is 0ms, i.e. never timeout
// and block until we send or receive successfuly. NOTE: if any command is
// timed out, we throw a TimeoutError exception.
connection_options.socket_timeout = std::chrono::milliseconds(3 * 1000);

// Connect to Redis server with a single connection.
Redis redis(connection_options);
// redis.rpush("", "");
redis.set("test", "test123");
auto value = redis.get("test");
if (value) {
printf("value=%s\n", value.value().c_str());
} else {
printf("value is null\n");
}
}

SEQ_FUNC RedisPtr seq_redis_new(const char *host, size_t hostLen, int port,
const char *password, size_t passwordLen,
int db, int timeout_ms) {
auto hostStr = std::string(host, hostLen);
auto pwdStr = std::string(password, passwordLen);

ConnectionOptions connection_options;
connection_options.host = hostStr;
connection_options.port = port;
connection_options.password = pwdStr;
connection_options.db = db;

// Optional. Timeout before we successfully send request to or receive
// response from redis. By default, the timeout is 0ms, i.e. never timeout
// and block until we send or receive successfuly. NOTE: if any command is
// timed out, we throw a TimeoutError exception.
connection_options.socket_timeout = std::chrono::milliseconds(timeout_ms);

return new Redis(connection_options);
}

SEQ_FUNC bool seq_redis_set(RedisPtr redis, const char *key, size_t keyLen,
const char *value, size_t valueLen) {
return redis->set(std::string(key, keyLen), std::string(value, valueLen));
}

SEQ_FUNC bool seq_redis_get(RedisPtr redis, const char *key, size_t keyLen,
char *value, size_t *valueLen) {
auto result = redis->get(std::string(key, keyLen));
if (result) {
*valueLen = result->size();
memcpy(value, result->data(), *valueLen);
return true;
}
return false;
}

SEQ_FUNC bool seq_redis_expire(RedisPtr redis, const char *key, size_t keyLen,
int expireTime) {
return redis->expire(std::string(key, keyLen), expireTime);
}

SEQ_FUNC int64_t seq_redis_rpush(RedisPtr redis, const char *key, size_t keyLen,
const char *value, size_t valueLen) {
return redis->rpush(std::string(key, keyLen), std::string(value, valueLen));
}

SEQ_FUNC size_t seq_redis_lrange(RedisPtr redis, const char *key, size_t keyLen,
char *values[], size_t *valuesLen,
size_t maxLen) {
std::vector<OptionalString> result;
redis->lrange(std::string(key, keyLen), 0, -1, std::back_inserter(result));
size_t count = 0;
for (const auto &val : result) {
if (count >= maxLen)
break;
if (val) {
values[count] = new char[val->size() + 1];
memcpy(values[count], val->data(), val->size());
values[count][val->size()] = '\0';
valuesLen[count] = val->size();
count++;
}
}
return count;
}

SEQ_FUNC int64_t seq_redis_hset(RedisPtr redis, const char *key, size_t keyLen,
const char *field, size_t fieldLen,
const char *value, size_t valueLen) {
return redis->hset(std::string(key, keyLen), std::string(field, fieldLen),
std::string(value, valueLen));
}

SEQ_FUNC size_t seq_redis_hgetall(RedisPtr redis, const char *key,
size_t keyLen, char *fields[],
size_t *fieldsLen, char *values[],
size_t *valuesLen, size_t maxLen) {
std::unordered_map<std::string, std::string> result;
redis->hgetall(std::string(key, keyLen),
std::inserter(result, result.begin()));
size_t count = 0;
for (const auto &pair : result) {
if (count >= maxLen)
break;
fields[count] = new char[pair.first.size() + 1];
memcpy(fields[count], pair.first.data(), pair.first.size());
fields[count][pair.first.size()] = '\0';
fieldsLen[count] = pair.first.size();

values[count] = new char[pair.second.size() + 1];
memcpy(values[count], pair.second.data(), pair.second.size());
values[count][pair.second.size()] = '\0';
valuesLen[count] = pair.second.size();
count++;
}
return count;
}

SEQ_FUNC int64_t seq_redis_sadd(RedisPtr redis, const char *key, size_t keyLen,
const char *member, size_t memberLen) {
return redis->sadd(std::string(key, keyLen),
std::string(member, memberLen));
}

SEQ_FUNC size_t seq_redis_smembers(RedisPtr redis, const char *key,
size_t keyLen, char *members[],
size_t *membersLen, size_t maxLen) {
std::unordered_set<std::string> result;
redis->smembers(std::string(key, keyLen),
std::inserter(result, result.begin()));
size_t count = 0;
for (const auto &member : result) {
if (count >= maxLen)
break;
members[count] = new char[member.size() + 1];
memcpy(members[count], member.data(), member.size());
members[count][member.size()] = '\0';
membersLen[count] = member.size();
count++;
}
return count;
}

SEQ_FUNC int64_t seq_redis_zadd(RedisPtr redis, const char *key, size_t keyLen,
const char *member, size_t memberLen,
double score) {
return redis->zadd(std::string(key, keyLen), std::string(member, memberLen),
score);
}

SEQ_FUNC void seq_redis_free(RedisPtr redis) { delete redis; }
6 changes: 6 additions & 0 deletions moxt/libredis.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MOXT_LIBREDIS_HPP
#define MOXT_LIBREDIS_HPP

#include "common.hpp"

#endif
2 changes: 2 additions & 0 deletions moxt/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ target("moxt")
add_packages("parallel-hashmap")
add_packages("yyjson")
add_packages("nanobench")
add_packages("redis-plus-plus")

if not is_plat("windows") then
-- add_ldflags("-latomic") -- for snmalloc
Expand Down Expand Up @@ -140,4 +141,5 @@ target("moxt_sole")
add_packages("parallel-hashmap")
add_packages("yyjson")
add_packages("nanobench")
add_packages("redis-plus-plus")
add_packages("tulipindicators")
3 changes: 2 additions & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set_project("moxt")
set_version("1.0.0")
set_xmakever("2.8.5")
set_xmakever("2.8.9")

set_encodings("utf-8")
set_languages("c++20") -- c++20/c++23
Expand Down Expand Up @@ -59,6 +59,7 @@ add_requires("yyjson-local", {alias = 'yyjson'})
add_requires("sonic-cpp-local", {alias = 'sonic-cpp'})
add_requires("toml++")
add_requires("uwebsockets")
add_requires("redis-plus-plus")

target("tulipindicators")
set_kind("static")
Expand Down

0 comments on commit c30dc01

Please sign in to comment.