CppCommon  1.0.4.1
C++ Common Library
Classes | Public Types | Public Member Functions | Friends | List of all members
CppCommon::FileCache Class Reference

File cache. More...

#include <filecache.h>

Public Types

typedef std::function< bool(FileCache &cache, const std::string &key, const std::string &value, const Timespan &timeout)> InsertHandler
 File cache insert handler type. More...
 

Public Member Functions

 FileCache ()=default
 
 FileCache (const FileCache &)=delete
 
 FileCache (FileCache &&)=delete
 
 ~FileCache ()=default
 
FileCacheoperator= (const FileCache &)=delete
 
FileCacheoperator= (FileCache &&)=delete
 
 operator bool () const
 Check if the file cache is not empty. More...
 
bool empty () const
 Is the file cache empty? More...
 
size_t size () const
 Get the file cache size. More...
 
bool emplace (std::string &&key, std::string &&value, const Timespan &timeout=Timespan(0))
 Emplace a new cache value with the given timeout into the file cache. More...
 
bool insert (const std::string &key, const std::string &value, const Timespan &timeout=Timespan(0))
 Insert a new cache value with the given timeout into the file cache. More...
 
std::pair< bool, std::string_view > find (const std::string &key)
 Try to find the cache value by the given key. More...
 
std::pair< bool, std::string_view > find (const std::string &key, Timestamp &timeout)
 Try to find the cache value with timeout by the given key. More...
 
bool remove (const std::string &key)
 Remove the cache value with the given key from the file cache. More...
 
bool insert_path (const CppCommon::Path &path, const std::string &prefix="/", const Timespan &timeout=Timespan(0), const InsertHandler &handler=[](FileCache &cache, const std::string &key, const std::string &value, const Timespan &timeout){ return cache.insert(key, value, timeout);})
 Insert a new cache path with the given timeout into the file cache. More...
 
bool find_path (const CppCommon::Path &path)
 Try to find the cache path. More...
 
bool find_path (const CppCommon::Path &path, Timestamp &timeout)
 Try to find the cache path with timeout. More...
 
bool remove_path (const CppCommon::Path &path)
 Remove the cache path from the file cache. More...
 
void clear ()
 Clear the memory cache. More...
 
void watchdog (const UtcTimestamp &utc=UtcTimestamp())
 Watchdog the file cache. More...
 
void swap (FileCache &cache) noexcept
 Swap two instances. More...
 

Friends

void swap (FileCache &cache1, FileCache &cache2) noexcept
 

Detailed Description

File cache.

File cache is used to cache files in memory with optional timeouts.

Thread-safe.

Examples
cache_filecache.cpp.

Definition at line 35 of file filecache.h.

Member Typedef Documentation

◆ InsertHandler

typedef std::function<bool (FileCache& cache, const std::string& key, const std::string& value, const Timespan& timeout)> CppCommon::FileCache::InsertHandler

File cache insert handler type.

Definition at line 39 of file filecache.h.

Constructor & Destructor Documentation

◆ FileCache() [1/3]

CppCommon::FileCache::FileCache ( )
default

◆ FileCache() [2/3]

CppCommon::FileCache::FileCache ( const FileCache )
delete

◆ FileCache() [3/3]

CppCommon::FileCache::FileCache ( FileCache &&  )
delete

◆ ~FileCache()

CppCommon::FileCache::~FileCache ( )
default

Member Function Documentation

◆ clear()

void CppCommon::FileCache::clear ( )

Clear the memory cache.

Definition at line 215 of file filecache.cpp.

◆ emplace()

bool CppCommon::FileCache::emplace ( std::string &&  key,
std::string &&  value,
const Timespan timeout = Timespan(0) 
)

Emplace a new cache value with the given timeout into the file cache.

Parameters
key- Key to emplace
value- Value to emplace
timeout- Cache timeout (default is 0 - no timeout)
Returns
'true' if the cache value was emplaced, 'false' if the given key was not emplaced

Definition at line 13 of file filecache.cpp.

◆ empty()

bool CppCommon::FileCache::empty ( ) const
inline

Is the file cache empty?

Definition at line 11 of file filecache.inl.

◆ find() [1/2]

std::pair< bool, std::string_view > CppCommon::FileCache::find ( const std::string &  key)

Try to find the cache value by the given key.

Parameters
key- Key to find
Returns
'true' if the cache value was found, 'false' if the given key was not found
Examples
cache_filecache.cpp.

Definition at line 55 of file filecache.cpp.

◆ find() [2/2]

std::pair< bool, std::string_view > CppCommon::FileCache::find ( const std::string &  key,
Timestamp timeout 
)

Try to find the cache value with timeout by the given key.

Parameters
key- Key to find
timeout- Cache timeout value
Returns
'true' if the cache value was found, 'false' if the given key was not found

Definition at line 67 of file filecache.cpp.

◆ find_path() [1/2]

bool CppCommon::FileCache::find_path ( const CppCommon::Path path)

Try to find the cache path.

Parameters
path- Path to find
Returns
'true' if the cache path was found, 'false' if the given path was not found

Definition at line 166 of file filecache.cpp.

◆ find_path() [2/2]

bool CppCommon::FileCache::find_path ( const CppCommon::Path path,
Timestamp timeout 
)

Try to find the cache path with timeout.

Parameters
path- Path to find
timeout- Cache timeout value
Returns
'true' if the cache path was found, 'false' if the given path was not found

Definition at line 178 of file filecache.cpp.

◆ insert()

bool CppCommon::FileCache::insert ( const std::string &  key,
const std::string &  value,
const Timespan timeout = Timespan(0) 
)

Insert a new cache value with the given timeout into the file cache.

Parameters
key- Key to insert
value- Value to insert
timeout- Cache timeout (default is 0 - no timeout)
Returns
'true' if the cache value was inserted, 'false' if the given key was not inserted
Examples
cache_filecache.cpp.

Definition at line 34 of file filecache.cpp.

◆ insert_path()

bool CppCommon::FileCache::insert_path ( const CppCommon::Path path,
const std::string &  prefix = "/",
const Timespan timeout = Timespan(0),
const InsertHandler handler = [](FileCache& cache, const std::string& key, const std::string& value, const Timespan& timeout){ return cache.insert(key, value, timeout); } 
)

Insert a new cache path with the given timeout into the file cache.

Parameters
path- Path to insert
prefix- Cache prefix (default is "/")
timeout- Cache timeout (default is 0 - no timeout)
handler- Cache insert handler (default is 'return cache.insert(key, value, timeout)')
Returns
'true' if the cache path was setup, 'false' if failed to setup the cache path

Definition at line 104 of file filecache.cpp.

◆ operator bool()

CppCommon::FileCache::operator bool ( ) const
inlineexplicit

Check if the file cache is not empty.

Definition at line 50 of file filecache.h.

◆ operator=() [1/2]

FileCache& CppCommon::FileCache::operator= ( const FileCache )
delete

◆ operator=() [2/2]

FileCache& CppCommon::FileCache::operator= ( FileCache &&  )
delete

◆ remove()

bool CppCommon::FileCache::remove ( const std::string &  key)

Remove the cache value with the given key from the file cache.

Parameters
key- Key to remove
Returns
'true' if the cache value was removed, 'false' if the given key was not found

Definition at line 80 of file filecache.cpp.

◆ remove_path()

bool CppCommon::FileCache::remove_path ( const CppCommon::Path path)

Remove the cache path from the file cache.

Parameters
path- Path to remove
Returns
'true' if the cache path was removed, 'false' if the given path was not found

Definition at line 191 of file filecache.cpp.

◆ size()

size_t CppCommon::FileCache::size ( ) const
inline

Get the file cache size.

Definition at line 17 of file filecache.inl.

◆ swap()

void CppCommon::FileCache::swap ( FileCache cache)
noexcept

Swap two instances.

Definition at line 273 of file filecache.cpp.

◆ watchdog()

void CppCommon::FileCache::watchdog ( const UtcTimestamp utc = UtcTimestamp())

Watchdog the file cache.

Examples
cache_filecache.cpp.

Definition at line 226 of file filecache.cpp.

Friends And Related Function Documentation

◆ swap

void swap ( FileCache cache1,
FileCache cache2 
)
friend

Definition at line 23 of file filecache.inl.


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