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

#include <TableFunctionsCommon.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_
 

Detailed Description

template<typename T>
class DataCache< T >

Definition at line 127 of file TableFunctionsCommon.h.

Member Function Documentation

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

Definition at line 532 of file TableFunctionsCommon.cpp.

532  {
533  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
534  const auto& cached_data_itr = data_cache_.find(key);
535  if (cached_data_itr == data_cache_.end()) {
536  const std::string error_msg{"Data for key " + key + " not found in cache."};
537  throw std::runtime_error(error_msg);
538  }
539  return cached_data_itr->second;
540 }
heavyai::shared_lock< heavyai::shared_mutex > read_lock
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

Definition at line 526 of file TableFunctionsCommon.cpp.

526  {
527  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
528  return data_cache_.count(key) > 0;
529 }
heavyai::shared_lock< heavyai::shared_mutex > read_lock
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 
)

Definition at line 543 of file TableFunctionsCommon.cpp.

543  {
544  std::unique_lock<std::shared_mutex> write_lock(cache_mutex_);
545  const auto& cached_data_itr = data_cache_.find(key);
546  if (cached_data_itr != data_cache_.end()) {
547  const std::string warning_msg =
548  "Data for key " + key + " already exists in cache. Replacing.";
549  std::cout << warning_msg << std::endl;
550  cached_data_itr->second.reset();
551  cached_data_itr->second = data;
552  }
553  data_cache_.insert(std::make_pair(key, data));
554 }
std::unordered_map< std::string, std::shared_ptr< T > > data_cache_
heavyai::unique_lock< heavyai::shared_mutex > write_lock
std::shared_mutex cache_mutex_

Member Data Documentation

template<typename T >
std::shared_mutex DataCache< T >::cache_mutex_
mutableprivate

Definition at line 137 of file TableFunctionsCommon.h.

template<typename T >
std::unordered_map<std::string, std::shared_ptr<T> > DataCache< T >::data_cache_
private

Definition at line 136 of file TableFunctionsCommon.h.


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