5G-MAG Reference Tools - MBMS Middleware
CacheManagement.cpp
Go to the documentation of this file.
1 // 5G-MAG Reference Tools
2 // MBMS Middleware Process
3 //
4 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
5 //
6 // Licensed under the License terms and conditions for use, reproduction, and
7 // distribution of 5G-MAG software (the “License”). You may not use this file
8 // except in compliance with the License. You may obtain a copy of the License at
9 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
10 // agreed to in writing, software distributed under the License is distributed on
11 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 // or implied.
13 //
14 // See the License for the specific language governing permissions and limitations
15 // under the License.
16 //
17 
18 #include "CacheManagement.h"
19 #include "spdlog/spdlog.h"
20 
21 MBMS_RT::CacheManagement::CacheManagement(const libconfig::Config& cfg, boost::asio::io_service& io_service)
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 }
28 
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 }
CacheManagement(const libconfig::Config &cfg, boost::asio::io_service &io_service)
static Config cfg
Global configuration object.
Definition: main.cpp:111