1//
2// ExpireStrategy.h
3//
4// Library: Foundation
5// Package: Cache
6// Module: ExpireStrategy
7//
8// Definition of the ExpireStrategy class.
9//
10// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_ExpireStrategy_INCLUDED
18#define Foundation_ExpireStrategy_INCLUDED
19
20
21#include "Poco/KeyValueArgs.h"
22#include "Poco/ValidArgs.h"
23#include "Poco/AbstractStrategy.h"
24#include "Poco/Bugcheck.h"
25#include "Poco/Timestamp.h"
26#include "Poco/EventArgs.h"
27#include "Poco/Exception.h"
28#include <set>
29#include <map>
30
31
32namespace Poco {
33
34
35template <
36 class TKey,
37 class TValue
38>
39class ExpireStrategy: public AbstractStrategy<TKey, TValue>
40 /// An ExpireStrategy implements time based expiration of cache entries
41{
42public:
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
49public:
50 ExpireStrategy(Timestamp::TimeDiff expireTimeInMilliSec): _expireTime(expireTimeInMilliSec * 1000)
51 /// Create an expire strategy. Note that the smallest allowed caching time is 25ms.
52 /// Anything lower than that is not useful with current operating systems.
53 {
54 if (_expireTime < 25000) throw InvalidArgumentException("expireTime must be at least 25 ms");
55 }
56
57 ~ExpireStrategy()
58 {
59 }
60
61 void onAdd(const void*, const KeyValueArgs <TKey, TValue>& args)
62 {
63 Timestamp now;
64 typename TimeIndex::value_type tiValue(now, args.key());
65 IndexIterator it = _keyIndex.insert(tiValue);
66 typename Keys::value_type kValue(args.key(), it);
67 std::pair<Iterator, bool> stat = _keys.insert(kValue);
68 if (!stat.second)
69 {
70 _keyIndex.erase(stat.first->second);
71 stat.first->second = it;
72 }
73 }
74
75 void onRemove(const void*, const TKey& key)
76 {
77 Iterator it = _keys.find(key);
78 if (it != _keys.end())
79 {
80 _keyIndex.erase(it->second);
81 _keys.erase(it);
82 }
83 }
84
85 void onGet(const void*, const TKey& key)
86 {
87 // get triggers no changes in an expire
88 }
89
90 void onClear(const void*, const EventArgs& args)
91 {
92 _keys.clear();
93 _keyIndex.clear();
94 }
95
96 void onIsValid(const void*, ValidArgs<TKey>& args)
97 {
98 Iterator it = _keys.find(args.key());
99 if (it != _keys.end())
100 {
101 if (it->second->first.isElapsed(_expireTime))
102 {
103 args.invalidate();
104 }
105 }
106 else //not found: probably removed by onReplace
107 args.invalidate();
108 }
109
110 void onReplace(const void*, std::set<TKey>& elemsToRemove)
111 {
112 // Note: replace only informs the cache which elements
113 // it would like to remove!
114 // it does not remove them on its own!
115 IndexIterator it = _keyIndex.begin();
116 while (it != _keyIndex.end() && it->first.isElapsed(_expireTime))
117 {
118 elemsToRemove.insert(it->second);
119 ++it;
120 }
121 }
122
123protected:
124 Timestamp::TimeDiff _expireTime;
125 Keys _keys; /// For faster replacement of keys, the iterator points to the _keyIndex map
126 TimeIndex _keyIndex; /// Maps time to key value
127};
128
129
130} // namespace Poco
131
132
133#endif // Foundation_ExpireStrategy_INCLUDED
134