How to use

TLDR

Just simple put @cache @cache over your method.

Example:

from methodcache import cache

@cache
def dothings(*args, **kwargs):
    pass

@cache

Is the caching decorator. use this decorator on methods you want to cache with the following optional kwargs:

  • store
  • category
  • ttl

store

For Attributes and thier documentaiton see methodcache.cache.cache

@cache can used without any parameters.

If the kwarg store is missing, it creates a default Store. This default Store are safed as static attribute named _default_store in the class methodcache.store.Store

category

If the kwarg category not set, a category named default was used

Categories can be seperated in “groups”. You can define in which group the current method is cached. The groups can be interleaved via :. eg. car:manufacturer:model

Demo

from methodcache import cache

@cache(category="car:manufacturer")
def get_manufacturer(*args, **kwargs):
    return ["BMW", "Opel", "VW", "Honda"]

@cache(category="car:manufacturer:model")
def get_model(manufacturer):
    models = {
        "BMW": ["CarType1", "CarType2", "CarType3"]
        "Opel": ["CarType4", "CarType5", "CarType6"]
    }
    return models[manufacturer]

manufacturer = get_manufacturer()
get_model(manufacturer[0])

If these example code was executed the Store objects the informations like:

{
    "car": {
        "manufacturer": {
            "method_store":{
                "get_manufacturer": ["BMW", "Opel", "VW", "Honda"]
            },
            "model": {
                "method_store": {
                    "get_model;BMW": ["CarType1", "CarType2", "CarType3"]
                }
            }
        }

    }
}

ttl

If the kwarg ttl is not set a default ttl of 5 minutes are set.

You can define a TTL by createing a Store object. This TTL from Store can be overwriten by @cache

TTLs are always in Seconds

# TTL of 5 Minutes
@cache(ttl=60*5)

The code Above is strongly simplified. In orginal software the arguments are also check.

The code Above is strongly simplified. In orginal software the arguments are also check.

Store

The Store object contains all caching information.

By initializeation you can set a default ttl.

st = Store(ttl=60*5)

to use the store as cache in for an app define it as handover parameters store

st = Store(ttl=60*5)

@cache(store=st)
def dothings()
    pass

You can define multiple Stores to seperate your Application section from each other

Full Example

import time
from methodcache import cache, Store

st = Store(ttl=5)


class Car:

    def __init__(self, serial_number, milage):
        self.serial_number = serial_number
        self.milage = milage


class CarManager:

    @cache(store=st, category="car")
    def get_cars(self):
        time.sleep(2)
        return [Car("JKaHO3hoHOe4GHOy4", 234214), Car("AJAHWho4HOI46HIOA4t", 34571)]

    @cache(store=st, category="car:manufacturer", ttl=5)
    def get_manufacturer(self):
        time.sleep(3)
        return ["BMW", "Opel", "Ford"]


class FruitManager:

    @cache(store=st, category="fruit")
    def get_fruits(self):
        time.sleep(3)
        return ["Apple", "Banana", "Pineapple"]

    @cache(store=st, category="fruit:country")
    def get_country(self):
        time.sleep(3)
        return ["Germany", "Ecuador", "Costa Rica"]


fruits = FruitManager()
print()
print("Without Cache")
print(fruits.get_fruits())
print(fruits.get_country())
print()
print("With Cache")
print("-"*10)
print(fruits.get_fruits())
print(fruits.get_country())


cars = CarManager()
print()
print("Without Cache")
print(cars.get_cars())
print(cars.get_manufacturer())
print()
print("With Cache")
print("-"*10)
print(cars.get_cars())
print(cars.get_manufacturer())
print()
print("With Expired TTL")
print("-"*10)
print(fruits.get_country())
print()
print("Get All Categorys")
print("-"*10)
print(st.get_all_categorys())
print(st.get_category("car"))