Simple namespaced cache service using Redis
Project description
python-cache-service
A lightweight, namespaced caching service using Redis.
Supports fast prototyping and production-ready use, with built-in support for Redis and fakeredis for tests.
🚀 Features
- 🔑 Namespaced keys to avoid collisions
- ⏳ Optional TTL for cache expiration
- 🧪
fakeredissupport for seamless unit testing - 🧩 Decorator to cache function results transparently
- 🧼 Cache clearing by namespace
📦 Installation
pip install python-cache-service
Usage
from cache.domain.factories.cache_service_factory import CacheServiceFactory
cache = CacheServiceFactory.create(namespace="my_app")
# Store a value
cache.set("my_key", {"data": 123}, ttl_seconds=3600)
# Retrieve a value
value = cache.get("my_key")
# Delete a value
cache.delete("my_key")
# Clear all keys in the namespace
cache.clear()
Configuration
You can configure the cache service by setting environment variables:
- REDIS_HOST=localhost
- REDIS_PORT=6379
- REDIS_DB=0
🧪 Using in tests
from cache.domain.factories.cache_service_factory import CacheServiceFactory
cache = CacheServiceFactory.createTest()
cache.set("test_key", [1, 2, 3])
assert cache.get("test_key") == [1, 2, 3]
You can also disable caching entirely in test mode:
from cache.domain.factories.cache_service_factory import CacheServiceFactory
cache = CacheServiceFactory.createTest(disabled=True)
🎯 Using the @cache_result decorator
from cache.decorators.cache_result import cache_result
from cache.domain.factories.cache_service_factory import CacheServiceFactory
cache = CacheServiceFactory.create(namespace="my_app")
@cache_result(cache, ttl_seconds=120, key_prefix="my_func", verbose=True)
def heavy_computation(x, y):
return x + y
# First call: computed and cached
heavy_computation(2, 3)
# Second call: cache hit
heavy_computation(2, 3)
🧾 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_cache_service-0.1.1.tar.gz.
File metadata
- Download URL: python_cache_service-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8639b67632f44b1c19b8271be6811f8a8dd8b5349bbd84353c95a472df46ab6
|
|
| MD5 |
bae7f24f8a38241f13a2976dc107d853
|
|
| BLAKE2b-256 |
49499299d87a3e280c12b1e58d463eb77104f3db94f7a2a180521f0070981e22
|
File details
Details for the file python_cache_service-0.1.1-py3-none-any.whl.
File metadata
- Download URL: python_cache_service-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce30d50db9349e67c6754c1e4bd3699697b42faf79eebfdb3958304508980ee4
|
|
| MD5 |
d4d6bc625e8dee1dcb459305b776cc5b
|
|
| BLAKE2b-256 |
83d5e1f66e633011b29c12d7e86f33d181ceff802529cc3193567821b77251ed
|