forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OpenAtomFoundation#234 from chejinge/feature/cache…
…_string redis cache:add dependences
- Loading branch information
Showing
13 changed files
with
2,170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.