UniqueExpireStrategy.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // UniqueExpireStrategy.h
  3. //
  4. // Library: Foundation
  5. // Package: Cache
  6. // Module: UniqueExpireStrategy
  7. //
  8. // Definition of the UniqueExpireStrategy 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_UniqueExpireStrategy_INCLUDED
  16. #define Foundation_UniqueExpireStrategy_INCLUDED
  17. #include "Poco/KeyValueArgs.h"
  18. #include "Poco/ValidArgs.h"
  19. #include "Poco/AbstractStrategy.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 UniqueExpireStrategy: public AbstractStrategy<TKey, TValue>
  31. /// An UniqueExpireStrategy implements time based expiration of cache entries. In contrast
  32. /// to ExpireStrategy which only allows to set a per cache expiration value, it allows to define
  33. /// expiration per CacheEntry.
  34. /// Each TValue object must thus offer the following method:
  35. ///
  36. /// const Poco::Timestamp& getExpiration() const;
  37. ///
  38. /// which returns the absolute timepoint when the entry will be invalidated.
  39. {
  40. public:
  41. typedef std::multimap<Timestamp, TKey> TimeIndex;
  42. typedef typename TimeIndex::iterator IndexIterator;
  43. typedef typename TimeIndex::const_iterator ConstIndexIterator;
  44. typedef std::map<TKey, IndexIterator> Keys;
  45. typedef typename Keys::iterator Iterator;
  46. public:
  47. UniqueExpireStrategy()
  48. /// Create an unique expire strategy.
  49. {
  50. }
  51. ~UniqueExpireStrategy()
  52. {
  53. }
  54. void onAdd(const void*, const KeyValueArgs <TKey, TValue>& args)
  55. {
  56. // note: we have to insert even if the expire timepoint is in the past (for StrategyCollection classes to avoid inconsistency with LRU)
  57. // no problem: will be removed with next get
  58. const Timestamp& expire = args.value().getExpiration();
  59. IndexIterator it = _keyIndex.insert(std::make_pair(expire, args.key()));
  60. std::pair<Iterator, bool> stat = _keys.insert(std::make_pair(args.key(), it));
  61. if (!stat.second)
  62. {
  63. _keyIndex.erase(stat.first->second);
  64. stat.first->second = it;
  65. }
  66. }
  67. void onRemove(const void*, const TKey& key)
  68. {
  69. Iterator it = _keys.find(key);
  70. if (it != _keys.end())
  71. {
  72. _keyIndex.erase(it->second);
  73. _keys.erase(it);
  74. }
  75. }
  76. void onGet(const void*, const TKey& key)
  77. {
  78. // get triggers no changes in an expire
  79. }
  80. void onClear(const void*, const EventArgs& args)
  81. {
  82. _keys.clear();
  83. _keyIndex.clear();
  84. }
  85. void onIsValid(const void*, ValidArgs<TKey>& args)
  86. {
  87. Iterator it = _keys.find(args.key());
  88. if (it != _keys.end())
  89. {
  90. Timestamp now;
  91. if (it->second->first <= now)
  92. {
  93. args.invalidate();
  94. }
  95. }
  96. else //not found: probably removed by onReplace
  97. args.invalidate();
  98. }
  99. void onReplace(const void*, std::set<TKey>& elemsToRemove)
  100. {
  101. // Note: replace only informs the cache which elements
  102. // it would like to remove!
  103. // it does not remove them on its own!
  104. IndexIterator it = _keyIndex.begin();
  105. Timestamp now;
  106. while (it != _keyIndex.end() && it->first < now)
  107. {
  108. elemsToRemove.insert(it->second);
  109. ++it;
  110. }
  111. }
  112. protected:
  113. Keys _keys; /// For faster replacement of keys, the iterator points to the _keyIndex map
  114. TimeIndex _keyIndex; /// Maps time to key value
  115. };
  116. } // namespace Poco
  117. #endif // Foundation_UniqueExpireStrategy_INCLUDED