Skip to main content

xpropcache

Lightweight utilities for cached properties with selective reset and pickle-safe classes.

  • Extends functools.cached_property with flags
  • Class decorator for automatic registration
  • Selective cache invalidation
  • Automatic cleaning of cache values and marked attributes for pickling

The xpropcache decorator is @PropCache.cached_property | @PropCache.cached_property(flags)

these properties are referred as xprops below

Installation

  • Pure Python, no external dependencies except standard library
  • Python 3.10+ required
pip install xpropcache --upgrade

Quickstart

from xpropcache import PropCache, F__PickleIgnore__

FLAGS_DB = 0x01

@PropCache
class User:
    
    uid: int
    
    # ignore during pickling
    _session: Session | F__PickleIgnore__

    def __init__(self, uid, loader, session=None):
        self.uid = uid
        self._loader = loader
        self._session = session

    @PropCache.cached_property
    def profile(self):
        return self._loader.load_profile(self.uid)

    @PropCache.cached_property(FLAGS_DB)
    def settings(self):
        return self._loader.load_settings(self.uid)

u = User(42, loader)
if not u.profile:  # computed and cached
    new_user(u)
    PropCache.cp_purge(u)  # invalidates all xprops
elif u.settings.is_premium:  # computed and cached
    ...
    PropCache.cp_reset_by_flag(u, FLAGS_DB)  # only invalidates settings

Concepts

  • @PropCache.cached_property | @PropCache.cached_property(flags):
    • Like functools.cached_property, with additional optional flags (int) for grouping.
    • Cached values are ignored during pickling.
  • @PropCache:
    • Decorator for classes. Registers xprops, extends __getstate__, and adds __cp_purge__.
  • F__PickleIgnore__:
    • Marker type for type annotations. Attributes annotated with this type (directly or in union) are removed during pickling (in addition to cached xprops).

API (short)

  • PropCache.cp_purge(inst): Reset all cached xprops of the instance.
  • PropCache.cp_reset_by_flag(inst, flags): Reset cached xprops of the instance whose flag bits apply (xprop.flags & flags).
  • PropCache.pickle_purge(inst, inst.__dict__): Removes cached xprops and attributes marked with F_PickleIgnore__ from __dict__
  • Classes automatically receive:
    • __getstate__ with the pickle-purge logic
    • __cp_purge__: alias to PropCache.cp_purge(self)

Notes

  • Inheritance: xprops and F_PickleIgnore__ flags are merged via the MRO.

Changelog

0.3 - fix

  • filters the generic object.__getstate__

0.2

  • cache entries are now also accessible via subtypes
...
@PropCache
class User:
    ...

class PremiumUser(User):
    ...

pu = PremiumUser(42, loader)
PropCache.cp_purge(pu)

0.1

Initial release

Metadata

Release files for xpropcache 0.3.0

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

Source distribution (sdist)

Source distribution for xpropcache 0.3.0
File Size Uploaded Provenance Actions
xpropcache-0.3.0.tar.gz 4.4 kB Without provenance Details for xpropcache-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xpropcache 0.3.0
File Interpreter ABI Platform Actions
xpropcache-0.3.0-py3-none-any.whl Python 3 none any Details for xpropcache-0.3.0-py3-none-any.whl

Total release size: 9.0 kB

Release files / xpropcache-0.3.0.tar.gz

Download URL xpropcache-0.3.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bc913cb78fb47f15cd68e38a8eba3eb3c08e5620a0ec2aedf54c3712e66d29f7
BLAKE2b-256 checksum
How to use checksums
d5388d9ccef5f2062a122091d57631b7c1834fdf08a994588cbe17d72d47e67d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / xpropcache-0.3.0-py3-none-any.whl

Download URL xpropcache-0.3.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6e6690525cae02892dcd19d92821860f8491ca43dfd750455e411b80c9308c5
BLAKE2b-256 checksum
How to use checksums
99e4f56d4890928181fc2f76129ec54f6c970b8bb118891ae9315e1a0072cc9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Security for xpropcache 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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