Skip to main content

A Python package for memory management and caching

Project description

Memoria

A Python package for efficient function result caching with type safety and flexible storage options. Memoria helps you cache function results to avoid redundant computations, with built-in type checking and flexible storage options.

Installation

pip install memoria

Quick Start

The most basic usage of Memoria involves setting up a cache directory and using the @cache decorator on your functions. Here's a simple example:

from memoria import cache

# Set up the cache directory (required before using the cache)
cache.set_dir("./my_cache")

# Basic usage with the @cache decorator
@cache()
def expensive_computation(x, y):
    # Your expensive computation here
    return x + y

# The result will be cached and reused for the same inputs
result = expensive_computation(5, 3)  # Computes and caches
result = expensive_computation(5, 3)  # Returns cached result

Features

Type Safety

Memoria can enforce type checking on your cached results. This is useful when you want to ensure that your function always returns data of a specific type. If the function returns a value of a different type, a TypeError will be raised:

# Using actual type objects for type checking
@cache(output_type=list)  # Note: using list, not List
def get_numbers():
    return [1, 2, 3]

# This will raise a TypeError if the function returns something other than a list
# For example:
@cache(output_type=str)
def get_string():
    return "hello"  # This is fine
    # return 42  # This would raise TypeError: result is not of type str

Custom Cache Directory Structure

You can organize your cached results in subdirectories by specifying a directory name in the cache decorator. This helps keep your cache organized, especially when dealing with multiple functions:

@cache(dir="my_subdirectory")
def my_function():
    pass

The results will be stored in ./my_cache/my_subdirectory/ instead of directly in the cache root.

Custom Cache File Naming

By default, Memoria uses a hash of the function arguments to name cache files. You can customize this using the pattern parameter, which supports Python's string formatting:

@cache(pattern="{param1}_{param2}")
def my_function(param1, param2):
    pass

This will create cache files named like param1_value_param2_value.pkl instead of using a hash.

Cache Management

Memoria provides several tools to manage your cached results:

# Check if a result is cached
@cache()
def my_function(x):
    return x * 2

my_function.is_cached(x=5)  # Returns True if cached

# Clear specific cache
my_function.clear(x=5)  # Clears cache for x=5

# Clear all caches for a function
my_function.clear_all()

# Enable/disable verbose logging
cache.verbose_on()  # Shows cache hits/misses
cache.verbose_off()  # Hides cache hits/misses

The verbose mode is particularly useful during development as it shows you when results are being retrieved from cache versus being computed.

Global Cache Management

In addition to per-function cache management, Memoria provides global cache management functions:

# Get current cache directory
current_dir = cache.get_dir()

# Clear all caches
cache.clear()

# Unset cache directory
cache.unset_dir()

These functions help you manage the cache at a global level, useful for cleanup operations or changing cache locations.

Advanced Usage

Complex Data Types

Memoria works well with custom data types. When using custom classes, make sure to pass the actual class as the output_type:

from dataclasses import dataclass

@dataclass
class Result:
    value: int
    description: str

@cache(output_type=Result)  # Using the actual Result class
def process_data(x: int) -> Result:
    return Result(value=x*2, description=f"Processed {x}")

The type checking ensures that your function returns an instance of the specified class.

License

This project is licensed under the Conditional Freedom License (CFL-1.0) - 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

memoria-2025.4.27.4.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

memoria-2025.4.27.4-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file memoria-2025.4.27.4.tar.gz.

File metadata

  • Download URL: memoria-2025.4.27.4.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for memoria-2025.4.27.4.tar.gz
Algorithm Hash digest
SHA256 ed7ae4ed6116894bdd0aec3c7ca9aeb4483abd2ac5f502339b42a89fa066b2b9
MD5 2a60bf9322052d0b1aa184c74e78903e
BLAKE2b-256 ce9dcca8b359553a95bc5cf92d985ca63f7d80ae86fdf406e09012a1b4a7e5eb

See more details on using hashes here.

File details

Details for the file memoria-2025.4.27.4-py3-none-any.whl.

File metadata

  • Download URL: memoria-2025.4.27.4-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for memoria-2025.4.27.4-py3-none-any.whl
Algorithm Hash digest
SHA256 49c171294a3e2c54b6123240de72438720745db33a552a36b4ec3bfc578a399e
MD5 c81b75afbd626965ae1d12efaf10a0ac
BLAKE2b-256 6e07548885e81341f5739060f040d6085e485e6572e8582837046157ff8e3831

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