Loading...
Searching...
No Matches
APICache.h
Go to the documentation of this file.
#ifndef INCLUDE_API_CACHE_H
#define INCLUDE_API_CACHE_H
#include <chrono>
#include <string>
#include "HueCommandAPI.h"
namespace hueplusplus
{
constexpr std::chrono::steady_clock::duration c_refreshNever = std::chrono::steady_clock::duration::max();
class APICache
{
public:
APICache(
std::shared_ptr<APICache> baseCache, const std::string& subEntry, std::chrono::steady_clock::duration refresh);
APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh,
const nlohmann::json& initial);
void refresh();
nlohmann::json& getValue();
const nlohmann::json& getValue() const;
void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration);
std::chrono::steady_clock::duration getRefreshDuration() const;
HueCommandAPI& getCommandAPI();
const HueCommandAPI& getCommandAPI() const;
std::string getRequestPath() const;
private:
bool needsRefresh();
private:
std::shared_ptr<APICache> base;
std::string path;
HueCommandAPI commands;
std::chrono::steady_clock::duration refreshDuration;
std::chrono::steady_clock::time_point lastRefresh;
nlohmann::json value;
};
} // namespace hueplusplus
#endif
HueCommandAPI.h
hueplusplus::APICache::getRefreshDuration
std::chrono::steady_clock::duration getRefreshDuration() const
Get duration between refreshes.
Definition APICache.cpp:122
hueplusplus::APICache::setRefreshDuration
void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration)
Set duration after which the cache is refreshed.
Definition APICache.cpp:117
hueplusplus::APICache::refresh
void refresh()
Refresh cache now.
Definition APICache.cpp:47
hueplusplus::APICache::APICache
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
hueplusplus::APICache::getRequestPath
std::string getRequestPath() const
Get path the cache is refreshed from.
Definition APICache.cpp:166
hueplusplus::APICache::getCommandAPI
HueCommandAPI & getCommandAPI()
Get HueCommandAPI used for requests.
Definition APICache.cpp:127
hueplusplus::APICache::getValue
nlohmann::json & getValue()
Get cached value, refresh if necessary.
Definition APICache.cpp:69
hueplusplus::HueCommandAPI
Definition HueCommandAPI.h:38
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28
hueplusplus::c_refreshNever
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