OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
CurlHeaderCache.cpp
Go to the documentation of this file.
1 #include "CurlHeaderCache.h"
2 #include "CurlStreamDefaults.h"
3 
4 std::shared_ptr<ossim::CurlHeaderCache> ossim::CurlHeaderCache::m_instance;
5 
6 
8 :m_maxCacheEntries(ossim::CurlStreamDefaults::m_nReadCacheHeaders)
9 {
10 
11 }
12 
14 {
15  m_cache.clear();
16 }
17 
18 std::shared_ptr<ossim::CurlHeaderCache> ossim::CurlHeaderCache::instance()
19 {
20  if(!m_instance)
21  {
22  m_instance = std::make_shared<CurlHeaderCache>();
23  }
24 
25  return m_instance;
26 }
27 
29 {
30  std::unique_lock<std::mutex> lock(m_mutex);
31  bool result = false;
32  if(m_maxCacheEntries<=0) return result;
33  CacheType::const_iterator iter = m_cache.find(key);
34 
35  if(iter != m_cache.end())
36  {
37  filesize = iter->second->m_filesize;
38  result = true;
39  }
40  if(result)
41  {
42  ossim::CurlHeaderCache* curlHeaderConstPtr = const_cast<ossim::CurlHeaderCache*>(this);
43  curlHeaderConstPtr->touchEntryProtected(key);
44  }
45 
46  return result;
47 }
48 
50 {
51  std::unique_lock<std::mutex> lock(m_mutex);
52  if(m_maxCacheEntries<=0) return;
53  CacheType::const_iterator iter = m_cache.find(key);
54  if(iter != m_cache.end())
55  {
56  iter->second->m_filesize = node->m_filesize;
57  touchEntryProtected(key);
58  }
59  else
60  {
61  if(m_cache.size() >= m_maxCacheEntries)
62  {
63  shrinkEntries();
64  }
65  node->m_timestamp = ossimTimer::instance()->tick();
66  m_cache.insert( std::make_pair(key, node) );
67  m_cacheTime.insert( std::make_pair(node->m_timestamp, key) );
68  }
69 }
70 
72 {
73  // I think unique lock is recursive but need to test first
74  // So for now we will wrap with a touchEntryProtected that does not lock
75  // so all public methods are locking.
76  //
77  std::unique_lock<std::mutex> lock(m_mutex);
78  touchEntryProtected(key);
79 }
80 
82 {
83  CacheType::iterator iter = m_cache.find(key);
84  if(iter!=m_cache.end())
85  {
86  CacheTimeIndexType::iterator cacheTimeIter = m_cacheTime.find(iter->second->m_timestamp);
87  if(cacheTimeIter!=m_cacheTime.end())
88  {
89  m_cacheTime.erase(cacheTimeIter);
90  iter->second->m_timestamp = ossimTimer::instance()->tick();
91  m_cacheTime.insert( std::make_pair(iter->second->m_timestamp, key) );
92  }
93  }
94 }
95 
97 {
98  ossim_int64 targetSize = static_cast<ossim_int64>(0.2*m_maxCacheEntries);
99 
100  if(m_cacheTime.empty()) return;
101  if(targetSize >= m_cache.size())
102  {
103  m_cache.clear();
104  m_cacheTime.clear();
105  return;
106  }
107 
108  CacheTimeIndexType::iterator iter = m_cacheTime.begin();
109 
110  while(targetSize > 0)
111  {
112  m_cache.erase(iter->second);
113  iter = m_cacheTime.erase(iter);
114  --targetSize;
115  }
116 }
117 
static std::shared_ptr< CurlHeaderCache > m_instance
This code was derived from https://gist.github.com/mshockwave.
Definition: Barrier.h:8
static std::shared_ptr< CurlHeaderCache > instance()
static ossimTimer * instance()
Definition: ossimTimer.cpp:19
std::shared_ptr< CurlHeaderCacheNode > Node_t
void touchEntryProtected(const Key_t &key)
bool getCachedFilesize(const Key_t &key, ossim_int64 &filesize) const
void touchEntry(const Key_t &key)
long long ossim_int64
void addHeader(const Key_t &key, Node_t &node)
Timer_t tick() const
Get the timers tick value.
Definition: ossimTimer.cpp:95