hueplusplus  1.0.0
APICache.h
Go to the documentation of this file.
1 
23 #ifndef INCLUDE_API_CACHE_H
24 #define INCLUDE_API_CACHE_H
25 
26 #include <chrono>
27 #include <string>
28 
29 #include "HueCommandAPI.h"
30 
31 namespace hueplusplus
32 {
34 constexpr std::chrono::steady_clock::duration c_refreshNever = std::chrono::steady_clock::duration::max();
35 
37 class APICache
38 {
39 public:
47  APICache(
48  std::shared_ptr<APICache> baseCache, const std::string& subEntry, std::chrono::steady_clock::duration refresh);
49 
55  APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh,
56  const nlohmann::json& initial);
57 
65  void refresh();
66 
72  nlohmann::json& getValue();
75  const nlohmann::json& getValue() const;
76 
84  void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration);
85 
87  std::chrono::steady_clock::duration getRefreshDuration() const;
88 
92  const HueCommandAPI& getCommandAPI() const;
93 
96  std::string getRequestPath() const;
97 
98 private:
99  bool needsRefresh();
100 
101 private:
102  std::shared_ptr<APICache> base;
103  std::string path;
104  HueCommandAPI commands;
105  std::chrono::steady_clock::duration refreshDuration;
106  std::chrono::steady_clock::time_point lastRefresh;
107  nlohmann::json value;
108 };
109 } // namespace hueplusplus
110 
111 #endif
Definition: HueCommandAPI.h:37
HueCommandAPI & getCommandAPI()
Get HueCommandAPI used for requests.
Definition: APICache.cpp:127
Namespace for the hueplusplus library.
Definition: Action.h:27
Caches API GET requests and refreshes regularly.
Definition: APICache.h:37
void refresh()
Refresh cache now.
Definition: APICache.cpp:47
APICache(std::shared_ptr< APICache > baseCache, const std::string &subEntry, std::chrono::steady_clock::duration refresh)
Constructs APICache which forwards to a base cache.
Definition: APICache.cpp:29
void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration)
Set duration after which the cache is refreshed.
Definition: APICache.cpp:117
constexpr std::chrono::steady_clock::duration c_refreshNever
Maximum duration, used to indicate that the cache should never be refreshed automatically.
Definition: APICache.h:34
nlohmann::json & getValue()
Get cached value, refresh if necessary.
Definition: APICache.cpp:69
std::string getRequestPath() const
Get path the cache is refreshed from.
Definition: APICache.cpp:166
std::chrono::steady_clock::duration getRefreshDuration() const
Get duration between refreshes.
Definition: APICache.cpp:122