Giter Site home page Giter Site logo

pycache's Introduction

Functools

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

pycache's People

Contributors

huiibuh avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

pycache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.