Browse Source

keep good passphrases in cache

synchronous
Volker Birk 5 years ago
parent
commit
1256026589
  1. 12
      passphrase_cache.cc
  2. 4
      passphrase_cache.hh
  3. 3
      test/test_passphrase_cache.cc

12
passphrase_cache.cc

@ -12,9 +12,11 @@ namespace pEp {
std::lock_guard<std::mutex> 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);
}
};

4
passphrase_cache.hh

@ -16,8 +16,9 @@ namespace pEp {
std::string passphrase;
time_point tp;
};
using cache = std::list<cache_entry>;
std::list<cache_entry> _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);
};
};

3
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";

Loading…
Cancel
Save