UniqueExpireStrategy.h 3.4 KB

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