Fetch web resources caching them in the local filesystem following XDG specifications.
More...
#include <WebCache.h>
|
using | key_t = uint32_t |
|
using | local_id_t = std::string |
|
using | result_t = std::tuple< long, key_t > |
|
using | item_map_t = std::map< key_t, local_id_t > |
|
using | AsyncList = std::vector< std::future< result_t > > |
|
|
| WebCache (const std::string &rootUri, const path &xdgDir, const std::string &storeRoot, std::chrono::system_clock::duration duration) |
| Constructor. More...
|
|
template<typename It > |
| WebCache (const std::string &rootUri, const path &xdgDir, const std::string &storeRoot, std::chrono::system_clock::duration duration, It first, It last) |
|
void | setCacheItem (key_t key, local_id_t localId) |
| Add or change a cache item. More...
|
|
template<typename It > |
void | setCacheItem (It first, It last) |
| Add or change cache items from a container. More...
|
|
bool | storeExists () const |
| Test to see if the store exists. More...
|
|
virtual std::string | translateItemLocalId (local_id_t localId) |
| Translate a local id. More...
|
|
path | itemLocalPath (key_t key) |
| Get the filesystem path of a local item by key. More...
|
|
std::optional< path > | localItemExists (key_t key) |
| Get the local path to an item if it is known and exists in the local store. More...
|
|
std::optional< time_t > | cacheTime (const path &itemPath) |
| Get the time the item was cached if the cache time has expired. More...
|
|
virtual std::string | constructUrl (const local_id_t &localId) |
| Construct the appropriate URL for the item. More...
|
|
bool | itemKnown (key_t key) |
|
bool | fetchAll () |
| Fetch all cache items which have not been previously cached or have expired cache times.
|
|
bool | fetchItem (key_t key) |
|
bool | pendingFutures () const |
|
bool | processFutures () |
|
|
template<typename T > |
static std::chrono::system_clock::time_point | fileClockToSystemClock (T fileTimePoint) |
| Convert a filesystem time to a system clock time point. More...
|
|
static std::chrono::system_clock::time_point | fileClockToSystemClock (const path &filePath) |
| Convert a filesystem time to a system clock time point. More...
|
|
static WebCache::result_t | fetch (WebCache::key_t key, const std::string &itemUrl, const path &itemPath, const path &tempPath, std::optional< time_t > cacheFileTime) |
| Fetch a cache item. More...
|
|
|
void | asyncFetchItem (item_map_t::value_type &item) |
|
|
std::error_code | mEc {} |
| The last error code returned from a std::filesystem operation.
|
|
std::string | mRootURI {} |
| The root URI for items in the cache.
|
|
path | mStoreRoot {} |
| The root of the filesystem backing store.
|
|
file_status | mStoreStatus {} |
| File status of the filesystem backing store root.
|
|
std::mutex | mMutex |
| Mutex for locking the WebFileCache.
|
|
item_map_t | mItemMap {} |
| The map of keys to local cache items.
|
|
AsyncList | mAsyncList {} |
| The list of asynchronous fetches active.
|
|
std::chrono::system_clock::duration | mCacheValidDuration {} |
| The duration local files are considered valid. The interval between cache refresh checks.
|
|
Fetch web resources caching them in the local filesystem following XDG specifications.
◆ WebCache()
rose::WebCache::WebCache |
( |
const std::string & |
rootUri, |
|
|
const path & |
xdgDir, |
|
|
const std::string & |
storeRoot, |
|
|
std::chrono::system_clock::duration |
duration |
|
) |
| |
Constructor.
- Parameters
-
rootUri | The base URI locating all the items manage by the cache. |
xdgDir | The XDG compliant specification for the user caching directory. See rose::Environment. |
storeRoot | A relative path from xdgDir that this cache will use. |
duration | The period of time cache items should be considered valid before being reloaded. |
◆ cacheTime()
std::optional<time_t> rose::WebCache::cacheTime |
( |
const path & |
itemPath | ) |
|
|
inline |
Get the time the item was cached if the cache time has expired.
- Parameters
-
- Returns
- A time_t expressing the last write time of the local file if cache time has expired, an empty std::optional otherwise.
◆ constructUrl()
virtual std::string rose::WebCache::constructUrl |
( |
const local_id_t & |
localId | ) |
|
|
inlinevirtual |
Construct the appropriate URL for the item.
Default procedure is to append the local id to the rout URI.
- Parameters
-
- Returns
- The constructed URL.
Reimplemented in rose::ClearSkyEphemeris, and ClearSkyEphemeris.
◆ fetch()
WebCache::result_t rose::WebCache::fetch |
( |
WebCache::key_t |
key, |
|
|
const std::string & |
itemUrl, |
|
|
const path & |
itemPath, |
|
|
const path & |
tempPath, |
|
|
std::optional< time_t > |
cacheFileTime |
|
) |
| |
|
static |
Fetch a cache item.
- Parameters
-
key | The item key. |
itemUrl | The item url (returned by constructUrl()). |
itemPath | The full item path. |
cacheFileTime | The std::optional returned by cacheTime(). |
- Returns
- A tuple containing the returned HTTP status code and the item key.
◆ fileClockToSystemClock() [1/2]
template<typename T >
static std::chrono::system_clock::time_point rose::WebCache::fileClockToSystemClock |
( |
T |
fileTimePoint | ) |
|
|
inlinestatic |
Convert a filesystem time to a system clock time point.
- Template Parameters
-
T | The type of the file time point. |
- Parameters
-
fileTimePoint | The value of the file time point. |
- Returns
- std::chrono::system_clock::time_point.
◆ fileClockToSystemClock() [2/2]
static std::chrono::system_clock::time_point rose::WebCache::fileClockToSystemClock |
( |
const path & |
filePath | ) |
|
|
inlinestatic |
Convert a filesystem time to a system clock time point.
- Parameters
-
filePath | The filesystem path to get the last write time from. |
- Returns
- std::chrono::system_clock::time_point.
◆ itemLocalPath()
path rose::WebCache::itemLocalPath |
( |
key_t |
key | ) |
|
|
inline |
Get the filesystem path of a local item by key.
- Parameters
-
key | The key to search for. |
- Returns
- The translated local id. See translateItemLocalId().
◆ localItemExists()
std::optional<path> rose::WebCache::localItemExists |
( |
key_t |
key | ) |
|
|
inline |
Get the local path to an item if it is known and exists in the local store.
- Parameters
-
- Returns
- std::optional<path> with the file path if it exists.
◆ setCacheItem() [1/2]
void rose::WebCache::setCacheItem |
( |
key_t |
key, |
|
|
local_id_t |
localId |
|
) |
| |
|
inline |
Add or change a cache item.
- Parameters
-
key | The key to identify the cache item. |
localId | The local identity in the filesystem cache, the file name. |
◆ setCacheItem() [2/2]
template<typename It >
void rose::WebCache::setCacheItem |
( |
It |
first, |
|
|
It |
last |
|
) |
| |
|
inline |
Add or change cache items from a container.
- Template Parameters
-
- Parameters
-
first | The first item iterator. |
last | The last item iterator. |
◆ storeExists()
bool rose::WebCache::storeExists |
( |
| ) |
const |
|
inline |
Test to see if the store exists.
- Returns
- True if it exits and is a directory.
◆ translateItemLocalId()
virtual std::string rose::WebCache::translateItemLocalId |
( |
local_id_t |
localId | ) |
|
|
inlinevirtual |
Translate a local id.
This provides a method for derived cache types to modify the local name to something more suitable to the backing filesystem including prepending a relative path. The default is to do nothing.
- Parameters
-
localId | The local id to translate. |
- Returns
- The translated local id.
The documentation for this class was generated from the following files: