diff --git a/passphrase_cache.cc b/passphrase_cache.cc index c4211d4..196a352 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -12,9 +12,11 @@ namespace pEp { std::lock_guard lock(_mtx); cleanup(); - for (auto entry : _cache) { - if (callee(entry.passphrase)) + for (auto entry=_cache.begin(); entry!=_cache.end(); ++entry) { + if (callee(entry->passphrase)) { + refresh(entry); return true; + } } return false; @@ -27,5 +29,11 @@ namespace pEp { while (_cache.size() > _max_size) _cache.pop_front(); } + + void PassphraseCache::refresh(cache::iterator entry) + { + entry->tp = clock::now(); + _cache.splice(_cache.end(), _cache, entry); + } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 83ca0eb..60224f0 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -16,8 +16,9 @@ namespace pEp { std::string passphrase; time_point tp; }; + using cache = std::list; - std::list _cache; + cache _cache; std::mutex _mtx; int _max_size; duration _timeout; @@ -40,6 +41,7 @@ namespace pEp { protected: void cleanup(); + void refresh(cache::iterator entry); }; }; diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index 29ed967..22df2b5 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -17,6 +17,9 @@ int main() std::cout << "expected: one passphrase\n"; cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return true;}); + std::cout << "expected: two passphrases but reverse order\n"; + cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); + sleep(2); std::cout << "expected: no passphrase\n";