Tkrzw
Classes | Public Member Functions | List of all members
tkrzw::DoubleLRUCache< VALUETYPE > Class Template Referencefinal

Double-layered LRU cache. More...

#include <tkrzw_containers.h>

Classes

class  Iterator
 Iterator to access each record. More...
 

Public Member Functions

 DoubleLRUCache (size_t hot_capacity, size_t warm_capacity)
 Constructor. More...
 
std::shared_ptr< VALUETYPE > Add (int64_t id, VALUETYPE *value)
 Adds a record. More...
 
void GiveBack (int64_t id, std::shared_ptr< VALUETYPE > &&value)
 Gives back a record which has been removed. More...
 
std::shared_ptr< VALUETYPE > Get (int64_t id, bool promotion)
 Gets the value of a record. More...
 
void Remove (int64_t id)
 Removes a record. More...
 
std::shared_ptr< VALUETYPE > RemoveLRU (int64_t *id=nullptr)
 Removes the least recent used record. More...
 
size_t Size () const
 Gets the number of records. More...
 
bool IsEmpty () const
 Checks whether no records exist. More...
 
bool IsSaturated () const
 Checks whether stored records exceeds the capacity. More...
 
void Clear ()
 Removes all records. More...
 
Iterator MakeIterator ()
 Makes an iterator for each record. More...
 

Detailed Description

template<class VALUETYPE>
class tkrzw::DoubleLRUCache< VALUETYPE >

Double-layered LRU cache.

Parameters
VALUETYPEthe value type.

Constructor & Destructor Documentation

◆ DoubleLRUCache()

template<typename VALUETYPE >
tkrzw::DoubleLRUCache< VALUETYPE >::DoubleLRUCache ( size_t  hot_capacity,
size_t  warm_capacity 
)

Constructor.

Parameters
hot_capacityThe maximum number of records in the hot cache.
warm_capacityThe maximum number of records in the worm cache.

Member Function Documentation

◆ Add()

template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::DoubleLRUCache< VALUETYPE >::Add ( int64_t  id,
VALUETYPE *  value 
)

Adds a record.

Parameters
idThe ID of the record.
valueThe pointer to the value object. Ownership is taken.
Returns
A shared pointer to the value object.

If there is an existing record of the same ID, the new value object is just deleted and the return value refers to the existing record.

◆ GiveBack()

template<typename VALUETYPE >
void tkrzw::DoubleLRUCache< VALUETYPE >::GiveBack ( int64_t  id,
std::shared_ptr< VALUETYPE > &&  value 
)

Gives back a record which has been removed.

Parameters
idThe ID of the record.
valueThe moved pointer to the value object. Ownership is taken.

◆ Get()

template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::DoubleLRUCache< VALUETYPE >::Get ( int64_t  id,
bool  promotion 
)

Gets the value of a record.

Parameters
idThe ID of the record.
promotionTrue if the record can be promoted to the hot list.
Returns
A shared pointer to the value object. It points to nullptr on failure.

◆ Remove()

template<typename VALUETYPE >
void tkrzw::DoubleLRUCache< VALUETYPE >::Remove ( int64_t  id)

Removes a record.

Parameters
idThe ID of the record.

◆ RemoveLRU()

template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::DoubleLRUCache< VALUETYPE >::RemoveLRU ( int64_t *  id = nullptr)

Removes the least recent used record.

Parameters
idThe pointer to store the ID of the record. If it is nullptr, it is ignored.
Returns
A shared pointer to the value object. It points to nullptr on failure.

◆ Size()

template<typename VALUETYPE >
size_t tkrzw::DoubleLRUCache< VALUETYPE >::Size

Gets the number of records.

Returns
The number of records.

◆ IsEmpty()

template<typename VALUETYPE >
bool tkrzw::DoubleLRUCache< VALUETYPE >::IsEmpty

Checks whether no records exist.

Returns
True if there's no record or false if there are one or more records.

◆ IsSaturated()

template<typename VALUETYPE >
bool tkrzw::DoubleLRUCache< VALUETYPE >::IsSaturated

Checks whether stored records exceeds the capacity.

Returns
True if stored records exceeds the capacity or false if not.

◆ Clear()

template<typename VALUETYPE >
void tkrzw::DoubleLRUCache< VALUETYPE >::Clear

Removes all records.

◆ MakeIterator()

template<typename VALUETYPE >
DoubleLRUCache< VALUETYPE >::Iterator tkrzw::DoubleLRUCache< VALUETYPE >::MakeIterator

Makes an iterator for each record.

Returns
The iterator for each record.