simplememcache
simplememcache
is a lightweight Python package providing a simple and efficient Least Recently Used (LRU) cache implementation. It allows you to cache and manage key-value pairs with a maximum size, automatically evicting the least recently used items when the cache reaches its limit.
Installation
You can install simplememcache
using pip:
pip install simplememcache
Importing the package
from simplememcache.lru import LRUCache
Creating a cache instance
# Create an LRU cache with a maximum size of 100 items
cache = LRUCache(max_size=100)
Inserting items into the cache
cache.insert("key1", "value1")
cache.insert("key2", 42)
Getting items from the cache
value = cache.get("key1")
print(value) # Output: "value1"
Getting items with default value
value = cache.get_or_default("nonexistent_key", default="default_value")
print(value) # Output: "default_value"
Upserting items into the cache
# Upsert (update or insert) an item into the cache
result = cache.upsert("key1", "new_value")
print(result) # Output: False (key1 already existed, value updated)
Deleting items from the cache
deleted_value = cache.delete("key2")
print(deleted_value) # Output: 42
Deleting items with default value
deleted_value = cache.delete_or_default("nonexistent_key", default="default_value")
print(deleted_value) # Output: "default_value"
Checking the size of the cache
cache_size = cache.size
print(cache_size) # Output: 1
Contributing
Feel free to contribute by opening issues or submitting pull requests on the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.