5G-MAG Reference Tools - MBMS Middleware
Public Member Functions | Private Attributes | List of all members
MBMS_RT::CacheManagement Class Reference

#include <CacheManagement.h>

Collaboration diagram for MBMS_RT::CacheManagement:
Collaboration graph

Public Member Functions

 CacheManagement (const libconfig::Config &cfg, boost::asio::io_service &io_service)
 
virtual ~CacheManagement ()=default
 
void add_item (std::shared_ptr< CacheItem > item)
 
void remove_item (const std::string &location)
 
const std::map< std::string, std::shared_ptr< CacheItem > > & item_map () const
 
void check_file_expiry_and_cache_size ()
 

Private Attributes

std::map< std::string, std::shared_ptr< CacheItem > > _cache_items
 
unsigned _max_cache_size = 512
 
unsigned _total_cache_size = 0
 
unsigned _max_cache_file_age = 30
 
boost::asio::io_service & _io_service
 

Detailed Description

Definition at line 25 of file CacheManagement.h.

Constructor & Destructor Documentation

◆ CacheManagement()

MBMS_RT::CacheManagement::CacheManagement ( const libconfig::Config &  cfg,
boost::asio::io_service &  io_service 
)

Definition at line 21 of file CacheManagement.cpp.

22  : _io_service(io_service)
23 {
24  cfg.lookupValue("mw.cache.max_total_size", _max_cache_size);
25  _max_cache_size *= 1024 * 1024;
26  cfg.lookupValue("mw.cache.max_file_age", _max_cache_file_age);
27 }
boost::asio::io_service & _io_service
static Config cfg
Global configuration object.
Definition: main.cpp:111

◆ ~CacheManagement()

virtual MBMS_RT::CacheManagement::~CacheManagement ( )
virtualdefault

Member Function Documentation

◆ add_item()

void MBMS_RT::CacheManagement::add_item ( std::shared_ptr< CacheItem item)
inline

Definition at line 30 of file CacheManagement.h.

30  {
31  _cache_items[item->content_location()] = item;
32  };
std::map< std::string, std::shared_ptr< CacheItem > > _cache_items

◆ check_file_expiry_and_cache_size()

auto MBMS_RT::CacheManagement::check_file_expiry_and_cache_size ( )

Definition at line 29 of file CacheManagement.cpp.

30 {
31  std::multimap<unsigned, std::string> items_by_age;
32  for (auto it = _cache_items.cbegin(); it != _cache_items.cend();) {
33  spdlog::debug("checking {}", it->second->content_location());
34  if (it->second->received_at() != 0) {
35  auto age = time(nullptr) - it->second->received_at();
36  if (age > _max_cache_file_age) {
37  spdlog::info("Cache management deleting expired item at {} after {} seconds",
38  it->second->content_location(), age);
39  it = _cache_items.erase(it);
40  } else {
41  items_by_age.insert(std::make_pair(age, it->first));
42  ++it;
43  }
44  } else {
45  ++it;
46  }
47  }
48  uint32_t total_size = 0;
49  for (const auto& it : items_by_age) {
50  total_size += _cache_items[it.second]->content_length();
51  if (total_size > _max_cache_size) {
52  spdlog::info("Cache management deleting item at {} (aged {} secs) due to cache size limit",
53  it.second, it.first);
54  _cache_items.erase(it.second);
55  }
56  }
57 }

◆ item_map()

const std::map<std::string, std::shared_ptr<CacheItem> >& MBMS_RT::CacheManagement::item_map ( ) const
inline

Definition at line 34 of file CacheManagement.h.

34 { return _cache_items; };

◆ remove_item()

void MBMS_RT::CacheManagement::remove_item ( const std::string &  location)
inline

Definition at line 33 of file CacheManagement.h.

33 { _cache_items.erase(location); };

Member Data Documentation

◆ _cache_items

std::map<std::string, std::shared_ptr<CacheItem> > MBMS_RT::CacheManagement::_cache_items
private

Definition at line 40 of file CacheManagement.h.

◆ _io_service

boost::asio::io_service& MBMS_RT::CacheManagement::_io_service
private

Definition at line 44 of file CacheManagement.h.

◆ _max_cache_file_age

unsigned MBMS_RT::CacheManagement::_max_cache_file_age = 30
private

Definition at line 43 of file CacheManagement.h.

◆ _max_cache_size

unsigned MBMS_RT::CacheManagement::_max_cache_size = 512
private

Definition at line 41 of file CacheManagement.h.

◆ _total_cache_size

unsigned MBMS_RT::CacheManagement::_total_cache_size = 0
private

Definition at line 42 of file CacheManagement.h.


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