annotate OrthancFramework/Sources/Cache/LeastRecentlyUsedIndex.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Cache/LeastRecentlyUsedIndex.h@94f4a18a79cc
children bf7b9edf6b81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3453
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #pragma once
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <list>
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <map>
3453
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
38 #include <vector>
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <boost/noncopyable.hpp>
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
40 #include <cassert>
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
41
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
42 #include "../OrthancException.h"
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 283
diff changeset
43 #include "../Toolbox.h"
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 namespace Orthanc
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 * This class implements the index of a cache with least recently
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 * used (LRU) recycling policy. All the items of the cache index
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 * can be associated with a payload.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 * Reference: http://stackoverflow.com/a/2504317
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 template <typename T, typename Payload = NullType>
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
54 class LeastRecentlyUsedIndex : public boost::noncopyable
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 private:
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 typedef std::list< std::pair<T, Payload> > Queue;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 typedef std::map<T, typename Queue::iterator> Index;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 Index index_;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 Queue queue_;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 * Internal method for debug builds to check whether the internal
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 * data structures are not corrupted.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 void CheckInvariants() const;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 public:
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 * Add a new element to the cache index, and make it the most
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 * recent element.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 * \param id The ID of the element.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 * \param payload The payload of the element.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 void Add(T id, Payload payload = Payload());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
78 void AddOrMakeMostRecent(T id, Payload payload = Payload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
79
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 * When accessing an element of the cache, this method tags the
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 * element as the most recently used.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 * \param id The most recently accessed item.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 **/
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
85 void MakeMostRecent(T id);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
86
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
87 void MakeMostRecent(T id, Payload updatedPayload);
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 * Remove an element from the cache index.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 * \param id The item to remove.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 Payload Invalidate(T id);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 * Get the oldest element in the cache and remove it.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 * \return The oldest item.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 **/
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
99 T RemoveOldest();
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 * Get the oldest element in the cache, remove it and return the
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 * associated payload.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 * \param payload Where to store the associated payload.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 * \return The oldest item.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 T RemoveOldest(Payload& payload);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 * Check whether an element is contained in the cache.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 * \param id The item.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 * \return \c true iff the item is indexed by the cache.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 bool Contains(T id) const
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 return index_.find(id) != index_.end();
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
119 bool Contains(T id, Payload& payload) const
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
120 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
121 typename Index::const_iterator it = index_.find(id);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
122 if (it == index_.end())
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
123 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
124 return false;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
125 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
126 else
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
127 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
128 payload = it->second->second;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
129 return true;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
130 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
131 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
132
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
133 /**
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
134 * Return the number of elements in the cache.
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
135 * \return The number of elements.
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
136 **/
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
137 size_t GetSize() const
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
138 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
139 assert(index_.size() == queue_.size());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
140 return queue_.size();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
141 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
142
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 /**
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 * Check whether the cache index is empty.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 * \return \c true iff the cache is empty.
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 **/
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 bool IsEmpty() const
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 return index_.empty();
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
151
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
152 const T& GetOldest() const;
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
153
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
154 const Payload& GetOldestPayload() const;
3453
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
155
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
156 void GetAllKeys(std::vector<T>& keys) const
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
157 {
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
158 keys.clear();
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
159 keys.reserve(GetSize());
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
160 for (typename Index::const_iterator it = index_.begin(); it != index_.end(); it++)
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
161 {
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
162 keys.push_back(it->first);
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
163 }
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
164 }
8c663bbe5363 LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
165
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 };
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
167
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
168
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
169
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
170
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
171 /******************************************************************
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
172 ** Implementation of the template
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
173 ******************************************************************/
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
174
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
175 template <typename T, typename Payload>
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
176 void LeastRecentlyUsedIndex<T, Payload>::CheckInvariants() const
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
177 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
178 #ifndef NDEBUG
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
179 assert(index_.size() == queue_.size());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
180
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
181 for (typename Index::const_iterator
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
182 it = index_.begin(); it != index_.end(); it++)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
183 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
184 assert(it->second != queue_.end());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
185 assert(it->second->first == it->first);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
186 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
187 #endif
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
188 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
189
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
190
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
191 template <typename T, typename Payload>
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
192 void LeastRecentlyUsedIndex<T, Payload>::Add(T id, Payload payload)
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
193 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
194 if (Contains(id))
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
195 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
196 throw OrthancException(ErrorCode_BadSequenceOfCalls);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
197 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
198
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
199 queue_.push_front(std::make_pair(id, payload));
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
200 index_[id] = queue_.begin();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
201
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
202 CheckInvariants();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
203 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
204
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
205
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
206 template <typename T, typename Payload>
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
207 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id)
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
208 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
209 if (!Contains(id))
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
210 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
211 throw OrthancException(ErrorCode_InexistentItem);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
212 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
213
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
214 typename Index::iterator it = index_.find(id);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
215 assert(it != index_.end());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
216
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
217 std::pair<T, Payload> item = *(it->second);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
218
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
219 queue_.erase(it->second);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
220 queue_.push_front(item);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
221 index_[id] = queue_.begin();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
222
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
223 CheckInvariants();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
224 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
225
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
226
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
227 template <typename T, typename Payload>
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
228 void LeastRecentlyUsedIndex<T, Payload>::AddOrMakeMostRecent(T id, Payload payload)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
229 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
230 typename Index::iterator it = index_.find(id);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
231
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
232 if (it != index_.end())
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
233 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
234 // Already existing. Make it most recent.
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
235 std::pair<T, Payload> item = *(it->second);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
236 item.second = payload;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
237 queue_.erase(it->second);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
238 queue_.push_front(item);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
239 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
240 else
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
241 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
242 // New item
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
243 queue_.push_front(std::make_pair(id, payload));
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
244 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
245
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
246 index_[id] = queue_.begin();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
247
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
248 CheckInvariants();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
249 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
250
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
251
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
252 template <typename T, typename Payload>
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
253 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id, Payload updatedPayload)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
254 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
255 if (!Contains(id))
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
256 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
257 throw OrthancException(ErrorCode_InexistentItem);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
258 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
259
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
260 typename Index::iterator it = index_.find(id);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
261 assert(it != index_.end());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
262
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
263 std::pair<T, Payload> item = *(it->second);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
264 item.second = updatedPayload;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
265
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
266 queue_.erase(it->second);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
267 queue_.push_front(item);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
268 index_[id] = queue_.begin();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
269
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
270 CheckInvariants();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
271 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
272
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
273
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
274 template <typename T, typename Payload>
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
275 Payload LeastRecentlyUsedIndex<T, Payload>::Invalidate(T id)
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
276 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
277 if (!Contains(id))
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
278 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
279 throw OrthancException(ErrorCode_InexistentItem);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
280 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
281
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
282 typename Index::iterator it = index_.find(id);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
283 assert(it != index_.end());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
284
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
285 Payload payload = it->second->second;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
286 queue_.erase(it->second);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
287 index_.erase(it);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
288
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
289 CheckInvariants();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
290 return payload;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
291 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
292
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
293
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
294 template <typename T, typename Payload>
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
295 T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest(Payload& payload)
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
296 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
297 if (IsEmpty())
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
298 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
299 throw OrthancException(ErrorCode_BadSequenceOfCalls);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
300 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
301
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
302 std::pair<T, Payload> item = queue_.back();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
303 T oldest = item.first;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
304 payload = item.second;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
305
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
306 queue_.pop_back();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
307 assert(index_.find(oldest) != index_.end());
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
308 index_.erase(oldest);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
309
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
310 CheckInvariants();
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
311
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
312 return oldest;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
313 }
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
314
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
315
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
316 template <typename T, typename Payload>
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
317 T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest()
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
318 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
319 if (IsEmpty())
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
320 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
321 throw OrthancException(ErrorCode_BadSequenceOfCalls);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
322 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
323
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
324 std::pair<T, Payload> item = queue_.back();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
325 T oldest = item.first;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
326
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
327 queue_.pop_back();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
328 assert(index_.find(oldest) != index_.end());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
329 index_.erase(oldest);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
330
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
331 CheckInvariants();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
332
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
333 return oldest;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
334 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
335
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
336
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
337 template <typename T, typename Payload>
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
338 const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
339 {
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
340 if (IsEmpty())
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
341 {
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
342 throw OrthancException(ErrorCode_BadSequenceOfCalls);
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
343 }
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
344
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
345 return queue_.back().first;
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
346 }
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
347
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
348
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
349 template <typename T, typename Payload>
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
350 const Payload& LeastRecentlyUsedIndex<T, Payload>::GetOldestPayload() const
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
351 {
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
352 if (IsEmpty())
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
353 {
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
354 throw OrthancException(ErrorCode_BadSequenceOfCalls);
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
355 }
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
356
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
357 return queue_.back().second;
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
358 }
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 }