Method _cache
Why
If you want to _cache the calls to a specific method or function you could use the python functools._cache decorator.
If this has not enough configuration options for your taste, or you work with arguments which are not hashable this _
cache decorator could be useful.
Advantages
- Works with non hashable objects
- Set expiry after time
- Set expiry after a schedule
- Set maximal _cache size per method
- Works with sync and async functions
- Properly tested
Usage
Cache
Use a cache which expires after a certain amount of time:
from pycache import cache
# The format for schedule_type is <hh:mm:ss>
# This _cache would expire every 10 seconds
@cache(expires_at="*:*:10")
def please_cache():
pass
# This _cache would expire every 5 minutes and 10 seconds
@cache(expires_every="*:5:10")
def please_cache():
pass
Use a _cache which expires every time at a certain time (A bit like a cron job).
from pycache import cache
# The format for _schedule_str is <hh:mm:ss>
# This _cache would expire every day at 15:10:05
@cache(expires_at="15:10:05")
def please_cache():
pass
# This _cache would expire every hour 8 minutes after a full hour
@cache(expires_at="*:08:00")
def please_cache():
pass
Limit the number of _cache entries
from pycache import cache
# This would result in only one _cache entry
@cache(expires_every="*:*:10", max_cache_size=1)
def please_cache(data: str):
pass
# Gets placed in _cache
please_cache("hello")
# Gets called from _cache
please_cache("hello")
# Gets placed in _cache and "hello" gets removed
please_cache("world")
# Is not found in _cache, because "world" is the only _cache entry,
# because the _cache size is one
please_cache("hello")
Schedule
from pycache import schedule, add_schedule, ScheduleSubscription
import asyncio
# Gets called every 10 seconds
@schedule(call_every="*:*:10")
def schedule_me():
pass
# Gets called every at 10 am
@schedule(call_every="10:00:00")
def schedule_me():
pass
# Gets called 3 times
@schedule(call_every="10:00:00", stop_after=3)
def schedule_me():
pass
# Call with args and keyword args
@schedule(call_every="10:00:00", args=(3,), kwargs={"hello": "world"})
def schedule_me(three: int, hello: str):
pass
# Pass an event loop
@schedule(call_every="10:00:00", event_loop=your_event_loop)
async def schedule_me():
pass
def schedule_programmatically():
pass
# Call this every five seconds
schedule_subscription: ScheduleSubscription = add_schedule(schedule_programmatically, call_every="*:*:5")
# Stop the schedule call
schedule_subscription.stop()
# Start the schedule again
schedule_subscription.stop()
Metadata
Release files for py-cache 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py-_cache-0.3.2.tar.gz | 6.8 kB | Without provenance | Details for py-_cache-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py__cache-0.3.2-py3-none-any.whl | Details for py__cache-0.3.2-py3-none-any.whl |
Total release size: 14.3 kB
Release files / py-_cache-0.3.2.tar.gz
| Download URL | py-_cache-0.3.2.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53f09fa203738a216c631ec2c31ece5abec817e7eca5fd7cf8e38d3b20e33c70
|
|
BLAKE2b-256 checksum How to use checksums |
fb0a602e089e2b1b3075153cd9b703f4b04cd9c86cbe1d3ded3511b37674ce44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.0 CPython/3.6.13 Linux/5.4.0-1040-azure
|
Release files / py__cache-0.3.2-py3-none-any.whl
| Download URL | py__cache-0.3.2-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
|
SHA-256 checksum How to use checksums |
f1c5a8696c407302c2711c3568c2e0b65e7673bf2b95a54cc90457f2e2143ec9
|
|
BLAKE2b-256 checksum How to use checksums |
ac912b9381deeadaff7846a12162eb7eb7866ac81572963dac2f754583a7cb60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.0 CPython/3.6.13 Linux/5.4.0-1040-azure
|
Security for py-cache 0.3.2
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.