Loading...
Searching...
No Matches
hueplusplus::APICache Class Reference

Caches API GET requests and refreshes regularly. More...

`#include <hueplusplus/APICache.h>`

## Public Member Functions

| APICache (std::shared_ptr< APICache > baseCache, const std::string &subEntry, std::chrono::steady_clock::duration refresh)
| Constructs APICache which forwards to a base cache.
| APICache (const std::string &path, const HueCommandAPI &commands, std::chrono::steady_clock::duration refresh, const nlohmann::json &initial)
| Constructs APICache with an own internal json cache.
void | refresh ()
| Refresh cache now.
nlohmann::json & | getValue ()
| Get cached value, refresh if necessary.
const nlohmann::json & | getValue () const
| Get cached value, does not refresh.
void | setRefreshDuration (std::chrono::steady_clock::duration refreshDuration)
| Set duration after which the cache is refreshed.
std::chrono::steady_clock::duration | getRefreshDuration () const
| Get duration between refreshes.
HueCommandAPI & | getCommandAPI ()
| Get HueCommandAPI used for requests.
const HueCommandAPI & | getCommandAPI () const
| Get HueCommandAPI used for requests.
std::string | getRequestPath () const
| Get path the cache is refreshed from.

## Detailed Description

Caches API GET requests and refreshes regularly.

## Constructor & Destructor Documentation

## APICache() [1/2]

hueplusplus::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.

Parameters

  baseCache | Base cache providing a parent state, must not be nullptr
  subEntry | Key of the child to use in the base cache
  refresh | Interval between cache refreshing. May be 0 to always refresh. This is independent from the base cache refresh rate.

Refreshes only part of the base cache.

## APICache() [2/2]

hueplusplus::APICache::APICache | ( | const std::string & | path,
|  | const HueCommandAPI & | commands,
|  | std::chrono::steady_clock::duration | refresh,
|  | const nlohmann::json & | initial
| ) |  |

Constructs APICache with an own internal json cache.

Parameters

  path | URL appended after username, may be empty.
  commands | HueCommandAPI for making API requests.
  refresh | Interval between cache refreshing. May be 0 to always refresh.
  initial | Initial value, may be null. If present, assumes the value is up to date.

## Member Function Documentation

## getCommandAPI() [1/2]

HueCommandAPI & hueplusplus::APICache::getCommandAPI | ( |  | ) |

Get HueCommandAPI used for requests.

## getCommandAPI() [2/2]

const HueCommandAPI & hueplusplus::APICache::getCommandAPI | ( |  | ) | const

Get HueCommandAPI used for requests.

## getRefreshDuration()

std::chrono::steady_clock::duration hueplusplus::APICache::getRefreshDuration | ( |  | ) | const

Get duration between refreshes.

## getRequestPath()

std::string hueplusplus::APICache::getRequestPath | ( |  | ) | const

Get path the cache is refreshed from.

Returns
  Request path as passed to HueCommandAPI::GETRequest

## getValue() [1/2]

nlohmann::json & hueplusplus::APICache::getValue | ( |  | ) |

Get cached value, refresh if necessary.

Exceptions

  std::system_error | when system or socket operations fail
  HueException | when response contained no body
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when response could not be parsed

## getValue() [2/2]

const nlohmann::json & hueplusplus::APICache::getValue | ( |  | ) | const

Get cached value, does not refresh.

Exceptions

  HueException | when no previous request was cached

## refresh()

void hueplusplus::APICache::refresh | ( |  | ) |

Refresh cache now.

Exceptions

  std::system_error | when system or socket operations fail
  HueException | when response contained no body
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when response could not be parsed

If there is a base cache, refreshes only the used part of that cache.

## setRefreshDuration()

void hueplusplus::APICache::setRefreshDuration | ( | std::chrono::steady_clock::duration | refreshDuration | ) |

Set duration after which the cache is refreshed.

Parameters

  refreshDuration | Interval between cache refreshing. May be 0 to always refresh, or c_refreshNever to never refresh.

If the new refresh duration is exceeded, does not refresh immediately. Instead, the next non-const getValue() call will refresh the value. This is to reduce the number of unneccessary requests.

The documentation for this class was generated from the following files:

- include/hueplusplus/APICache.h
- src/APICache.cpp