Skip to content

Commit

Permalink
Merge pull request OpenAtomFoundation#234 from chejinge/feature/cache…
Browse files Browse the repository at this point in the history
…_string

redis cache:add dependences
  • Loading branch information
AlexStocks authored Oct 23, 2023
2 parents 924df2c + 43a1f89 commit 4ddc9a7
Show file tree
Hide file tree
Showing 13 changed files with 2,170 additions and 1 deletion.
37 changes: 36 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,35 @@ ExternalProject_Add(rocksdb
make -j${CPU_CORE}
)

ExternalProject_Add(rediscache
URL
https:/pikiwidb/rediscache/archive/refs/tags/v1.0.2.tar.gz
URL_HASH
MD5=0d72a2d4832500773bd47d3efb23bca8
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
""
LOG_BUILD
1
BUILD_IN_SOURCE
1
SOURCE_SUBDIR
""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_INSTALL_INCLUDEDIR=${INSTALL_INCLUDEDIR}
-DCMAKE_INSTALL_LIBDIR=${INSTALL_LIBDIR}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
BUILD_ALWAYS
1
BUILD_COMMAND
make -j${CPU_CORE}
)
set(REDISCACHE_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
set(REDISCACHE_LIBRARY ${INSTALL_LIBDIR}/librediscache.a)


option(USE_PIKA_TOOLS "compile pika-tools" OFF)
if (USE_PIKA_TOOLS)
ExternalProject_Add(hiredis
Expand Down Expand Up @@ -697,6 +726,7 @@ set(ROCKSDB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${EP_BASE_SUFFIX}/Source/rock
add_subdirectory(src/pstd)
add_subdirectory(src/net)
add_subdirectory(src/storage)
add_subdirectory(src/cache)
if (USE_PIKA_TOOLS)
add_subdirectory(tools)
endif()
Expand Down Expand Up @@ -766,7 +796,10 @@ add_dependencies(${PROJECT_NAME}
protobuf
pstd
net
storage)
rediscache
storage
cache
)

target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
Expand All @@ -775,6 +808,7 @@ target_include_directories(${PROJECT_NAME}
)

target_link_libraries(${PROJECT_NAME}
cache
storage
net
pstd
Expand All @@ -787,6 +821,7 @@ target_link_libraries(${PROJECT_NAME}
libzstd.a
liblz4.a
libz.a
librediscache.a
${LIBUNWIND_LIBRARY}
${JEMALLOC_LIBRARY})

Expand Down
20 changes: 20 additions & 0 deletions src/cache/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required (VERSION 3.18)

set (CMAKE_CXX_STANDARD 17)
project (cache)

aux_source_directory(./src DIR_SRCS)
include_directories(include)
add_library(cache STATIC ${DIR_SRCS})
add_dependencies(cache net protobuf glog gflags ${LIBUNWIND_NAME})

target_link_libraries(cache
PUBLIC ${GTEST_LIBRARY}
PUBLIC pstd
PUBLIC ${ROCKSDB_LIBRARY}
PUBLIC storage
PUBLIC ${GLOG_LIBRARY}
PUBLIC ${GFLAGS_LIBRARY}
PUBLIC ${LIBUNWIND_LIBRARY}
PUBLIC ${REDISCACHE_LIBRARY}
)
176 changes: 176 additions & 0 deletions src/cache/include/RedisCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright (c) 2023-present The dory Authors. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.

#ifndef __REDIS_CACHE_H__
#define __REDIS_CACHE_H__

#include <unistd.h>
#include <string>
#include <map>
#include <list>
#include <queue>
#include <vector>

extern "C" {
#include "rediscache/redisdbIF.h"
}

#include "RedisDefine.h"
#include "pstd_status.h"
#include "storage/storage.h"

namespace cache {

using Status = rocksdb::Status;

class RedisCache {
public:
RedisCache();
~RedisCache();

// Server APIs
static void SetConfig(CacheConfig *cfg);
static uint64_t GetUsedMemory(void);
static void GetHitAndMissNum(long long *hits, long long *misses);
static void ResetHitAndMissNum(void);
Status Open(void);
int ActiveExpireCycle(void);

// Normal Commands
bool Exists(std::string &key);
long long DbSize(void);
void FlushDb(void);

Status Del(const std::string &key);
Status Expire(std::string &key, int64_t ttl);
Status Expireat(std::string &key, int64_t ttl);
Status TTL(std::string &key, int64_t *ttl);
Status Persist(std::string &key);
Status Type(std::string &key, std::string *value);
Status RandomKey(std::string *key);

// String Commands
Status Set(std::string &key, std::string &value, int64_t ttl);
Status SetWithoutTTL(std::string &key, std::string &value);
Status Setnx(std::string &key, std::string &value, int64_t ttl);
Status SetnxWithoutTTL(std::string &key, std::string &value);
Status Setxx(std::string &key, std::string &value, int64_t ttl);
Status SetxxWithoutTTL(std::string &key, std::string &value);
Status Get(const std::string &key, std::string *value);
Status Incr(std::string &key);
Status Decr(std::string &key);
Status IncrBy(std::string &key, long long incr);
Status DecrBy(std::string &key, long long incr);
Status Incrbyfloat(std::string &key, long double incr);
Status Append(std::string &key, std::string &value);
Status GetRange(std::string &key, int64_t start, int64_t end, std::string *value);
Status SetRange(std::string &key, int64_t start, std::string &value);
Status Strlen(std::string &key, int32_t *len);

// Hash Commands
Status HDel(std::string& key, std::vector<std::string> &fields);
Status HSet(std::string &key, std::string &field, std::string &value);
Status HSetnx(std::string &key, std::string &field, std::string &value);
Status HMSet(std::string &key, std::vector<storage::FieldValue> &fvs);
Status HGet(std::string &key, std::string &field, std::string *value);
Status HMGet(std::string &key,
std::vector<std::string> &fields,
std::vector<storage::ValueStatus>* vss);
Status HGetall(std::string &key, std::vector<storage::FieldValue> *fvs);
Status HKeys(std::string &key, std::vector<std::string> *fields);
Status HVals(std::string &key, std::vector<std::string> *values);
Status HExists(std::string &key, std::string &field);
Status HIncrby(std::string &key, std::string &field, int64_t value);
Status HIncrbyfloat(std::string &key, std::string &field, long double value);
Status HLen(std::string &key, unsigned long *len);
Status HStrlen(std::string &key, std::string &field, unsigned long *len);

// List Commands
Status LIndex(std::string &key, long index, std::string *element);
Status LInsert(std::string &key, storage::BeforeOrAfter &before_or_after,
std::string &pivot, std::string &value);
Status LLen(std::string &key, unsigned long *len);
Status LPop(std::string &key, std::string *element);
Status LPush(std::string &key, std::vector<std::string> &values);
Status LPushx(std::string &key, std::vector<std::string> &values);
Status LRange(std::string &key, long start, long stop, std::vector<std::string> *values);
Status LRem(std::string &key, long count, std::string &value);
Status LSet(std::string &key, long index, std::string &value);
Status LTrim(std::string &key, long start, long stop);
Status RPop(std::string &key, std::string *element);
Status RPush(std::string &key, std::vector<std::string> &values);
Status RPushx(std::string &key, std::vector<std::string> &values);

// Set Commands
Status SAdd(std::string &key, std::vector<std::string> &members);
Status SCard(std::string &key, unsigned long *len);
Status SIsmember(std::string &key, std::string &member);
Status SMembers(std::string &key, std::vector<std::string> *members);
Status SRem(std::string &key, std::vector<std::string> &members);
Status SRandmember(std::string &key, long count, std::vector<std::string> *members);

// Zset Commands
Status ZAdd(std::string &key, std::vector<storage::ScoreMember> &score_members);
Status ZCard(std::string &key, unsigned long *len);
Status ZCount(std::string &key, std::string &min, std::string &max, unsigned long *len);
Status ZIncrby(std::string &key, std::string &member, double increment);
Status ZRange(std::string &key,
long start, long stop,
std::vector<storage::ScoreMember> *score_members);
Status ZRangebyscore(std::string &key,
std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members,
int64_t offset = 0, int64_t count = -1);
Status ZRank(std::string &key, std::string &member, long *rank);
Status ZRem(std::string &key, std::vector<std::string> &members);
Status ZRemrangebyrank(std::string &key, std::string &min, std::string &max);
Status ZRemrangebyscore(std::string &key, std::string &min, std::string &max);
Status ZRevrange(std::string &key,
long start, long stop,
std::vector<storage::ScoreMember> *score_members);
Status ZRevrangebyscore(std::string &key,
std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members,
int64_t offset = 0, int64_t count = -1);
Status ZRevrangebylex(std::string &key,
std::string &min, std::string &max,
std::vector<std::string> *members);
Status ZRevrank(std::string &key, std::string &member, long *rank);
Status ZScore(std::string &key, std::string &member, double *score);
Status ZRangebylex(std::string &key,
std::string &min, std::string &max,
std::vector<std::string> *members);
Status ZLexcount(std::string &key, std::string &min, std::string &max, unsigned long *len);
Status ZRemrangebylex(std::string &key, std::string &min, std::string &max);

// Bit Commands
Status SetBit(std::string &key, size_t offset, long value);
Status GetBit(std::string &key, size_t offset, long *value);
Status BitCount(std::string &key, long start, long end, long *value, bool have_offset);
Status BitPos(std::string &key, long bit, long *value);
Status BitPos(std::string &key, long bit, long start, long *value);
Status BitPos(std::string &key, long bit, long start, long end, long *value);

protected:
void DecrObjectsRefCount(robj *argv1, robj *argv2 = nullptr, robj *argv3 = nullptr);
void FreeSdsList(sds *items, unsigned int size);
void FreeObjectList(robj **items, unsigned int size);
void FreeHitemList(hitem *items, unsigned int size);
void FreeZitemList(zitem *items, unsigned int size);
void ConvertObjectToString(robj *obj, std::string *value);

private:
RedisCache(const RedisCache&);
RedisCache& operator=(const RedisCache&);

private:
redisDbIF *m_RedisDB;
};

} // namespace cache

#endif

/* EOF */
66 changes: 66 additions & 0 deletions src/cache/include/RedisDefine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2023-present The dory Authors. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.

#ifndef __REDIS_DEFINE_H__
#define __REDIS_DEFINE_H__

#include "rediscache/commondef.h"

namespace cache {

/* Redis maxmemory strategies */
#define CACHE_VOLATILE_LRU 0
#define CACHE_ALLKEYS_LRU 1
#define CACHE_VOLATILE_LFU 2
#define CACHE_ALLKEYS_LFU 3
#define CACHE_VOLATILE_RANDOM 4
#define CACHE_ALLKEYS_RANDOM 5
#define CACHE_VOLATILE_TTL 6
#define CACHE_NO_EVICTION 7

#define CACHE_DEFAULT_MAXMEMORY CONFIG_DEFAULT_MAXMEMORY // 10G
#define CACHE_DEFAULT_MAXMEMORY_SAMPLES 5
#define CACHE_DEFAULT_LFU_DECAY_TIME 1

/*
* cache start pos
*/
constexpr int CACHE_START_FROM_BEGIN = 0;
constexpr int CACHE_START_FROM_END = -1;
/*
* cache items per key
*/
#define DEFAULT_CACHE_ITEMS_PER_KEY 512

struct CacheConfig {
unsigned long long maxmemory; /* Can used max memory */
int maxmemory_policy; /* Policy for key eviction */
int maxmemory_samples; /* Pricision of random sampling */
int lfu_decay_time; /* LFU counter decay factor. */
int cache_start_pos;
int cache_items_per_key;

CacheConfig()
: maxmemory(CACHE_DEFAULT_MAXMEMORY)
, maxmemory_policy(CACHE_NO_EVICTION)
, maxmemory_samples(CACHE_DEFAULT_MAXMEMORY_SAMPLES)
, lfu_decay_time(CACHE_DEFAULT_LFU_DECAY_TIME)
, cache_start_pos(CACHE_START_FROM_BEGIN)
, cache_items_per_key(DEFAULT_CACHE_ITEMS_PER_KEY){}

CacheConfig& operator=(const CacheConfig& obj) {
maxmemory = obj.maxmemory;
maxmemory_policy = obj.maxmemory_policy;
maxmemory_samples = obj.maxmemory_samples;
lfu_decay_time = obj.lfu_decay_time;
cache_start_pos = obj.cache_start_pos;
cache_items_per_key = obj.cache_items_per_key;
return *this;
}
};

} // namespace cache

#endif
15 changes: 15 additions & 0 deletions src/cache/include/build_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023-present The dory Authors. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.

#ifndef INCLUDE_DORY_BUILD_VERSION_H_
#define INCLUDE_DORY_BUILD_VERSION_H_

// this variable tells us about the git revision
extern const char* dory_build_git_sha;

// Date on which the code was compiled:
extern const char* dory_build_compile_date;

#endif // INCLUDE_REDISCACHE_BUILD_VERSION_H_
Loading

0 comments on commit 4ddc9a7

Please sign in to comment.