Skip to main content

Python implementation of LRU Cache

Project description

lru-od

Python implementation of LRU-Cache using OrderedDict

Installing

Python 3.7 or higher required!

To install the stable version of the library:

# on linux/macOS
python3 -m pip install lru-od

# windows
py -3 -m pip install lru-od

To install the development version of the library:

$ git clone https://github.com/XiehCanCode/lru-od
$ cd lru-od
$ python3 -m pip install -U .

Example

from lru import LRUCache

cache: LRUCache[str, str] = LRUCache(max_size=2)
cache.set("foo", "bar") # you can also use: cache['foo'] = 'bar'
cache.set("bar", "foo")
print(cache.get("foo")) # this key-pair would be pushed to end
cache.set("ping", "pong") # since we're exceeding the max size, the least used will be removed, in this case it's {'bar': 'foo'}

API

  • class LRUCache
    LRU Cache implementation

    Parameters

    max_size: Optional[int]
    Max size of the cache, default's to 120

    Operations

    x == y
    x != y
    x in y

    Methods

    • cache_info() -> str
      Information about the cache
    • get(key: Any) -> Any | None
      Get a value of the key-value pair with given key if exists
    • items() -> tuple[Any, Any]
      D.values() -> a set-like object providing a view on D's items
    • keys() -> KeysView[Any]
      D.keys() -> a set-like object providing a view on D's keys
    • remove(key: Any) -> None
      Remove a key-value pair associated with the key parameter if exists
    • set(key: Any, value: Any) -> dict[Any, Any]
      Set/Update a new/existing key-value pair with given key and pair and returns the key-value pair created/updated
    • values() -> ValuesView[Any]
      D.values() -> a set-like object providing a view on D's values

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lru-od-0.0.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

lru_od-0.0.1-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page