Skip to main content

Provides caching for class properties.

Project description

Reactive Caching

2026 by rgzz666

v0.1.0

pip3 install reactive_caching

Reactive Caching is a Python module providing the ability to cache properties of a class, and automatically invalidate them on specified rules of changes in attributes. This can avoid calculating the property everytime when calling it and keeping the data up to date.

Usage

Import the module

To use the module, you must import it with:

import reactive_caching

or if you prefer to directly use the contents:

from reactive_caching import CachedClass

Note that the latter will be used as the example in the folowing guide.

Subclassing

The class containing any cached properties must have the ability to handle them. In this project, the class get those abilities by subclassing CachedClass .

class MyClass(CachedClass):
    ...

This will redefine its __setattr__() , so if your class requires a customized __setattr__() routine, you must run CachedClass.__setattr(name, value) inside your routine.

cached_property Decorator Factory

Each cached property should be decorated by a decorator which generated by cached_property factory. Usage shown as below:

class MyClass(CachedClass):
    ...
    @MyClass.cached_property(watched_attrs = "-all-")
    def a_property_that_is_calced_with_all_attributes(self) -> int:
        ...
        return 1

Caching Rules

This is a parameter of cached_property decorator factory, used to specify rules of the cache.

Caching rules can be set to either a list of property names, or one of the keywords. Effect of each configuration will be introduced below:

  • List of property names: All attributes with name included in that list will be watched. When any of their values is changed, the cache will be flagged dirty.
  • Keyword -all-: All attributes will be watched and if any of them is changed, the cache will be flagged dirty.
  • Keyword -exposed-: All exposed attributes (those with name that does not starts with `_`) will be watched. When any of their values is changed, the cache will be flagged dirty.

on_cache_dirty Bind

If you decide to carry out a specific routine when a cache turned dirty, override _on_cache_dirty() function with your own when inheriting CachedClass. Your function should receive a parameter in type string, containing the name of the property which has its cache turned dirty, and returns None.

class A(CachedClass):
    ...
    def _on_cache_dirty(prop_name: str) -> None:
        print(f"Cached property {prop_name} got its cache dirty.")

Destroying Cache

If a cached object is no longer needed, you may use destroy_cache() to free up memory space by removing all cache-relating stuff of such object.

Copyright

   Copyright 2026 rgzz666

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Questions?

For code contribution, create a pull request.

For bugs and feature requests, create an issue.

For questions in usage or other discussions, use GitHub Discussions.


That's all, Good luck!

(C) 2026 rgzz666, licensed under Apache License 2.0, see Copyright section for more.

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

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

reactive_caching-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file reactive_caching-0.1.0.tar.gz.

File metadata

  • Download URL: reactive_caching-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for reactive_caching-0.1.0.tar.gz
Algorithm Hash digest
SHA256 65269db36996d62573a47506489809faaef923984eb8648217b4e321f620ff29
MD5 cc8f2c8dba4d36cfe5c1f2480092ccf4
BLAKE2b-256 c6a7c7b36bd2131f83fc45a9cf7253c819eb1b7977b03445dc15c318e42aa69a

See more details on using hashes here.

Provenance

The following attestation bundles were made for reactive_caching-0.1.0.tar.gz:

Publisher: python-publish.yml on TotoWang-hhh/reactive_caching

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file reactive_caching-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for reactive_caching-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cdd7b4491ec51dddbcad9b55659cbc152eb4938c966cbf34ca2a7e88aeb419a
MD5 ed9d28d3a6c69d166fe42bb80aba7f09
BLAKE2b-256 bfedae3564b78fb73be6b2a037e77489bf3fbdbf830be6d3d739b7ef07d20cac

See more details on using hashes here.

Provenance

The following attestation bundles were made for reactive_caching-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on TotoWang-hhh/reactive_caching

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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