Skip to main content

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
  • 🧪 fakeredis support 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.

Metadata

Release files for python-cache-service 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-cache-service 0.1.1
File Size Uploaded
python_cache_service-0.1.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-cache-service 0.1.1
File Interpreter ABI Platform
python_cache_service-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / python_cache_service-0.1.1.tar.gz

Download URL python_cache_service-0.1.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a8639b67632f44b1c19b8271be6811f8a8dd8b5349bbd84353c95a472df46ab6
BLAKE2b-256 checksum
How to use checksums
49499299d87a3e280c12b1e58d463eb77104f3db94f7a2a180521f0070981e22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / python_cache_service-0.1.1-py3-none-any.whl

Download URL python_cache_service-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce30d50db9349e67c6754c1e4bd3699697b42faf79eebfdb3958304508980ee4
BLAKE2b-256 checksum
How to use checksums
83d5e1f66e633011b29c12d7e86f33d181ceff802529cc3193567821b77251ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page