Skip to main content

Provide cache with decoration

Project description

FlexiCache

FlexiCache is a versatile and user-friendly cache decorator for Python functions that is compatible with various storage backends. This allows you to cache function results in memory, on a local file system, or in AWS DynamoDB. You can set up the cache expiration time, and if not set up, the cache will always be valid.

Features

  • Decorate functions to cache their results
  • Compatible with memory, local file system, and AWS DynamoDB storage backends
  • Set custom cache expiration times
  • Easily expandable for other storage backends

Local Test Setup

Follow these steps to build and install the package for local testing:

# Build the package
python setup.py bdist_wheel

# Install the package
pip install --force-reinstall dist/FlexiCache-0.0.1-py3-none-any.whl

Examples

Install FlexiCache

First, install the FlexiCache package:

pip install flexicache

Import Classes

Import the required classes for the cache types you want to use:

from flexicache import Cache, MemoryCache, FileCache, DDBCache

Cache Storage Format

Cache data is stored in a readable format that includes the function name, arguments, keyword arguments, timestamp, and value:

{
    "c7512aa16312c157ac8490dbdd00f999": {
        "function": "file_cached_function",
        "args": "(1, 2)",
        "kwargs": "{}",
        "timestamp": 1685835554.402383,
        "value": 2
    },
    "296849916ce184fe8419fb3b466bd233": {
        "function": "file_cached_function",
        "args": "(2, 2)",
        "kwargs": "{}",
        "timestamp": 1685837065.483979,
        "value": 4
    }
}

Usage

Memory Cache

from flexicache import Cache, MemoryCache

memory_cache = MemoryCache()
@Cache(memory_cache)
def memory_cached_function(a, b):
    return a + b

Local File System Cache

from flexicache import Cache, FileCache

file_cache = FileCache('cache.json')
@Cache(file_cache)
def file_cached_function(a, b):
    return a * b

AWS DynamoDB Cache

Ensure the boto3 package is installed and AWS credentials are configured to use the DDBCache class.

import boto3
from flexicache import Cache, DDBCache

aws_ddb_client = boto3.client('dynamodb', region_name='your-region', aws_access_key_id='your-access-key', aws_secret_access_key='your-secret-key')
ddb_cache = DDBCache(aws_ddb_client, table_name='my_ddb_table', cache_seconds=900)

@Cache(ddb_cache)
def ddb_cached_function(a, b):
    return a ** b

Extending to Other Storage Backends

To support a new cache storage backend, create a new class that inherits from CacheStrategy and implements the get and set methods.

from cache_strategy import CacheStrategy

class MyCache(CacheStrategy):
    def get(self, key):
        # Implement logic to get the value for the key from your storage backend
        pass

    def set(self, key, value, func, *args, **kwargs):
        # Implement logic to store the value for the key in your storage backend
        pass

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

FlexiCache-0.0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file FlexiCache-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: FlexiCache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for FlexiCache-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 91f4f3328bc96cfa27883ea1b86183fa8a9dc9da1483be0b8d918ee59ade04ab
MD5 8eac377c693a4d009806a292d3bd53f7
BLAKE2b-256 71d02fc459e28cd4daa69342f6eed2146ee07b88e0c2b8385e5252cce601798d

See more details on using hashes here.

Supported by

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