comparison Framework/Deprecated/Toolbox/BaseWebService.cpp @ 918:d6c029d15aaa

Merged am-dev into default
author Alain Mazy <am@osimis.io>
date Fri, 19 Jul 2019 15:15:13 +0200
parents 200b4e0dddfc
children 861c080ef47b
comparison
equal deleted inserted replaced
914:4d1f57773b5b 918:d6c029d15aaa
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>
31 #include <Core/Logging.h>
30 32
31 namespace Deprecated 33 namespace Deprecated
32 { 34 {
33 35
34 36
87 Orthanc::IDynamicObject* payload /* takes ownership */, 89 Orthanc::IDynamicObject* payload /* takes ownership */,
88 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback, 90 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
89 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, 91 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
90 unsigned int timeoutInSeconds) 92 unsigned int timeoutInSeconds)
91 { 93 {
92 if (cache_.find(uri) == cache_.end()) 94 if (!cacheEnabled_ || cache_.find(uri) == cache_.end())
93 { 95 {
94 GetAsyncInternal(uri, headers, 96 GetAsyncInternal(uri, headers,
95 new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered 97 new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered
96 new OrthancStone::Callable<BaseWebService, IWebService::HttpRequestSuccessMessage> 98 new OrthancStone::Callable<BaseWebService, IWebService::HttpRequestSuccessMessage>
97 (*this, &BaseWebService::CacheAndNotifyHttpSuccess), 99 (*this, &BaseWebService::CacheAndNotifyHttpSuccess),
99 (*this, &BaseWebService::NotifyHttpError), 101 (*this, &BaseWebService::NotifyHttpError),
100 timeoutInSeconds); 102 timeoutInSeconds);
101 } 103 }
102 else 104 else
103 { 105 {
106 // put the uri on top of the most recently accessed list
107 std::deque<std::string>::iterator it = std::find(orderedCacheKeys_.begin(), orderedCacheKeys_.end(), uri);
108 if (it != orderedCacheKeys_.end())
109 {
110 std::string uri = *it;
111 orderedCacheKeys_.erase(it);
112 orderedCacheKeys_.push_front(uri);
113 }
114
104 // create a command and "post" it to the Oracle so it is executed and commited "later" 115 // create a command and "post" it to the Oracle so it is executed and commited "later"
105 NotifyHttpSuccessLater(cache_[uri], payload, successCallback); 116 NotifyHttpSuccessLater(cache_[uri], payload, successCallback);
106 } 117 }
107 118
108 } 119 }
121 } 132 }
122 } 133 }
123 134
124 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) 135 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
125 { 136 {
126 cache_[message.GetUri()] = boost::shared_ptr<CachedHttpRequestSuccessMessage>(new CachedHttpRequestSuccessMessage(message)); 137 if (cacheEnabled_)
138 {
139 while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0)
140 {
141 VLOG(1) << "BaseWebService: clearing cache: " << cacheCurrentSize_ << "/" << cacheMaxSize_ << "(" << message.GetAnswerSize() << ")";
142 const std::string& oldestUri = orderedCacheKeys_.back();
143 HttpCache::iterator it = cache_.find(oldestUri);
144 if (it != cache_.end())
145 {
146 cacheCurrentSize_ -= it->second->GetAnswerSize();
147 cache_.erase(it);
148 }
149 orderedCacheKeys_.pop_back();
150
151 }
152
153 boost::shared_ptr<CachedHttpRequestSuccessMessage> cachedMessage(new CachedHttpRequestSuccessMessage(message));
154 cache_[message.GetUri()] = cachedMessage;
155 orderedCacheKeys_.push_front(message.GetUri());
156 cacheCurrentSize_ += message.GetAnswerSize();
157 }
158
127 NotifyHttpSuccess(message); 159 NotifyHttpSuccess(message);
128 } 160 }
129 161
130 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message) 162 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message)
131 { 163 {