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

#include <TableFunctionsCommon.hpp>

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.hpp.

Member Function Documentation

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

Definition at line 539 of file TableFunctionsCommon.cpp.

539  {
540  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
541  const auto& cached_data_itr = data_cache_.find(key);
542  if (cached_data_itr == data_cache_.end()) {
543  const std::string error_msg{"Data for key " + key + " not found in cache."};
544  throw std::runtime_error(error_msg);
545  }
546  return cached_data_itr->second;
547 }
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 533 of file TableFunctionsCommon.cpp.

533  {
534  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
535  return data_cache_.count(key) > 0;
536 }
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 550 of file TableFunctionsCommon.cpp.

550  {
551  std::unique_lock<std::shared_mutex> write_lock(cache_mutex_);
552  const auto& cached_data_itr = data_cache_.find(key);
553  if (cached_data_itr != data_cache_.end()) {
554  const std::string warning_msg =
555  "Data for key " + key + " already exists in cache. Replacing.";
556  std::cout << warning_msg << std::endl;
557  cached_data_itr->second.reset();
558  cached_data_itr->second = data;
559  }
560  data_cache_.insert(std::make_pair(key, data));
561 }
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.hpp.

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

Definition at line 136 of file TableFunctionsCommon.hpp.


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