blob: 78064df4fe3665bbaea8c120a3893dfcd3e8324b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#pragma once
#include "base/macros.hpp"
#include <chrono>
#include <map>
namespace booking
{
namespace filter
{
namespace availability
{
class Cache
{
public:
enum class HotelStatus
{
// The hotel is absent in cache.
Absent,
// Information about the hotel was requested, but request is not ready yet.
NotReady,
// The hotel is unavailable for booking.
Unavailable,
// The hotel is available for booking.
Available,
};
using Clock = std::chrono::steady_clock;
struct Item
{
Item() = default;
explicit Item(HotelStatus s) : m_status(s) {}
Clock::time_point m_timestamp = Clock::now();
HotelStatus m_status = HotelStatus::NotReady;
};
Cache() = default;
Cache(size_t maxCount, size_t expiryPeriodSeconds);
HotelStatus Get(std::string const & hotelId);
void Reserve(std::string const & hotelId);
void Insert(std::string const & hotelId, HotelStatus const s);
void RemoveOutdated();
void Clear();
private:
using HotelsMap = std::map<std::string, Item>;
// In case when size >= |m_maxCount| removes items except those who have the status
// HotelStatus::NotReady.
void RemoveExtra();
bool IsExpired(Clock::time_point const & timestamp) const;
HotelStatus Get(HotelsMap & src, std::string const & hotelId);
void RemoveOutdated(HotelsMap & src);
HotelsMap m_hotelToStatus;
HotelsMap m_notReadyHotels;
// Max count of |m_hotelToStatus| container.
// Count is unlimited when |m_maxCount| is equal to zero.
size_t const m_maxCount = 5000;
// Do not use aging when |m_expiryPeriodSeconds| is equal to zero.
size_t const m_expiryPeriodSeconds = 300;
};
std::string DebugPrint(Cache::HotelStatus status);
} // namespace availability
} // namespace filter
} // namespace booking
|