PyZCache is dependency free python key value cache based file storage and json serialize
Project description
PyZCache is dependency free python key value cache based file storage and json serialize.
extra features:
- limit able stack cache
- option to add ttl (time to life) in cache content
- smart request
Installation
pip install zcache
example
basic example:
from zcache import Cache
import time
c = Cache(path="/tmp")
print("set foo=bar: ", c.set("foo", "bar"))
print("c size:", c.size())
print("c has foo: ", c.has("foo"))
print("c get foo: ", c.get("foo"))
print("c delete foo: ", c.delete("foo"))
print("c has foo: ", c.has("foo"))
print("c has spam:", c.has("spam"))
print("c set spam=eggs, ttl=3: ", c.set("spam", "eggs", ttl=3)) # cache with ttl
print("c has spam:", c.has("spam"))
print("sleep 3")
time.sleep(3)
print("c has spam:", c.has("spam"))
print("c size:", c.size())
example with limited stack:
from zcache import Cache
d = Cache(path="/tmp", limit=2)
d.reset() # reset cache stack to 0
print(d.set("one", 1)) # True
print(d.set("two", 2)) # True
print(d.set("three", 3)) # False out of stack limit
d.delete("one") # delete one item from stack
print(d.set("three", 3)) # True
SmartRequest
SmartRequest
is Simple HTTP Client with smart caching system provide by zcache
.
example usage of `SmartRequest(url, cache_path, cache_time, offline_ttl)``
from zcache import SmartRequest
req = SmartRequest("https://www.example.com")
print(req.is_loaded_from_cache) # check is response loaded from cache
response_headers = req.response.get('headers')
response_body = req.response.get('body')
to make advance request you can create costum url object with other library, for example:
from zcache import SmartRequest
import requests
class MyRequest:
url = "https://www.example.com"
def get():
"""
this method called by SmartRequest to retrieve content.
you can put request logic get, post etc and return tuple(headers=dict, body=str)
"""
ret = requests.get(MyRequest.url)
return dict(ret.headers), ret.text
req = SmartRequest(MyRequest)
note: caching for request media/binary content is possible with base64
encode.
License
MIT
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
Built Distribution
File details
Details for the file zcache-1.0.0.tar.gz
.
File metadata
- Download URL: zcache-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62d2daea8b28bc4f5e46f14fede5c2b47501c47e52647df08e6a8dcd88eb006c |
|
MD5 | d05c2794b69ed21e0934d51f0ada855a |
|
BLAKE2b-256 | 184914e0205864c9c2220f59c8de93fd118ef646d5bba6ca92d0fe93f5d4a360 |
File details
Details for the file zcache-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: zcache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f29535b2b3e8835e26f33d2cc520c67e63ae4eb8a5af1b6064c639a8f54b462 |
|
MD5 | d94f312edbac5b86dd82700c10e0a903 |
|
BLAKE2b-256 | 1b798a033e43a19430a79b63cf7b3ae6bcd76373b8161177613b47c3dc918d7d |