diff --git a/TODO.md b/TODO.md index e348099..718cd27 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ ## šŸ“Œ Tarefas Pendentes ### Funcionalidades -- [ ] Implementar expiraĆ§Ć£o dos valores ao utilizar o comando `SET key value expire`. +- [x] Implementar expiraĆ§Ć£o dos valores ao utilizar o comando `SET key value expire`. ### Melhorias - [ ] Melhorar o mĆ©todo de salvamento dos dados no arquivo `.dat`. diff --git a/core/cache/cache.cpp b/core/cache/cache.cpp index ee45c10..b733781 100644 --- a/core/cache/cache.cpp +++ b/core/cache/cache.cpp @@ -128,4 +128,21 @@ void Cache::save(){ cachedata.serialize(file); } } +} + +void Cache::expire(){ + std::thread([this]() { + while (true) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + for (auto& row: cache_) { + for (auto& item: row.second) { + if (item.second.expire == 0){ + del(row.first, item.first); + }else if(item.second.expire > 0){ + --item.second.expire; + } + } + } + } + }).detach(); } \ No newline at end of file diff --git a/core/cache/cache.h b/core/cache/cache.h index dbc931d..1bbcb22 100644 --- a/core/cache/cache.h +++ b/core/cache/cache.h @@ -24,6 +24,8 @@ #include #include + #include + #include #include #include @@ -55,6 +57,7 @@ bool del(std::string db, std::string key); std::vector keys (std::string db); void save(); + void expire(); private: std::unordered_map> cache_; diff --git a/core/manager/manager.h b/core/manager/manager.h index c393ccc..c658c87 100644 --- a/core/manager/manager.h +++ b/core/manager/manager.h @@ -22,7 +22,6 @@ #ifndef CORE_MANAGER_H #define CORE_MANAGER_H - #include #include #include #include diff --git a/main.cpp b/main.cpp index d87e184..7b0c6b1 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ int main(){ Cache cache_; + cache_.expire(); try { boost::asio::io_context io_context;