LRU cache.
More...
#include <tkrzw_containers.h>
|
| 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...
|
|
template<class VALUETYPE>
class tkrzw::LRUCache< VALUETYPE >
LRU cache.
- Parameters
-
◆ LRUCache()
template<typename VALUETYPE >
Constructor.
- Parameters
-
capacity | The maximum number of records in the cache. |
◆ Add()
template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::LRUCache< VALUETYPE >::Add |
( |
int64_t |
id, |
|
|
VALUETYPE * |
value |
|
) |
| |
Adds a record.
- Parameters
-
id | The ID of the record. |
value | The 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
-
id | The ID of the record. |
value | The 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
-
- Returns
- A shared pointer to the value object. It points to nullptr on failure.
◆ Remove()
template<typename VALUETYPE >
Removes a record.
- Parameters
-
◆ RemoveLRU()
template<typename VALUETYPE >
std::shared_ptr< VALUETYPE > tkrzw::LRUCache< VALUETYPE >::RemoveLRU |
( |
int64_t * |
id = nullptr | ) |
|
Removes the least recent used record.
- Parameters
-
id | The 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 >
Gets the number of records.
- Returns
- The number of records.
◆ IsEmpty()
template<typename VALUETYPE >
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 >
Checks whether stored records exceeds the capacity.
- Returns
- True if stored records exceeds the capacity or false if not.
◆ Clear()
template<typename VALUETYPE >
◆ MakeIterator()
template<typename VALUETYPE >
Makes an iterator for each record.
- Returns
- The iterator for each record.