Skip to main content

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

Project description

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.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

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

xpropcache-0.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

xpropcache-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file xpropcache-0.2.0.tar.gz.

File metadata

  • Download URL: xpropcache-0.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xpropcache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 83424874cf7dfaf1e674a4517cb8e388e8d8d00a26d76e0defe57151efab1b8d
MD5 49aefe70061d046da56d4e3b11aa5310
BLAKE2b-256 e0e45fc091fcff50d89bac9c944a8552ccdda00ccbec906f93a18d0a89d63ddc

See more details on using hashes here.

File details

Details for the file xpropcache-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xpropcache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xpropcache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0198b30203a238ef67f9429525dcc26b745b1c86753897b3c9d0a359febf843
MD5 24c0e1b764357cee5312c65d65477667
BLAKE2b-256 66812f20da30b4e4881b85e0cd2d88d668557deadb1cfb23de7de7dd6ce721b7

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