Rose
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
rose::WebCache Class Reference

Fetch web resources caching them in the local filesystem following XDG specifications. More...

#include <WebCache.h>

Inheritance diagram for rose::WebCache:
Inheritance graph
[legend]
Collaboration diagram for rose::WebCache:
Collaboration graph
[legend]

Public Types

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 > >
 

Public Member Functions

 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 ()
 

Static Public Member Functions

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

Public Attributes

WebCacheProtocol::signal_type cacheLoaded {}
 

Protected Member Functions

void asyncFetchItem (item_map_t::value_type &item)
 

Protected Attributes

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.
 

Detailed Description

Fetch web resources caching them in the local filesystem following XDG specifications.

Constructor & Destructor Documentation

◆ WebCache()

rose::WebCache::WebCache ( const std::string &  rootUri,
const path &  xdgDir,
const std::string &  storeRoot,
std::chrono::system_clock::duration  duration 
)

Constructor.

Parameters
rootUriThe base URI locating all the items manage by the cache.
xdgDirThe XDG compliant specification for the user caching directory. See rose::Environment.
storeRootA relative path from xdgDir that this cache will use.
durationThe period of time cache items should be considered valid before being reloaded.

Member Function Documentation

◆ 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
itemPathThe item path (returned by itemLocalPath()).
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
localIdThe local id.
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
keyThe item key.
itemUrlThe item url (returned by constructUrl()).
itemPathThe full item path.
cacheFileTimeThe 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 ( fileTimePoint)
inlinestatic

Convert a filesystem time to a system clock time point.

Template Parameters
TThe type of the file time point.
Parameters
fileTimePointThe 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
filePathThe 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
keyThe 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
keyThe item key.
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
keyThe key to identify the cache item.
localIdThe 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
ItThe iterator type.
Parameters
firstThe first item iterator.
lastThe 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
localIdThe local id to translate.
Returns
The translated local id.

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