fixed GH #2738: Poco::AccessExpireStrategy::onGet() must not extend expiration time after expiration

This commit is contained in:
Günter Obiltschnig 2019-07-01 17:51:38 +02:00
parent 4c3351ea05
commit 12897d3d63
5 changed files with 44 additions and 9 deletions

View File

@ -31,7 +31,7 @@
namespace Poco { namespace Poco {
template < template <
class TKey, class TKey,
class TValue class TValue
> >
@ -55,11 +55,14 @@ public:
typename ExpireStrategy<TKey, TValue>::Iterator it = this->_keys.find(key); typename ExpireStrategy<TKey, TValue>::Iterator it = this->_keys.find(key);
if (it != this->_keys.end()) if (it != this->_keys.end())
{ {
this->_keyIndex.erase(it->second); if (!it->second->first.isElapsed(this->_expireTime)) // don't extend if already expired
Timestamp now; {
typename ExpireStrategy<TKey, TValue>::IndexIterator itIdx = this->_keyIndex.erase(it->second);
this->_keyIndex.insert(typename ExpireStrategy<TKey, TValue>::TimeIndex::value_type(now, key)); Timestamp now;
it->second = itIdx; typename ExpireStrategy<TKey, TValue>::IndexIterator itIdx =
this->_keyIndex.insert(typename ExpireStrategy<TKey, TValue>::TimeIndex::value_type(now, key));
it->second = itIdx;
}
} }
} }
}; };

View File

@ -183,7 +183,22 @@ void ExpireCacheTest::testExpireWithHas()
aCache.add(1, 2); // 1 aCache.add(1, 2); // 1
assertTrue (aCache.has(1)); assertTrue (aCache.has(1));
Thread::sleep(DURWAIT); Thread::sleep(DURWAIT);
assertTrue (!aCache.has(1)); assert (!aCache.has(1));
}
void ExpireCacheTest::testAccessExpireGet()
{
AccessExpireCache<int, int> aCache(DURSLEEP);
aCache.add(1, 2); // 1
assertTrue (aCache.has(1));
SharedPtr<int> tmp = aCache.get(1);
assertTrue (!tmp.isNull());
assertTrue (*tmp == 2);
assertTrue (aCache.size() == 1);
Thread::sleep(DURWAIT);
tmp = aCache.get(1);
assertTrue (tmp.isNull());
} }
@ -207,6 +222,7 @@ CppUnit::Test* ExpireCacheTest::suite()
CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd); CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);
CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN); CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN);
CppUnit_addTest(pSuite, ExpireCacheTest, testExpireWithHas); CppUnit_addTest(pSuite, ExpireCacheTest, testExpireWithHas);
CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireGet);
return pSuite; return pSuite;
} }

View File

@ -29,8 +29,8 @@ public:
void testExpireN(); void testExpireN();
void testAccessExpireN(); void testAccessExpireN();
void testExpireWithHas(); void testExpireWithHas();
void testAccessExpireGet();
void setUp(); void setUp();
void tearDown(); void tearDown();
static CppUnit::Test* suite(); static CppUnit::Test* suite();

View File

@ -303,6 +303,20 @@ void ExpireLRUCacheTest::testDuplicateAdd()
} }
void ExpireLRUCacheTest::testAccessExpireGet()
{
ExpireLRUCache<int, int> aCache(3, DURSLEEP);
aCache.add(1, 2); // 1
assertTrue (aCache.has(1));
SharedPtr<int> tmp = aCache.get(1);
assertTrue (!tmp.isNull());
assertTrue (*tmp == 2);
Thread::sleep(DURWAIT);
tmp = aCache.get(1);
assertTrue (tmp.isNull());
}
void ExpireLRUCacheTest::setUp() void ExpireLRUCacheTest::setUp()
{ {
} }
@ -326,6 +340,7 @@ CppUnit::Test* ExpireLRUCacheTest::suite()
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize2); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize2);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSizeN); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSizeN);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testDuplicateAdd); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testDuplicateAdd);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testAccessExpireGet);
return pSuite; return pSuite;
} }

View File

@ -32,7 +32,8 @@ public:
void testCacheSize2(); void testCacheSize2();
void testCacheSizeN(); void testCacheSizeN();
void testDuplicateAdd(); void testDuplicateAdd();
void testAccessExpireGet();
void setUp(); void setUp();
void tearDown(); void tearDown();
static CppUnit::Test* suite(); static CppUnit::Test* suite();