Skip to main content

Simple to use caching decorator with more capabilites than the default one.

Project description

Method _cache

codecov Upload Python Package

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


# 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


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()

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

py-_cache-0.2.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

py__cache-0.2.0-py3-none-any.whl (7.3 kB view details)

Uploaded

File details

Details for the file py-_cache-0.2.0.tar.gz.

File metadata

  • Download URL: py-_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.6.13 Linux/5.4.0-1040-azure

File hashes

Hashes for py-_cache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cf1e413b97388d62835cb461ea70ed01f876e03b02ff835cdabe54635f93a5a7
MD5 891d395505ab193fdd82eee688031399
BLAKE2b-256 8e27da4576a7cb7ac54fb550344cb5b68eae803ad3f706a76e8e9f3300c42610

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py__cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.6.13 Linux/5.4.0-1040-azure

File hashes

Hashes for py__cache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7e980648a962be86b4a93f5467638b11eb34c76d534b51b999df15310e82015
MD5 ac4a764139250ec8b5c4f256b9c5dce6
BLAKE2b-256 61acaa9cbfb7c07ff13903525fb7c218a3b4331c6e445c8ef5b174a8c78aa46f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page