diff --git a/passphrase_cache.cc b/passphrase_cache.cc index 57c0f35..c4211d4 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -24,6 +24,8 @@ namespace pEp { { while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout) _cache.pop_front(); + while (_cache.size() > _max_size) + _cache.pop_front(); } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index d74e3be..83ca0eb 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -19,11 +19,12 @@ namespace pEp { std::list _cache; std::mutex _mtx; + int _max_size; duration _timeout; public: - PassphraseCache(duration timeout = std::chrono::minutes(10)) : - _timeout(timeout) { } + PassphraseCache(int max_size=20, duration timeout = std::chrono::minutes(10)) : + _max_size(max_size), _timeout(timeout) { } ~PassphraseCache() { } PassphraseCache(const PassphraseCache& second) : _cache(second._cache), _timeout(second._timeout) { } diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index 77c5f43..29ed967 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -6,7 +6,8 @@ int main() { - pEp::PassphraseCache cache{std::chrono::seconds(1)}; + pEp::PassphraseCache cache{2, std::chrono::seconds(1)}; + cache.add("say"); cache.add("hello"); cache.add("world");