Class LRUCache

class ooodev.utils.cache.LRUCache(capacity)[source]

Least Recently Used (LRU) Cache

Parameters:

capacity (int) –

__contains__(key)[source]
Return type:

bool

Parameters:

key (Any) –

__delitem__(key)[source]
Return type:

None

Parameters:

key (Any) –

__getitem__(key)[source]
Return type:

Any

Parameters:

key (Any) –

__init__(capacity)[source]

Least Recently Used (LRU) Cache

Parameters:

capacity (int) – Cache capacity.

__len__()[source]
Return type:

int

__setitem__(key, value)[source]
Return type:

None

Parameters:
  • key (Any) –

  • value (Any) –

clear()[source]

Clear cache.

Return type:

None

get(key)[source]

Get value by key.

Parameters:

key (Any) – Any Hashable object.

Returns:

Value or None if not found.

Return type:

Any

Note

The get method is an alias for the __getitem__ method. So you can use cache_inst.get(key) or cache_inst[key] interchangeably.

put(key, value)[source]

Put value by key.

Parameters:
  • key (Any) – Any Hashable object.

  • value (Any) – Any object.

Return type:

None

Note

The put method is an alias for the __setitem__ method. So you can use cache_inst.put(key, value) or cache_inst[key] = value interchangeably.

Return type:

None

Parameters:
  • key (Any) –

  • value (Any) –

remove(key)[source]

Remove key.

Parameters:

key (Any) – Any Hashable object.

Return type:

None

Note

The remove method is an alias for the __delitem__ method. So you can use cache_inst.remove(key) or del cache_inst[key] interchangeably.

Return type:

None

Parameters:

key (Any) –

property capacity: int

Gets/Sets Cache capacity.

Setting the capacity to 0 or less will clear the cache and effectively turn caching off. Setting the capacity to a lower value will remove the least recently used items.

Returns:

Cache capacity.

Return type:

int