comparison Core/Cache/MemoryObjectCache.cpp @ 3563:4812825e69fc

accessor to the cache can now require to be unique
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Nov 2019 15:35:18 +0100
parents 4d809b2e1141
children 94f4a18a79cc
comparison
equal deleted inserted replaced
3562:f47149cdc048 3563:4812825e69fc
206 currentSize_ -= size; 206 currentSize_ -= size;
207 } 207 }
208 } 208 }
209 209
210 210
211 MemoryObjectCache::Reader::Reader(MemoryObjectCache& cache, 211 MemoryObjectCache::Accessor::Accessor(MemoryObjectCache& cache,
212 const std::string& key) : 212 const std::string& key,
213 #if !defined(__EMSCRIPTEN__) 213 bool unique) :
214 contentLock_(cache.contentMutex_),
215 cacheLock_(cache.cacheMutex_),
216 #endif
217 item_(NULL) 214 item_(NULL)
218 { 215 {
216 #if !defined(__EMSCRIPTEN__)
217 if (unique)
218 {
219 writerLock_ = WriterLock(cache.contentMutex_);
220 }
221 else
222 {
223 readerLock_ = ReaderLock(cache.contentMutex_);
224 }
225
226 // Lock the global structure of the cache, must be *after* the
227 // reader/writer lock
228 cacheLock_ = boost::mutex::scoped_lock(cache.cacheMutex_);
229 #endif
230
219 if (cache.content_.Contains(key, item_)) 231 if (cache.content_.Contains(key, item_))
220 { 232 {
221 cache.content_.MakeMostRecent(key); 233 cache.content_.MakeMostRecent(key);
222 } 234 }
223 235
224 #if !defined(__EMSCRIPTEN__) 236 #if !defined(__EMSCRIPTEN__)
225 cacheLock_.unlock(); 237 cacheLock_.unlock();
226 238
227 if (item_ == NULL) 239 if (item_ == NULL)
228 { 240 {
229 contentLock_.unlock(); 241 // This item does not exist in the cache, we can release the
230 } 242 // reader/writer lock
231 #endif 243 if (unique)
232 } 244 {
233 245 writerLock_.unlock();
234 246 }
235 ICacheable& MemoryObjectCache::Reader::GetValue() const 247 else
248 {
249 readerLock_.unlock();
250 }
251 }
252 #endif
253 }
254
255
256 ICacheable& MemoryObjectCache::Accessor::GetValue() const
236 { 257 {
237 if (IsValid()) 258 if (IsValid())
238 { 259 {
239 return item_->GetValue(); 260 return item_->GetValue();
240 } 261 }
243 throw OrthancException(ErrorCode_BadSequenceOfCalls); 264 throw OrthancException(ErrorCode_BadSequenceOfCalls);
244 } 265 }
245 } 266 }
246 267
247 268
248 const boost::posix_time::ptime& MemoryObjectCache::Reader::GetTime() const 269 const boost::posix_time::ptime& MemoryObjectCache::Accessor::GetTime() const
249 { 270 {
250 if (IsValid()) 271 if (IsValid())
251 { 272 {
252 return item_->GetTime(); 273 return item_->GetTime();
253 } 274 }