Skip to main content

A simple at-fork cache invalidation library

Project description

forkache

A simple cache invalidation library for Python that automatically clears cached values when your process forks.

Problem

When using os.fork() in Python, cached values initialized in the parent process persist in the child process. This can lead to subtle bugs where the child process uses stale cached data that doesn't reflect the current state.

For example:

from functools import cache

@cache
def get_pid():
    return os.getpid()

get_pid()  # Returns parent PID, caches it

pid = os.fork()
if pid == 0:
    # Child process
    get_pid()  # Returns cached parent PID, not the child's PID!

Solution

forkache provides a decorator that automatically clears cache entries after a fork occurs, ensuring your child process starts with a clean cache.

from functools import cache
from forkache import fork_cache_clear

@fork_cache_clear()
@cache
def get_pid():
    return os.getpid()

get_pid()  # Returns parent PID, caches it

pid = os.fork()
if pid == 0:
    # Child process
    get_pid()  # Clears cache after fork, returns correct child PID

Installation

pip install forkache

Usage

Basic Usage

Use the @fork_cache_clear() decorator on your cached functions:

from functools import cache
from forkache import fork_cache_clear

@fork_cache_clear()
@cache
def expensive_operation():
    return compute_something()

Supported Caching Libraries

The decorator works with any caching library that implements a standard cache clearing method:

functools

from functools import cache
from forkache import fork_cache_clear

@fork_cache_clear()
@cache
def my_function(x):
    return x * 2

cachetools

from cachetools import cached, LRUCache
from forkache import fork_cache_clear

@fork_cache_clear()
@cached(cache=LRUCache(maxsize=32))
def my_function(x):
    return x * 2

Custom Cache Methods

By default, fork_cache_clear() looks for cache clearing methods in this order:

  • cache_clear (functools standard)
  • clear (common convention)
  • delete_memoized (memoization libraries)

If your caching library uses a different method name, specify it explicitly:

@fork_cache_clear(clear_cache_function_name="invalidate")
@my_custom_cache_decorator
def my_function(x):
    return x * 2

Requirements

  • Python 3.13+
  • No external dependencies

License

MIT

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

forkache-0.0.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

forkache-0.0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file forkache-0.0.1.tar.gz.

File metadata

  • Download URL: forkache-0.0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for forkache-0.0.1.tar.gz
Algorithm Hash digest
SHA256 61278cf9178d8db785977481bb5a9484b3eff400b340bba4257a32b1fd743bf6
MD5 315db3e66ce3e2e9f0b7440950b8b614
BLAKE2b-256 ae444ed6523673569f2b21b564f38add3c43e65bb32fe6da3ad2e672563244f3

See more details on using hashes here.

File details

Details for the file forkache-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: forkache-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for forkache-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6eec9e6cc58bc873601cd03293760ff49b6c092ea9a8ebc742b200422ea8ecd4
MD5 9162e5f5389d6c235391a29e04e74445
BLAKE2b-256 980f7de7cf2fb60475db2bfc3ce40ffeeb6d31c9065251ff72b01a51fe04b9da

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