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.
- Like
@PropCache:- Decorator for classes. Registers xprops, extends
__getstate__, and adds__cp_purge__.
- Decorator for classes. Registers xprops, extends
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 withF_PickleIgnore__from__dict__- Classes automatically receive:
__getstate__with the pickle-purge logic__cp_purge__: alias toPropCache.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
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.3.0.tar.gz
(4.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xpropcache-0.3.0.tar.gz.
File metadata
- Download URL: xpropcache-0.3.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.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc913cb78fb47f15cd68e38a8eba3eb3c08e5620a0ec2aedf54c3712e66d29f7
|
|
| MD5 |
194515a39d3d4a1a895900d1f557c985
|
|
| BLAKE2b-256 |
d5388d9ccef5f2062a122091d57631b7c1834fdf08a994588cbe17d72d47e67d
|
File details
Details for the file xpropcache-0.3.0-py3-none-any.whl.
File metadata
- Download URL: xpropcache-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6e6690525cae02892dcd19d92821860f8491ca43dfd750455e411b80c9308c5
|
|
| MD5 |
c87c600b20439ebd6f3260f2b621ccc8
|
|
| BLAKE2b-256 |
99e4f56d4890928181fc2f76129ec54f6c970b8bb118891ae9315e1a0072cc9f
|