Skip to content

Commit cf60090

Browse files
Mixficsolwuxianrong
andauthored
fix: Reduced ReadCache tags for some commands (OpenAtomFoundation#3034)
* Reduced ReadCache tags for some commands * Add comment section * add Conditional judgment * Some Cache interfaces were repaired to resolve data inconsistency * Format repair --------- Co-authored-by: wuxianrong <[email protected]>
1 parent 0cd1c4e commit cf60090

12 files changed

Lines changed: 66 additions & 47 deletions

File tree

src/cache/include/cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class RedisCache {
7474

7575
// Hash Commands
7676
Status HDel(std::string& key, std::vector<std::string> &fields);
77-
Status HSet(std::string& key, std::string &field, std::string &value);
77+
Status HSetIfKeyExist(std::string& key, std::string &field, std::string &value);
7878
Status HSetnx(std::string& key, std::string &field, std::string &value);
7979
Status HMSet(std::string& key, std::vector<storage::FieldValue> &fvs);
8080
Status HGet(std::string& key, std::string &field, std::string *value);

src/cache/src/bit.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Status RedisCache::SetBit(std::string& key, size_t offset, int64_t value) {
1515
return Status::Corruption("[error] Free memory faild !");
1616
}
1717

18+
if (!Exists(key)) {
19+
return Status::NotFound("key not exist");
20+
}
1821
// createObject is a function in redis, the init ref count of robj is 1
1922
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
2023
DEFER {

src/cache/src/hash.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ Status RedisCache::HDel(std::string& key, std::vector<std::string> &fields) {
3131
return Status::OK();
3232
}
3333

34-
Status RedisCache::HSet(std::string& key, std::string &field, std::string &value) {
34+
Status RedisCache::HSetIfKeyExist(std::string& key, std::string &field, std::string &value) {
3535
int res = RcFreeMemoryIfNeeded(cache_);
3636
if (C_OK != res) {
3737
return Status::Corruption("[error] Free memory faild !");
3838
}
3939

40+
if (!Exists(key)) {
41+
return Status::NotFound("key not exist");
42+
}
4043
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
4144
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
4245
robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size()));
@@ -75,6 +78,9 @@ Status RedisCache::HMSet(std::string& key, std::vector<storage::FieldValue> &fvs
7578
return Status::Corruption("[error] Free memory faild !");
7679
}
7780

81+
if (!Exists(key)) {
82+
return Status::NotFound("key not exist");
83+
}
7884
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
7985
unsigned int items_size = fvs.size() * 2;
8086
robj **items = (robj **)zcallocate(sizeof(robj *) * items_size);
@@ -141,7 +147,7 @@ Status RedisCache::HMGet(std::string& key, std::vector<std::string> &fields, std
141147
if (C_OK == items[i].status) {
142148
vss->push_back({std::string(items[i].value, sdslen(items[i].value)), rocksdb::Status::OK()});
143149
} else {
144-
vss->push_back({std::string(), rocksdb::Status::NotFound()});
150+
return Status::NotFound("field not in cache");
145151
}
146152
}
147153

src/cache/src/list.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Status RedisCache::LPush(std::string& key, std::vector<std::string> &values) {
9999
return Status::Corruption("[error] Free memory faild !");
100100
}
101101

102+
if (!Exists(key)) {
103+
return Status::NotFound("key not exist");
104+
}
102105
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
103106
robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size());
104107
for (unsigned int i = 0; i < values.size(); ++i) {
@@ -244,7 +247,9 @@ Status RedisCache::RPush(std::string& key, std::vector<std::string> &values) {
244247
if (C_OK != res) {
245248
return Status::Corruption("[error] Free memory faild !");
246249
}
247-
250+
if (!Exists(key)) {
251+
return Status::NotFound("key not exist");
252+
}
248253
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
249254
robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size());
250255
for (unsigned int i = 0; i < values.size(); ++i) {

src/cache/src/set.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Status RedisCache::SAdd(std::string& key, std::vector<std::string> &members) {
1414
return Status::Corruption("[error] Free memory faild !");
1515
}
1616

17+
if (!Exists(key)) {
18+
return Status::NotFound("key not exist");
19+
}
1720
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
1821
robj **vals = (robj **)zcallocate(sizeof(robj *) * members.size());
1922
for (unsigned int i = 0; i < members.size(); ++i) {

src/cache/src/string.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Status RedisCache::SetRange(std::string& key, int64_t start, std::string &value)
260260
return Status::Corruption("[error] Free memory faild !");
261261
}
262262

263+
if (!Exists(key)) {
264+
return Status::NotFound("key not exist");
265+
}
263266
uint64_t ret = 0;
264267
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
265268
robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size()));

src/cache/src/zset.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Status RedisCache::ZAdd(std::string& key, std::vector<storage::ScoreMember> &sco
1414
return Status::Corruption("[error] Free memory faild !");
1515
}
1616

17+
if (!Exists(key)) {
18+
return Status::NotFound("key not exist");
19+
}
1720
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
1821
unsigned int items_size = score_members.size() * 2;
1922
robj **items = (robj **)zcallocate(sizeof(robj *) * items_size);
@@ -73,6 +76,9 @@ Status RedisCache::ZIncrby(std::string& key, std::string& member, double increme
7376
return Status::Corruption("[error] Free memory faild !");
7477
}
7578

79+
if (!Exists(key)) {
80+
return Status::NotFound("key not exist");
81+
}
7682
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
7783
robj **items = (robj **)zcallocate(sizeof(robj *) * 2);
7884
items[0] = createStringObjectFromLongDouble(increment, 0);

src/pika_cache.cc

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,7 @@ Status PikaCache::GetRange(std::string& key, int64_t start, int64_t end, std::st
419419
Status PikaCache::SetRangexx(std::string& key, int64_t start, std::string &value) {
420420
int cache_index = CacheIndex(key);
421421
std::lock_guard lm(*cache_mutexs_[cache_index]);
422-
if (caches_[cache_index]->Exists(key)) {
423-
return caches_[cache_index]->SetRange(key, start, value);
424-
}
425-
return Status::NotFound("key not exist");
422+
return caches_[cache_index]->SetRange(key, start, value);
426423
}
427424

428425
Status PikaCache::Strlen(std::string& key, int32_t *len) {
@@ -443,16 +440,13 @@ Status PikaCache::HDel(std::string& key, std::vector<std::string> &fields) {
443440
Status PikaCache::HSet(std::string& key, std::string &field, std::string &value) {
444441
int cache_index = CacheIndex(key);
445442
std::lock_guard lm(*cache_mutexs_[cache_index]);
446-
return caches_[cache_index]->HSet(key, field, value);
443+
return caches_[cache_index]->HSetIfKeyExist(key, field, value);
447444
}
448445

449446
Status PikaCache::HSetIfKeyExist(std::string& key, std::string &field, std::string &value) {
450447
int cache_index = CacheIndex(key);
451448
std::lock_guard lm(*cache_mutexs_[cache_index]);
452-
if (caches_[cache_index]->Exists(key)) {
453-
return caches_[cache_index]->HSet(key, field, value);
454-
}
455-
return Status::NotFound("key not exist");
449+
return caches_[cache_index]->HSetIfKeyExist(key, field, value);
456450
}
457451

458452
Status PikaCache::HSetIfKeyExistAndFieldNotExist(std::string& key, std::string &field, std::string &value) {
@@ -496,11 +490,7 @@ Status PikaCache::HMSetnxWithoutTTL(std::string& key, std::vector<storage::Field
496490
Status PikaCache::HMSetxx(std::string& key, std::vector<storage::FieldValue> &fvs) {
497491
int cache_index = CacheIndex(key);
498492
std::lock_guard lm(*cache_mutexs_[cache_index]);
499-
if (caches_[cache_index]->Exists(key)) {
500-
return caches_[cache_index]->HMSet(key, fvs);
501-
} else {
502-
return Status::NotFound("key not exist");
503-
}
493+
return caches_[cache_index]->HMSet(key, fvs);
504494
}
505495

506496
Status PikaCache::HGet(std::string& key, std::string &field, std::string *value) {
@@ -687,10 +677,7 @@ Status PikaCache::SAdd(std::string& key, std::vector<std::string> &members) {
687677
Status PikaCache::SAddIfKeyExist(std::string& key, std::vector<std::string> &members) {
688678
int cache_index = CacheIndex(key);
689679
std::lock_guard lm(*cache_mutexs_[cache_index]);
690-
if (caches_[cache_index]->Exists(key)) {
691-
return caches_[cache_index]->SAdd(key, members);
692-
}
693-
return Status::NotFound("key not exist");
680+
return caches_[cache_index]->SAdd(key, members);
694681
}
695682

696683
Status PikaCache::SAddnx(std::string& key, std::vector<std::string> &members, int64_t ttl) {
@@ -1566,10 +1553,7 @@ Status PikaCache::SetBit(std::string& key, size_t offset, int64_t value) {
15661553
Status PikaCache::SetBitIfKeyExist(std::string& key, size_t offset, int64_t value) {
15671554
int cache_index = CacheIndex(key);
15681555
std::lock_guard lm(*cache_mutexs_[cache_index]);
1569-
if (caches_[cache_index]->Exists(key)) {
1570-
return caches_[cache_index]->SetBit(key, offset, value);
1571-
}
1572-
return Status::NotFound("key not exist");
1556+
return caches_[cache_index]->SetBit(key, offset, value);
15731557
}
15741558

15751559
Status PikaCache::GetBit(std::string& key, size_t offset, int64_t *value) {

src/pika_cache_load_thread.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ bool PikaCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr<DB>& db
6969
bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>& db) {
7070
int32_t len = 0;
7171
db->storage()->HLen(key, &len);
72+
// If the Hash type contains more than 2048 data members,
73+
// it will not be updated to RedisCache
7274
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
7375
return false;
7476
}
@@ -88,6 +90,8 @@ bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>&
8890
bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>& db) {
8991
uint64_t len = 0;
9092
db->storage()->LLen(key, &len);
93+
// If the List type contains more than 2048 data members,
94+
// it will not be updated to RedisCache
9195
if (len <= 0 || CACHE_VALUE_ITEM_MAX_SIZE < len) {
9296
LOG(WARNING) << "can not load key, because item size:" << len
9397
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
@@ -109,6 +113,8 @@ bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>&
109113
bool PikaCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr<DB>& db) {
110114
int32_t len = 0;
111115
db->storage()->SCard(key, &len);
116+
// If the Set type contains more than 2048 data members,
117+
// it will not be updated to RedisCache
112118
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
113119
LOG(WARNING) << "can not load key, because item size:" << len
114120
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
@@ -142,6 +148,9 @@ bool PikaCacheLoadThread::LoadZset(std::string& key, const std::shared_ptr<DB>&
142148
if (cache_len != 0) {
143149
return true;
144150
}
151+
// Only 512 members will be cached (in the default configuration),
152+
// and the first or last 512 elements will be cached depending on
153+
// whether the zset-cache-start-direction is 0 or 1
145154
if (zset_cache_start_direction_ == cache::CACHE_START_FROM_BEGIN) {
146155
if (zset_cache_field_num_per_key_ <= len) {
147156
stop_index = zset_cache_field_num_per_key_ - 1;

0 commit comments

Comments
 (0)