Scrooge Cache
What is Scrooge?
Scrooge is a Smart Cache Storage for stronger gentlemen
Backend supports:
How can I use?
Scrooge is able to cache function returns based on its input arguments for a given time.
Rules:
- Just a unique namespace per backend instance;
- If you do not set expiration_time scrooge will 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 an undetermined time
import time
from scrooge import RedisBackend, Client
backend = RedisBackend(host='127.0.0.1', port=6379)
client = Client(cache_backend=backend)
# Cached for an undetermined 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 10 seconds
@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 an undetermined time
import time
from scrooge import MemcacheBackend, Client
backend = MemcacheBackend(host='127.0.0.1', port=11211)
client = Client(cache_backend=backend)
# Cached for an undetermined 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=11211)
client = Client(cache_backend=backend)
# Cached for 10 seconds
@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))
No caching
If you wanna a cacheless request, you can do this using the no_cache argument
import time
from scrooge import MemcacheBackend, Client
backend = MemcacheBackend(host='127.0.0.1', port=11211)
client = Client(cache_backend=backend)
# Cached for 10 seconds
@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))
# Cacheless request below
# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5, no_cache=True))
Force cache update
If you wanna force the cache update, you can do this using the force_cache_update argument
import time
from scrooge import MemcacheBackend, Client
backend = MemcacheBackend(host='127.0.0.1', port=11211)
client = Client(cache_backend=backend)
# Cached for 10 seconds
@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))
# Force cache update
# After 5 seconds the return will be {"p1": 4, "p2": 5}
print(function_to_be_cached(4,5, force_cache_update=True))
Run tests
pytest -ra
Metadata
Release files for scrooge-cache 0.1.3
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 |
|---|---|---|---|---|
| scrooge_cache-0.1.3.tar.gz | 4.3 kB | Without provenance | Details for scrooge_cache-0.1.3.tar.gz |
Release files / scrooge_cache-0.1.3.tar.gz
| Download URL | scrooge_cache-0.1.3.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8681799f466ec2f0a0666f01b9031c4b35eb7263e88980bc57217c39a8afaf0d
|
|
BLAKE2b-256 checksum How to use checksums |
ac8b361a391d32242a72f9a3469a089d5f5514f39ef9f684089e12bf1d117fcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3
|
Security for scrooge-cache 0.1.3
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.