Skip to content

Commit

Permalink
LRU cache - call cleanup after 'put' and allow capacity-sized cache
Browse files Browse the repository at this point in the history
 - this makes intention clearer
  • Loading branch information
astibal committed Oct 7, 2024
1 parent 2a88afb commit bed4200
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/service/cfgapi/cfgapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,6 @@ bool CfgFactory::load_settings () {
NbrHood::instance().cache().set_capacity(static_cast<size_t>(nbr_cache_size));
}



if(int open_timeout = 0; load_if_exists(cfgapi.getRoot()["settings"]["tuning"], "host_open_timeout", open_timeout)) {
baseHostCX::params.open_timeout = open_timeout;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/lru.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class LRUCache {
}

void cleanup_ul() {
while(cache_.size() >= capacity_) {
while(cache_.size() > capacity_) {
if(cache_.size() > 0L) {
Key oldestKey = lruList.back();
lruList.pop_back();
Expand All @@ -76,15 +76,15 @@ class LRUCache {

void put_ul(const Key& key, Value const& value) {

cleanup_ul();

if (auto it = cache_.find(key); it != cache_.end()) {
lruList.erase(it->second.second);
cache_.erase(it);
}

lruList.push_front(key);
cache_[key] = {value, lruList.begin()};

cleanup_ul();
}

std::string to_string(int verbosity) const {
Expand Down

0 comments on commit bed4200

Please sign in to comment.