Time Cache
Project description
==============
timecache
==============
`timecache` is a timeout-based cache implementation for Python 3.x without
any external dependencies. Depending on the selected backend, the cache can be
made persistent to survive a fresh start of a script (currently, json and
pickle files are supported as backend).
Warning: this is an early alpha version so the interface may change in future
versions.
Setup
-----
Install `timecache` using `pip`:
::
pip install -U timecache
Example
-------
A cache with a memory backend can be created as follows:
::
import time
from timecache import Cache, FIVE_SECONDS
# create cache with memory backend
cache = Cache()
# add a value to the cache with a default duration of 1 minute
cache["one"] = 1
# add a value to the cache with a duration of 5 seconds
cache.set("two", 2, FIVE_SECONDS)
print("two in cache: {}".format("two" in cache))
# show cache entry
print(cache)
# wait for 6 s
time.sleep(6)
# show cache entry
print(cache)
print("two in cache: {}".format("two" in cache))
A cache with a json file backend can be created as follows:
::
import time
from timecache import Cache, JsonBackend
# create cache with json backend
cache = Cache(backend=JsonBackend())
if not cache.is_existing:
# add a value to the cache with a default duration of 1 minute
print("adding new value to the cache...")
cache["one"] = 1
print("saving cache to file '{}'".format(cache.filename))
cache.save()
# show cache entry
print(cache)
When calling the script again the cached value will be loaded from the
json file.
Decorators
----------
There are also the three decorators `memorycache`, `jsoncache`,
`picklecache` that allows an easy caching of function return values.
::
import time
from timecache import jsoncache, FIVE_SECONDS
@jsoncache(cache_filename="add.json", ttl=FIVE_SECONDS)
def add(x, y):
return x + y
# new cache entry
print(add(1, 2))
# new cache entry
print(add(1, 3))
# load existing cache entry
print(add(1, 2))
# wait for 6 s
time.sleep(6)
# new cache entry, since old entry has expired
print(add(1, 2))
Debugging
-------
To debug the cache you can simply set the debug level.
::
import logging
logging.basicConfig(level=logging.DEBUG)
Testing
-------
For testing additionally install `nose` and then run the tests:
::
pip install nose
nosetests
Hint
----
If you do not need a time-based cache, consider the `lru_cache` function of
Python's `functools` module
(https://docs.python.org/3/library/functools.html).
timecache
==============
`timecache` is a timeout-based cache implementation for Python 3.x without
any external dependencies. Depending on the selected backend, the cache can be
made persistent to survive a fresh start of a script (currently, json and
pickle files are supported as backend).
Warning: this is an early alpha version so the interface may change in future
versions.
Setup
-----
Install `timecache` using `pip`:
::
pip install -U timecache
Example
-------
A cache with a memory backend can be created as follows:
::
import time
from timecache import Cache, FIVE_SECONDS
# create cache with memory backend
cache = Cache()
# add a value to the cache with a default duration of 1 minute
cache["one"] = 1
# add a value to the cache with a duration of 5 seconds
cache.set("two", 2, FIVE_SECONDS)
print("two in cache: {}".format("two" in cache))
# show cache entry
print(cache)
# wait for 6 s
time.sleep(6)
# show cache entry
print(cache)
print("two in cache: {}".format("two" in cache))
A cache with a json file backend can be created as follows:
::
import time
from timecache import Cache, JsonBackend
# create cache with json backend
cache = Cache(backend=JsonBackend())
if not cache.is_existing:
# add a value to the cache with a default duration of 1 minute
print("adding new value to the cache...")
cache["one"] = 1
print("saving cache to file '{}'".format(cache.filename))
cache.save()
# show cache entry
print(cache)
When calling the script again the cached value will be loaded from the
json file.
Decorators
----------
There are also the three decorators `memorycache`, `jsoncache`,
`picklecache` that allows an easy caching of function return values.
::
import time
from timecache import jsoncache, FIVE_SECONDS
@jsoncache(cache_filename="add.json", ttl=FIVE_SECONDS)
def add(x, y):
return x + y
# new cache entry
print(add(1, 2))
# new cache entry
print(add(1, 3))
# load existing cache entry
print(add(1, 2))
# wait for 6 s
time.sleep(6)
# new cache entry, since old entry has expired
print(add(1, 2))
Debugging
-------
To debug the cache you can simply set the debug level.
::
import logging
logging.basicConfig(level=logging.DEBUG)
Testing
-------
For testing additionally install `nose` and then run the tests:
::
pip install nose
nosetests
Hint
----
If you do not need a time-based cache, consider the `lru_cache` function of
Python's `functools` module
(https://docs.python.org/3/library/functools.html).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
timecache-0.0.4.tar.gz
(6.9 kB
view details)
File details
Details for the file timecache-0.0.4.tar.gz
.
File metadata
- Download URL: timecache-0.0.4.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ba37e717977d5d0845145cdf9419aeab3e3a720c6eb5a0a23e4b5f28a1decd0 |
|
MD5 | 5a1ea6e80a5fe04d10d79bf10d58b31c |
|
BLAKE2b-256 | d5ccfbaaca1577bd43ff627bb609171e9add0967ade1351d707eed882bc9cdc1 |