AccessExpireStrategy.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // AccessExpireStrategy.h
  3. //
  4. // Library: Foundation
  5. // Package: Cache
  6. // Module: AccessExpireStrategy
  7. //
  8. // Definition of the AccessExpireStrategy class.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_AccessExpireStrategy_INCLUDED
  16. #define Foundation_AccessExpireStrategy_INCLUDED
  17. #include "Poco/KeyValueArgs.h"
  18. #include "Poco/ValidArgs.h"
  19. #include "Poco/ExpireStrategy.h"
  20. #include "Poco/Bugcheck.h"
  21. #include "Poco/Timestamp.h"
  22. #include "Poco/EventArgs.h"
  23. #include <set>
  24. #include <map>
  25. namespace Poco {
  26. template <
  27. class TKey,
  28. class TValue
  29. >
  30. class AccessExpireStrategy: public ExpireStrategy<TKey, TValue>
  31. /// An AccessExpireStrategy implements time and access based expiration of cache entries
  32. {
  33. public:
  34. AccessExpireStrategy(Timestamp::TimeDiff expireTimeInMilliSec): ExpireStrategy<TKey, TValue>(expireTimeInMilliSec)
  35. /// Create an expire strategy. Note that the smallest allowed caching time is 25ms.
  36. /// Anything lower than that is not useful with current operating systems.
  37. {
  38. }
  39. ~AccessExpireStrategy()
  40. {
  41. }
  42. void onGet(const void*, const TKey& key)
  43. {
  44. // get triggers an update to the expiration time
  45. typename ExpireStrategy<TKey, TValue>::Iterator it = this->_keys.find(key);
  46. if (it != this->_keys.end())
  47. {
  48. if (!it->second->first.isElapsed(this->_expireTime)) // don't extend if already expired
  49. {
  50. this->_keyIndex.erase(it->second);
  51. Timestamp now;
  52. typename ExpireStrategy<TKey, TValue>::IndexIterator itIdx =
  53. this->_keyIndex.insert(typename ExpireStrategy<TKey, TValue>::TimeIndex::value_type(now, key));
  54. it->second = itIdx;
  55. }
  56. }
  57. }
  58. };
  59. } // namespace Poco
  60. #endif // Foundation_AccessExpireStrategy_INCLUDED