OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
ossim::CurlHeaderCache Class Reference

#include <CurlHeaderCache.h>

Public Types

typedef std::shared_ptr< CurlHeaderCacheNodeNode_t
 
typedef std::string Key_t
 
typedef ossim_int64 TimeIndex_t
 
typedef std::map< Key_t, Node_tCacheType
 
typedef std::map< TimeIndex_t, Key_tCacheTimeIndexType
 

Public Member Functions

 CurlHeaderCache ()
 
virtual ~CurlHeaderCache ()
 
bool getCachedFilesize (const Key_t &key, ossim_int64 &filesize) const
 
void addHeader (const Key_t &key, Node_t &node)
 
void setMaxCacheEntries (ossim_int64 maxEntries)
 
void touchEntry (const Key_t &key)
 

Static Public Member Functions

static std::shared_ptr< CurlHeaderCacheinstance ()
 

Protected Member Functions

void shrinkEntries ()
 
void touchEntryProtected (const Key_t &key)
 

Protected Attributes

std::mutex m_mutex
 
CacheType m_cache
 
CacheTimeIndexType m_cacheTime
 
ossim_int64 m_maxCacheEntries
 

Static Protected Attributes

static std::shared_ptr< CurlHeaderCachem_instance
 

Detailed Description

Definition at line 27 of file CurlHeaderCache.h.

Member Typedef Documentation

◆ CacheTimeIndexType

Definition at line 34 of file CurlHeaderCache.h.

◆ CacheType

Definition at line 33 of file CurlHeaderCache.h.

◆ Key_t

typedef std::string ossim::CurlHeaderCache::Key_t

Definition at line 31 of file CurlHeaderCache.h.

◆ Node_t

Definition at line 30 of file CurlHeaderCache.h.

◆ TimeIndex_t

Definition at line 32 of file CurlHeaderCache.h.

Constructor & Destructor Documentation

◆ CurlHeaderCache()

ossim::CurlHeaderCache::CurlHeaderCache ( )

Definition at line 7 of file CurlHeaderCache.cpp.

◆ ~CurlHeaderCache()

ossim::CurlHeaderCache::~CurlHeaderCache ( )
virtual

Definition at line 13 of file CurlHeaderCache.cpp.

14 {
15  m_cache.clear();
16 }

Member Function Documentation

◆ addHeader()

void ossim::CurlHeaderCache::addHeader ( const Key_t key,
Node_t node 
)

Definition at line 49 of file CurlHeaderCache.cpp.

References ossimTimer::instance(), and ossimTimer::tick().

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;
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 }
static ossimTimer * instance()
Definition: ossimTimer.cpp:19
ossim_int64 m_maxCacheEntries
void touchEntryProtected(const Key_t &key)
Timer_t tick() const
Get the timers tick value.
Definition: ossimTimer.cpp:95
CacheTimeIndexType m_cacheTime

◆ getCachedFilesize()

bool ossim::CurlHeaderCache::getCachedFilesize ( const Key_t key,
ossim_int64 filesize 
) const

Definition at line 28 of file CurlHeaderCache.cpp.

References touchEntryProtected().

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 }
ossim_int64 m_maxCacheEntries
void touchEntryProtected(const Key_t &key)

◆ instance()

std::shared_ptr< ossim::CurlHeaderCache > ossim::CurlHeaderCache::instance ( )
static

Definition at line 18 of file CurlHeaderCache.cpp.

19 {
20  if(!m_instance)
21  {
22  m_instance = std::make_shared<CurlHeaderCache>();
23  }
24 
25  return m_instance;
26 }
static std::shared_ptr< CurlHeaderCache > m_instance

◆ setMaxCacheEntries()

void ossim::CurlHeaderCache::setMaxCacheEntries ( ossim_int64  maxEntries)

◆ shrinkEntries()

void ossim::CurlHeaderCache::shrinkEntries ( )
protected

Definition at line 96 of file CurlHeaderCache.cpp.

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 }
ossim_int64 m_maxCacheEntries
long long ossim_int64
CacheTimeIndexType m_cacheTime

◆ touchEntry()

void ossim::CurlHeaderCache::touchEntry ( const Key_t key)

Definition at line 71 of file CurlHeaderCache.cpp.

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);
79 }
void touchEntryProtected(const Key_t &key)

◆ touchEntryProtected()

void ossim::CurlHeaderCache::touchEntryProtected ( const Key_t key)
protected

Definition at line 81 of file CurlHeaderCache.cpp.

References ossimTimer::instance(), and ossimTimer::tick().

Referenced by getCachedFilesize().

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 }
static ossimTimer * instance()
Definition: ossimTimer.cpp:19
Timer_t tick() const
Get the timers tick value.
Definition: ossimTimer.cpp:95
CacheTimeIndexType m_cacheTime

Member Data Documentation

◆ m_cache

CacheType ossim::CurlHeaderCache::m_cache
protected

Definition at line 52 of file CurlHeaderCache.h.

◆ m_cacheTime

CacheTimeIndexType ossim::CurlHeaderCache::m_cacheTime
protected

Definition at line 53 of file CurlHeaderCache.h.

◆ m_instance

std::shared_ptr< ossim::CurlHeaderCache > ossim::CurlHeaderCache::m_instance
staticprotected

Definition at line 49 of file CurlHeaderCache.h.

◆ m_maxCacheEntries

ossim_int64 ossim::CurlHeaderCache::m_maxCacheEntries
protected

Definition at line 54 of file CurlHeaderCache.h.

◆ m_mutex

std::mutex ossim::CurlHeaderCache::m_mutex
mutableprotected

Definition at line 50 of file CurlHeaderCache.h.


The documentation for this class was generated from the following files: