|
3 | 3 | #include <drogon/drogon.h> |
4 | 4 | #include "util.h" |
5 | 5 |
|
| 6 | +#define INVALID_SET_MEMBER "_empty_" |
| 7 | + |
6 | 8 | using namespace drogon; |
7 | 9 | using namespace std::chrono_literals; |
8 | 10 |
|
9 | 11 | namespace service { |
10 | 12 | MemoryCache::MemoryCache() = default; |
11 | 13 |
|
| 14 | + Task<bool> MemoryCache::exists(std::string key) const { |
| 15 | + const auto client = app().getFastRedisClient(); |
| 16 | + const auto resp = co_await client->execCommandCoro("EXISTS %s", key.data()); |
| 17 | + co_return !resp.isNil() && resp.asInteger() == true; |
| 18 | + } |
| 19 | + |
| 20 | + Task<bool> MemoryCache::isSetMember(std::string key, std::string value) const { |
| 21 | + const auto client = app().getFastRedisClient(); |
| 22 | + const auto resp = co_await client->execCommandCoro("SISMEMBER %s %s", key.data(), value.data()); |
| 23 | + co_return !resp.isNil() && resp.asInteger() == true; |
| 24 | + } |
| 25 | + |
12 | 26 | Task<std::optional<std::string>> MemoryCache::getFromCache(std::string key) const { |
13 | 27 | const auto client = app().getFastRedisClient(); |
14 | 28 | const auto resp = co_await client->execCommandCoro("GET %s", key.data()); |
@@ -44,6 +58,24 @@ namespace service { |
44 | 58 | co_await trans->executeCoro(); |
45 | 59 | } |
46 | 60 |
|
| 61 | + Task<> MemoryCache::updateCacheSet(std::string key, const std::vector<std::string> value, |
| 62 | + const std::chrono::duration<long> expire) const { |
| 63 | + const auto client = app().getFastRedisClient(); |
| 64 | + const auto expireSeconds = std::chrono::seconds(expire).count(); |
| 65 | + |
| 66 | + std::vector valueCopy(value); |
| 67 | + if (valueCopy.empty()) { |
| 68 | + valueCopy.push_back(INVALID_SET_MEMBER); |
| 69 | + } |
| 70 | + |
| 71 | + const auto trans = co_await client->newTransactionCoro(); |
| 72 | + for (const auto &item: valueCopy) { |
| 73 | + co_await trans->execCommandCoro("SADD %s %s", key.data(), item.data()); |
| 74 | + } |
| 75 | + co_await trans->execCommandCoro("EXPIRE %s %ld", key.data(), expireSeconds); |
| 76 | + co_await trans->executeCoro(); |
| 77 | + } |
| 78 | + |
47 | 79 | Task<> MemoryCache::erase(std::string key) const { |
48 | 80 | const auto client = app().getFastRedisClient(); |
49 | 81 | co_await client->execCommandCoro("DEL %s", key.data()); |
|
0 commit comments