CppCommon  1.0.4.1
C++ Common Library
Classes | Public Member Functions | Friends | List of all members
CppCommon::MemCache< TKey, TValue > Class Template Reference

Memory cache. More...

#include <memcache.h>

Public Member Functions

 MemCache ()=default
 
 MemCache (const MemCache &)=delete
 
 MemCache (MemCache &&)=delete
 
 ~MemCache ()=default
 
MemCacheoperator= (const MemCache &)=delete
 
MemCacheoperator= (MemCache &&)=delete
 
 operator bool () const
 Check if the memory cache is not empty. More...
 
bool empty () const
 Is the memory cache empty? More...
 
size_t size () const
 Get the memory cache size. More...
 
bool emplace (TKey &&key, TValue &&value, const Timespan &timeout=Timespan(0))
 Emplace a new cache value with the given timeout into the memory cache. More...
 
bool insert (const TKey &key, const TValue &value, const Timespan &timeout=Timespan(0))
 Insert a new cache value with the given timeout into the memory cache. More...
 
bool find (const TKey &key)
 Try to find the cache value by the given key. More...
 
bool find (const TKey &key, TValue &value)
 Try to find the cache value by the given key. More...
 
bool find (const TKey &key, TValue &value, Timestamp &timeout)
 Try to find the cache value with timeout by the given key. More...
 
bool remove (const TKey &key)
 Remove the cache value with the given key from the memory cache. More...
 
void clear ()
 Clear the memory cache. More...
 
void watchdog (const UtcTimestamp &utc=UtcTimestamp())
 Watchdog the memory cache. More...
 
void swap (MemCache &cache) noexcept
 Swap two instances. More...
 

Friends

template<typename UKey , typename UValue >
void swap (MemCache< UKey, UValue > &cache1, MemCache< UKey, UValue > &cache2) noexcept
 

Detailed Description

template<typename TKey, typename TValue>
class CppCommon::MemCache< TKey, TValue >

Memory cache.

Memory cache is used to cache data in memory with optional timeouts.

Thread-safe.

Examples
cache_memcache.cpp.

Definition at line 29 of file memcache.h.

Constructor & Destructor Documentation

◆ MemCache() [1/3]

template<typename TKey , typename TValue >
CppCommon::MemCache< TKey, TValue >::MemCache ( )
default

◆ MemCache() [2/3]

template<typename TKey , typename TValue >
CppCommon::MemCache< TKey, TValue >::MemCache ( const MemCache< TKey, TValue > &  )
delete

◆ MemCache() [3/3]

template<typename TKey , typename TValue >
CppCommon::MemCache< TKey, TValue >::MemCache ( MemCache< TKey, TValue > &&  )
delete

◆ ~MemCache()

template<typename TKey , typename TValue >
CppCommon::MemCache< TKey, TValue >::~MemCache ( )
default

Member Function Documentation

◆ clear()

template<typename TKey , typename TValue >
void CppCommon::MemCache< TKey, TValue >::clear
inline

Clear the memory cache.

Definition at line 138 of file memcache.inl.

◆ emplace()

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::emplace ( TKey &&  key,
TValue &&  value,
const Timespan timeout = Timespan(0) 
)
inline

Emplace a new cache value with the given timeout into the memory 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 26 of file memcache.inl.

◆ empty()

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::empty
inline

Is the memory cache empty?

Definition at line 12 of file memcache.inl.

◆ find() [1/3]

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::find ( const TKey &  key)
inline

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

Definition at line 70 of file memcache.inl.

◆ find() [2/3]

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::find ( const TKey &  key,
TValue &  value 
)
inline

Try to find the cache value by the given key.

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

Definition at line 83 of file memcache.inl.

◆ find() [3/3]

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::find ( const TKey &  key,
TValue &  value,
Timestamp timeout 
)
inline

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

Parameters
key- Key to find
value- Value 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 97 of file memcache.inl.

◆ insert()

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::insert ( const TKey &  key,
const TValue &  value,
const Timespan timeout = Timespan(0) 
)
inline

Insert a new cache value with the given timeout into the memory 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_memcache.cpp.

Definition at line 48 of file memcache.inl.

◆ operator bool()

template<typename TKey , typename TValue >
CppCommon::MemCache< TKey, TValue >::operator bool ( ) const
inlineexplicit

Check if the memory cache is not empty.

Definition at line 41 of file memcache.h.

◆ operator=() [1/2]

template<typename TKey , typename TValue >
MemCache& CppCommon::MemCache< TKey, TValue >::operator= ( const MemCache< TKey, TValue > &  )
delete

◆ operator=() [2/2]

template<typename TKey , typename TValue >
MemCache& CppCommon::MemCache< TKey, TValue >::operator= ( MemCache< TKey, TValue > &&  )
delete

◆ remove()

template<typename TKey , typename TValue >
bool CppCommon::MemCache< TKey, TValue >::remove ( const TKey &  key)
inline

Remove the cache value with the given key from the memory 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 112 of file memcache.inl.

◆ size()

template<typename TKey , typename TValue >
size_t CppCommon::MemCache< TKey, TValue >::size
inline

Get the memory cache size.

Definition at line 19 of file memcache.inl.

◆ swap()

template<typename TKey , typename TValue >
void CppCommon::MemCache< TKey, TValue >::swap ( MemCache< TKey, TValue > &  cache)
inlinenoexcept

Swap two instances.

Definition at line 172 of file memcache.inl.

◆ watchdog()

template<typename TKey , typename TValue >
void CppCommon::MemCache< TKey, TValue >::watchdog ( const UtcTimestamp utc = UtcTimestamp())
inline

Watchdog the memory cache.

Examples
cache_memcache.cpp.

Definition at line 148 of file memcache.inl.

Friends And Related Function Documentation

◆ swap

template<typename TKey , typename TValue >
template<typename UKey , typename UValue >
void swap ( MemCache< UKey, UValue > &  cache1,
MemCache< UKey, UValue > &  cache2 
)
friend

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