blob: b3dec23958fa8e7232d7e8976d906c515093b357 (
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#pragma once
#include "partners_api/booking_api.hpp"
#include "base/macros.hpp"
#include <chrono>
#include <map>
#include <optional>
#include <utility>
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 Info
{
Info() = default;
explicit Info(HotelStatus status) : m_status(status) {}
Info(HotelStatus status, Extras const & extras) : m_status(status), m_extras(extras) {}
HotelStatus m_status = HotelStatus::Absent;
std::optional<Extras> m_extras;
};
Cache() = default;
Cache(size_t maxCount, size_t expiryPeriodSeconds);
Info Get(std::string const & hotelId);
// Removes items from available and unavailable
// in case of (current items count + |count|) >= |m_maxCount|.
void ReserveAdditional(size_t count);
void InsertNotReady(std::string const & hotelId);
void InsertUnavailable(std::string const & hotelId);
void InsertAvailable(std::string const & hotelId, Extras && extras);
void RemoveOutdated();
void Clear();
private:
struct Item
{
Item() = default;
explicit Item(Extras && extras) : m_extras(std::move(extras)) {}
Clock::time_point m_timestamp = Clock::now();
Extras m_extras;
};
using HotelWithTimestampMap = std::map<std::string, Clock::time_point>;
using HotelWithExtrasMap = std::map<std::string, Item>;
// Removes items in case size >= |m_maxCount|.
template <typename Container>
void ReserveAdditional(Container & container, size_t additionalCount);
HotelWithTimestampMap m_notReadyHotels;
HotelWithTimestampMap m_unavailableHotels;
HotelWithExtrasMap m_availableHotels;
// Max count of |m_availableHotels| or |m_unavailableHotels| container.
// Count is unlimited when |m_maxCount| is equal to zero.
size_t const m_maxCount = 3000;
// 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
|