comparison Framework/Deprecated/Toolbox/BaseWebService.cpp @ 872:733c6db3e5a3 am-dev

limiting the size of the cache
author Alain Mazy <alain@mazy.be>
date Mon, 01 Jul 2019 13:36:51 +0200
parents c0fcb2757b0a
children 200b4e0dddfc
comparison
equal deleted inserted replaced
863:23701fbf228e 872:733c6db3e5a3
25 #include "../../../Platforms/Generic/IOracleCommand.h" 25 #include "../../../Platforms/Generic/IOracleCommand.h"
26 26
27 #include <Core/OrthancException.h> 27 #include <Core/OrthancException.h>
28 28
29 #include <boost/shared_ptr.hpp> 29 #include <boost/shared_ptr.hpp>
30 #include <algorithm>
30 31
31 namespace Deprecated 32 namespace Deprecated
32 { 33 {
33 34
34 35
99 (*this, &BaseWebService::NotifyHttpError), 100 (*this, &BaseWebService::NotifyHttpError),
100 timeoutInSeconds); 101 timeoutInSeconds);
101 } 102 }
102 else 103 else
103 { 104 {
105 // put the uri on top of the most recently accessed list
106 std::deque<std::string>::iterator it = std::find(orderedCacheKeys_.begin(), orderedCacheKeys_.end(), uri);
107 if (it != orderedCacheKeys_.end())
108 {
109 std::string uri = *it;
110 orderedCacheKeys_.erase(it);
111 orderedCacheKeys_.push_front(uri);
112 }
113
104 // create a command and "post" it to the Oracle so it is executed and commited "later" 114 // create a command and "post" it to the Oracle so it is executed and commited "later"
105 NotifyHttpSuccessLater(cache_[uri], payload, successCallback); 115 NotifyHttpSuccessLater(cache_[uri], payload, successCallback);
106 } 116 }
107 117
108 } 118 }
121 } 131 }
122 } 132 }
123 133
124 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) 134 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
125 { 135 {
136 while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0)
137 {
138 const std::string& oldestUri = orderedCacheKeys_.back();
139 HttpCache::iterator it = cache_.find(oldestUri);
140 if (it != cache_.end())
141 {
142 cacheCurrentSize_ -= it->second->GetAnswerSize();
143 cache_.erase(it);
144 }
145 orderedCacheKeys_.pop_back();
146
147 }
148
126 cache_[message.GetUri()] = boost::shared_ptr<CachedHttpRequestSuccessMessage>(new CachedHttpRequestSuccessMessage(message)); 149 cache_[message.GetUri()] = boost::shared_ptr<CachedHttpRequestSuccessMessage>(new CachedHttpRequestSuccessMessage(message));
150 orderedCacheKeys_.push_front(message.GetUri());
151 cacheCurrentSize_ += message.GetAnswerSize();
152
127 NotifyHttpSuccess(message); 153 NotifyHttpSuccess(message);
128 } 154 }
129 155
130 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message) 156 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message)
131 { 157 {