Skip to main content

Scrooge Cache

Project description

Scrooge Cache

Build Status Coverage Status Pypi Version Python Version

alt text

What is Scrooge?

Scrooge is a Smart Cache Storage Only for Changes

Backend supports:

How can I use?

Scrooge is able to cache function returns for a given or infinite period.

Rules:

  • Just a unique namespace per backend instance;
  • If you do not set expiration_time scrooge it'll take infinite time;
  • The return of decorated function must be str or int or float or tuple, or list or dict;
  • If you use redis backend you can defined the db index using the parameter db=index, if you do not do this the default will be 0;

Installing

pip install scrooge-cache

Quick start

Using with redis as backend

This example below will cache the function return for the infinite time

import time
from scrooge import RedisBackend, Client

backend = RedisBackend(host='127.0.0.1', port=6379)
client = Client(cache_backend=backend)

# Cached for infinite time
@client.gentlemen_cache(namespace='f1')
def function_to_be_cached(p1, p2):
    time.sleep(5)
    return {"p1": p1, "p2": p2}

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

# The return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

This example below will cache the function return for 10 seconds

import time
from scrooge import RedisBackend, Client

backend = RedisBackend(host='127.0.0.1', port=6379)
client = Client(cache_backend=backend)

# Cached for infinite time
@client.gentlemen_cache(namespace='f1', expiration_time=10)
def function_to_be_cached(p1, p2):
    time.sleep(5)
    return {"p1": p1, "p2": p2}

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

# The return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

time.sleep(5)

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

Using with memcache as backend

This example below will cache the function return for the infinite time

import time
from scrooge import MemcacheBackend, Client

backend = MemcacheBackend(host='127.0.0.1', port=6379)
client = Client(cache_backend=backend)

# Cached for infinite time
@client.gentlemen_cache(namespace='f1')
def function_to_be_cached(p1, p2):
    time.sleep(5)
    return {"p1": p1, "p2": p2}

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

# The return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

This example below will cache the function return for 10 seconds

import time
from scrooge import MemcacheBackend, Client

backend = MemcacheBackend(host='127.0.0.1', port=6379)
client = Client(cache_backend=backend)

# Cached for infinite time
@client.gentlemen_cache(namespace='f1', expiration_time=10)
def function_to_be_cached(p1, p2):
    time.sleep(5)
    return {"p1": p1, "p2": p2}

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

# The return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

time.sleep(5)

# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5))

Run tests

pytest -ra

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

scrooge_cache-0.0.1.tar.gz (4.0 kB view hashes)

Uploaded Source

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