Skip to main content

Simple mem cache client for python

Project description

simplememcache

PyPI version License

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.

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

simplememcache-0.0.0a0.tar.gz (5.5 kB view hashes)

Uploaded Source

Built Distribution

simplememcache-0.0.0a0-py2.py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 2 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