source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/UniqueExpireStrategy.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1//
2// UniqueExpireStrategy.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/UniqueExpireStrategy.h#2 $
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// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef  Foundation_UniqueExpireStrategy_INCLUDED
40#define  Foundation_UniqueExpireStrategy_INCLUDED
41
42
43#include "Poco/KeyValueArgs.h"
44#include "Poco/ValidArgs.h"
45#include "Poco/AbstractStrategy.h"
46#include "Poco/Bugcheck.h"
47#include "Poco/Timestamp.h"
48#include "Poco/EventArgs.h"
49#include <set>
50#include <map>
51
52
53namespace Poco {
54
55
56template < 
57        class TKey,
58        class TValue
59>
60class UniqueExpireStrategy: public AbstractStrategy<TKey, TValue>
61        /// An UniqueExpireStrategy implements time based expiration of cache entries. In contrast
62        /// to ExpireStrategy which only allows to set a per cache expiration value, it allows to define
63        /// expiration per CacheEntry.
64        /// Each TValue object must thus offer the following method:
65        ///   
66        ///    const Poco::Timestamp& getExpiration() const;
67        ///   
68        /// which returns the absolute timepoint when the entry will be invalidated.
69{
70public:
71        typedef std::multimap<Timestamp, TKey>     TimeIndex;
72        typedef typename TimeIndex::iterator       IndexIterator;
73        typedef typename TimeIndex::const_iterator ConstIndexIterator;
74        typedef std::map<TKey, IndexIterator>      Keys;
75        typedef typename Keys::iterator            Iterator;
76
77public:
78        UniqueExpireStrategy()
79                /// Create an unique expire strategy.
80        {
81        }
82
83        ~UniqueExpireStrategy()
84        {
85        }
86
87        void onAdd(const void*, const KeyValueArgs <TKey, TValue>& args)
88        {
89                // note: we have to insert even if the expire timepoint is in the past (for StrategyCollection classes to avoid inconsistency with LRU)
90                // no problem: will be removed with next get
91                const Timestamp& expire = args.value().getExpiration();
92                IndexIterator it = _keyIndex.insert(std::make_pair(expire, args.key()));
93                std::pair<Iterator, bool> stat = _keys.insert(std::make_pair(args.key(), it));
94                if (!stat.second)
95                {
96                        _keyIndex.erase(stat.first->second);
97                        stat.first->second = it;
98                }
99        }
100
101        void onRemove(const void*, const TKey& key)
102        {
103                Iterator it = _keys.find(key);
104                if (it != _keys.end())
105                {
106                        _keyIndex.erase(it->second);
107                        _keys.erase(it);
108                }
109        }
110
111        void onGet(const void*, const TKey& key)
112        {
113                // get triggers no changes in an expire
114        }
115
116        void onClear(const void*, const EventArgs& args)
117        {
118                _keys.clear();
119                _keyIndex.clear();
120        }
121
122        void onIsValid(const void*, ValidArgs<TKey>& args)
123        {
124                Iterator it = _keys.find(args.key());
125                if (it != _keys.end())
126                {
127                        Timestamp now;
128                        if (it->second->first <= now)
129                        {
130                                args.invalidate();
131                        }
132                }
133                else //not found: probably removed by onReplace
134                        args.invalidate();
135        }
136
137        void onReplace(const void*, std::set<TKey>& elemsToRemove)
138        {
139                // Note: replace only informs the cache which elements
140                // it would like to remove!
141                // it does not remove them on its own!
142                IndexIterator it = _keyIndex.begin();
143                Timestamp now;
144                while (it != _keyIndex.end() && it->first < now)
145                {
146                        elemsToRemove.insert(it->second);
147                        ++it;
148                }
149        }
150
151protected:
152        Keys      _keys;     /// For faster replacement of keys, the iterator points to the _keyIndex map
153        TimeIndex _keyIndex; /// Maps time to key value
154};
155
156
157} // namespace Poco
158
159
160#endif
Note: See TracBrowser for help on using the repository browser.