Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ fix: improving performance #10

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 49 additions & 44 deletions core/cache/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "cache.h"

Cache::Cache(){
std::filesystem::create_directory("data");

for (const auto& row: std::filesystem::directory_iterator("data")){
if (row.path().extension() != ".dat") continue;

Expand Down Expand Up @@ -61,40 +63,34 @@ Cache::Cache(){
}
}

std::string Cache::get(std::string db, std::string key){
if (cache_.find(db) != cache_.end()) {
if (cache_[db].find(key) != cache_[db].end()){
return cache_[db][key].value;
std::string Cache::get(const std::string& db, const std::string& key) noexcept {
auto data = cache_.find(db);
if (data != cache_.end()) {
auto value = data->second.find(key);
if (value != data->second.end()){
return value->second.value;
}
}

return "";
}

bool Cache::set(std::string db, std::string key, std::string value, int expire){

CacheStruct data;
data.value = value;
data.expire = expire;

cache_[db][key] = data;
bool Cache::set(const std::string& db, const std::string& key, const std::string& value, int expire) noexcept {
CacheStruct data{std::move(value), expire};
cache_[db].insert_or_assign(key, std::move(data));
isChange = true;

return true;
}

bool Cache::del(std::string db, std::string key){
if (cache_.find(db) != cache_.end()) {
if (cache_[db].find(key) != cache_[db].end()){
cache_[db].erase(key);

return true;
}
}
bool Cache::del(const std::string& db, const std::string& key) noexcept {
cache_[db].erase(key);
isChange = true;

return false;
}

std::vector<std::string> Cache::keys(std::string db){
std::vector<std::string> Cache::keys(const std::string& db) noexcept {
std::vector<std::string> keys;

for (const auto& row: cache_[db]){
Expand All @@ -104,43 +100,47 @@ std::vector<std::string> Cache::keys(std::string db){
return keys;
}

void Cache::save(){
std::filesystem::create_directory("data");

size_t cacheSize = cache_.size();
void Cache::save() noexcept {
if (isChange) {
std::filesystem::create_directory("data");

size_t cacheSize = cache_.size();

for (const auto& row: cache_) {
const std::string& key = row.first;
const std::unordered_map<std::string, CacheStruct>& map = row.second;

for (const auto& row: cache_) {
const std::string& key = row.first;
const std::unordered_map<std::string, CacheStruct>& map = row.second;
std::string filename = "data/" + key + ".dat";
std::ofstream file(filename, std::ios::binary);

std::string filename = "data/" + key + ".dat";
std::ofstream file(filename, std::ios::binary);
file.write(reinterpret_cast<const char*>(&cacheSize), sizeof(cacheSize));

file.write(reinterpret_cast<const char*>(&cacheSize), sizeof(cacheSize));
size_t keySize = key.size();
file.write(reinterpret_cast<const char*>(&keySize), sizeof(keySize));
file.write(key.data(), keySize);

size_t keySize = key.size();
file.write(reinterpret_cast<const char*>(&keySize), sizeof(keySize));
file.write(key.data(), keySize);
size_t mapSize = map.size();
file.write(reinterpret_cast<const char*>(&mapSize), sizeof(mapSize));

size_t mapSize = map.size();
file.write(reinterpret_cast<const char*>(&mapSize), sizeof(mapSize));
for (const auto& rowPair: map) {
const std::string& pairKey = rowPair.first;
const CacheStruct& cachedata = rowPair.second;

for (const auto& rowPair: map) {
const std::string& pairKey = rowPair.first;
const CacheStruct& cachedata = rowPair.second;
size_t pairKeySize = pairKey.size();
file.write(reinterpret_cast<const char*>(&pairKeySize), sizeof(pairKeySize));
file.write(pairKey.data(), pairKeySize);

size_t pairKeySize = pairKey.size();
file.write(reinterpret_cast<const char*>(&pairKeySize), sizeof(pairKeySize));
file.write(pairKey.data(), pairKeySize);
cachedata.serialize(file);
}

cachedata.serialize(file);
file.close();
}

file.close();
isChange = false;
}
}

void Cache::expire(){
void Cache::expire() noexcept {
std::thread([this]() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
Expand All @@ -155,4 +155,9 @@ void Cache::expire(){
}
}
}).detach();
}

Cache::~Cache(){
isChange = true;
save();
}
14 changes: 8 additions & 6 deletions core/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@
class Cache {
public:
Cache ();
std::string get(std::string db, std::string key);
bool set(std::string db, std::string key, std::string value, int expire = -1);
bool del(std::string db, std::string key);
std::vector<std::string> keys (std::string db);
void save();
void expire();
std::string get(const std::string& db, const std::string& key) noexcept;
bool set(const std::string& db, const std::string& key, const std::string& value, int expire = -1) noexcept;
bool del(const std::string& db, const std::string& key) noexcept;
std::vector<std::string> keys(const std::string& db) noexcept;
void save() noexcept;
void expire() noexcept;
~Cache();

private:
std::unordered_map<std::string, std::unordered_map<std::string, CacheStruct>> cache_;
bool isChange = false;
};

#endif
Loading
Loading