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

LRU cache. More...

#include <tkrzw_containers.h>

Classes

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

Public Member Functions

 LRUCache (size_t 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)
 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::LRUCache< VALUETYPE >

LRU cache.

Parameters
VALUETYPEthe value type.

Constructor & Destructor Documentation

◆ LRUCache()

template<typename VALUETYPE >
tkrzw::LRUCache< VALUETYPE >::LRUCache ( size_t  capacity)

Constructor.

Parameters
capacityThe maximum number of records in the cache.

Member Function Documentation

◆ Add()

template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::LRUCache< 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::LRUCache< 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::LRUCache< VALUETYPE >::Get ( int64_t  id)

Gets the value of a record.

Parameters
idThe ID of the record.
Returns
A shared pointer to the value object. It points to nullptr on failure.

◆ Remove()

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

Removes a record.

Parameters
idThe ID of the record.

◆ RemoveLRU()

template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::LRUCache< 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::LRUCache< VALUETYPE >::Size

Gets the number of records.

Returns
The number of records.

◆ IsEmpty()

template<typename VALUETYPE >
bool tkrzw::LRUCache< 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::LRUCache< 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::LRUCache< VALUETYPE >::Clear

Removes all records.

◆ MakeIterator()

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

Makes an iterator for each record.

Returns
The iterator for each record.