OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataCache< T > Class Template Reference

#include <TableFunctionsDataCache.h>

Public Member Functions

bool isKeyCached (const std::string &key) const
 
std::shared_ptr< T > getDataForKey (const std::string &key) const
 
void putDataForKey (const std::string &key, std::shared_ptr< T > const data)
 

Private Attributes

std::unordered_map
< std::string, std::shared_ptr
< T > > 
data_cache_
 
std::shared_mutex cache_mutex_
 

Static Private Attributes

static constexpr bool debug_print_ {false}
 

Detailed Description

template<typename T>
class DataCache< T >

Definition at line 145 of file TableFunctionsDataCache.h.

Member Function Documentation

template<typename T >
std::shared_ptr<T> DataCache< T >::getDataForKey ( const std::string &  key) const
inline

Definition at line 152 of file TableFunctionsDataCache.h.

References DataCache< T >::cache_mutex_, and DataCache< T >::data_cache_.

152  {
153  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
154  const auto& cached_data_itr = data_cache_.find(key);
155  if (cached_data_itr == data_cache_.end()) {
156  const std::string error_msg{"Data for key " + key + " not found in cache."};
157  throw std::runtime_error(error_msg);
158  }
159  return cached_data_itr->second;
160  }
std::unordered_map< std::string, std::shared_ptr< T > > data_cache_
std::shared_mutex cache_mutex_
template<typename T >
bool DataCache< T >::isKeyCached ( const std::string &  key) const
inline

Definition at line 147 of file TableFunctionsDataCache.h.

References DataCache< T >::cache_mutex_, and DataCache< T >::data_cache_.

147  {
148  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
149  return data_cache_.count(key) > 0;
150  }
std::unordered_map< std::string, std::shared_ptr< T > > data_cache_
std::shared_mutex cache_mutex_
template<typename T >
void DataCache< T >::putDataForKey ( const std::string &  key,
std::shared_ptr< T > const  data 
)
inline

Definition at line 162 of file TableFunctionsDataCache.h.

References DataCache< T >::cache_mutex_, DataCache< T >::data_cache_, and DataCache< T >::debug_print_.

162  {
163  std::unique_lock<std::shared_mutex> write_lock(cache_mutex_);
164  const auto& cached_data_itr = data_cache_.find(key);
165  if (cached_data_itr != data_cache_.end()) {
166  if constexpr (debug_print_) {
167  const std::string warning_msg =
168  "Data for key " + key + " already exists in cache. Replacing.";
169  std::cout << warning_msg << std::endl;
170  }
171  cached_data_itr->second.reset();
172  cached_data_itr->second = data;
173  }
174  data_cache_.insert(std::make_pair(key, data));
175  }
std::unordered_map< std::string, std::shared_ptr< T > > data_cache_
static constexpr bool debug_print_
std::shared_mutex cache_mutex_

Member Data Documentation

template<typename T >
std::shared_mutex DataCache< T >::cache_mutex_
mutableprivate
template<typename T >
std::unordered_map<std::string, std::shared_ptr<T> > DataCache< T >::data_cache_
private
template<typename T >
constexpr bool DataCache< T >::debug_print_ {false}
staticprivate

Definition at line 180 of file TableFunctionsDataCache.h.

Referenced by DataCache< T >::putDataForKey().


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