[![status workflow test](https://github.com/guangrei/PyZcache/actions/workflows/python-app.yml/badge.svg)](https://github.com/guangrei/PyZcache/actions) [![status workflow build](https://github.com/guangrei/PyZcache/actions/workflows/release_to_pypi.yml/badge.svg)](https://github.com/guangrei/PyZcache/actions)
[![Downloads](https://static.pepy.tech/badge/zcache)](https://pepy.tech/project/zcache)
[![Downloads](https://static.pepy.tech/badge/zcache/month)](https://pepy.tech/project/zcache)
[![Downloads](https://static.pepy.tech/badge/zcache/week)](https://pepy.tech/project/zcache)
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:
```python
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:
```python
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)`:
```python
from zcache import SmartRequest
req = SmartRequest("https://www.example.com", cache_path="/tmp/request1.cache")
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:
```python
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, cache_path="/tmp/request2.cache")
```
note: caching for request media/binary content is possible with `base64` encode.
## License
MIT
Raw data
{
"_id": null,
"home_page": "https://github.com/guangrei/PyZCache",
"name": "zcache",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cache key value file json",
"author": "guangrei",
"author_email": "myawn@pm.me",
"download_url": "https://files.pythonhosted.org/packages/a2/cb/f82c1a20d7611c772e3af9db671a92da42724202f16e7c689c1b2e6e47df/zcache-1.0.1.tar.gz",
"platform": null,
"description": "[![status workflow test](https://github.com/guangrei/PyZcache/actions/workflows/python-app.yml/badge.svg)](https://github.com/guangrei/PyZcache/actions) [![status workflow build](https://github.com/guangrei/PyZcache/actions/workflows/release_to_pypi.yml/badge.svg)](https://github.com/guangrei/PyZcache/actions)\n[![Downloads](https://static.pepy.tech/badge/zcache)](https://pepy.tech/project/zcache)\n[![Downloads](https://static.pepy.tech/badge/zcache/month)](https://pepy.tech/project/zcache)\n[![Downloads](https://static.pepy.tech/badge/zcache/week)](https://pepy.tech/project/zcache)\n\nPyZCache is dependency free python key value cache based file storage and json serialize.\n\nextra features:\n- limit able stack cache\n- option to add ttl (time to life) in cache content\n- smart request\n\n## Installation\n```\npip install zcache\n```\n# example\n\nbasic example:\n```python\nfrom zcache import Cache\nimport time\n\nc = Cache(path=\"/tmp\")\nprint(\"set foo=bar: \", c.set(\"foo\", \"bar\"))\nprint(\"c size:\", c.size())\nprint(\"c has foo: \", c.has(\"foo\"))\nprint(\"c get foo: \", c.get(\"foo\"))\nprint(\"c delete foo: \", c.delete(\"foo\"))\nprint(\"c has foo: \", c.has(\"foo\"))\nprint(\"c has spam:\", c.has(\"spam\"))\nprint(\"c set spam=eggs, ttl=3: \", c.set(\"spam\", \"eggs\", ttl=3)) # cache with ttl\nprint(\"c has spam:\", c.has(\"spam\"))\nprint(\"sleep 3\")\ntime.sleep(3)\nprint(\"c has spam:\", c.has(\"spam\"))\nprint(\"c size:\", c.size())\n```\nexample with limited stack:\n```python\nfrom zcache import Cache\n\nd = Cache(path=\"/tmp\", limit=2)\nd.reset() # reset cache stack to 0\nprint(d.set(\"one\", 1)) # True\nprint(d.set(\"two\", 2)) # True\nprint(d.set(\"three\", 3)) # False out of stack limit\nd.delete(\"one\") # delete one item from stack\nprint(d.set(\"three\", 3)) # True\n```\n\n# SmartRequest\n\n`SmartRequest` is Simple HTTP Client with smart caching system provide by `zcache`.\n\nexample usage of `SmartRequest(url, cache_path, cache_time, offline_ttl)`:\n```python\nfrom zcache import SmartRequest\n\nreq = SmartRequest(\"https://www.example.com\", cache_path=\"/tmp/request1.cache\")\nprint(req.is_loaded_from_cache) # check is response loaded from cache\nresponse_headers = req.response.get('headers')\nresponse_body = req.response.get('body')\n```\nto make advance request you can create costum url object with other library, for example:\n```python\nfrom zcache import SmartRequest\nimport requests\n\nclass MyRequest:\n url = \"https://www.example.com\"\n \n def get():\n \"\"\"\n this method called by SmartRequest to retrieve content.\n you can put request logic get, post etc and return tuple(headers=dict, body=str)\n \"\"\"\n \n ret = requests.get(MyRequest.url)\n return dict(ret.headers), ret.text\n\n\nreq = SmartRequest(MyRequest, cache_path=\"/tmp/request2.cache\")\n```\nnote: caching for request media/binary content is possible with `base64` encode. \n## License\n\nMIT\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "PyZCache is dependency free python key value cache based file storage and json serialize",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/guangrei/PyZCache"
},
"split_keywords": [
"cache",
"key",
"value",
"file",
"json"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0ec9797c4927f0ccd994a88babce2b89542065564bdcad0841db6859cbcc8673",
"md5": "d1de13f790acf4adcc5063fb3cb8aa61",
"sha256": "523aef81cf90e956a9e229a65e19d1099e864dc828c2b03d85706d99895a7ad7"
},
"downloads": -1,
"filename": "zcache-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d1de13f790acf4adcc5063fb3cb8aa61",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6296,
"upload_time": "2023-08-13T16:44:02",
"upload_time_iso_8601": "2023-08-13T16:44:02.010943Z",
"url": "https://files.pythonhosted.org/packages/0e/c9/797c4927f0ccd994a88babce2b89542065564bdcad0841db6859cbcc8673/zcache-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a2cbf82c1a20d7611c772e3af9db671a92da42724202f16e7c689c1b2e6e47df",
"md5": "1f29424cd04723abfec2c5547c4a3436",
"sha256": "1676b825f75d7a95854cf0cb928de9fda87de94f59938da2f285ad0088436af4"
},
"downloads": -1,
"filename": "zcache-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "1f29424cd04723abfec2c5547c4a3436",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4353,
"upload_time": "2023-08-13T16:44:03",
"upload_time_iso_8601": "2023-08-13T16:44:03.401600Z",
"url": "https://files.pythonhosted.org/packages/a2/cb/f82c1a20d7611c772e3af9db671a92da42724202f16e7c689c1b2e6e47df/zcache-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-13 16:44:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "guangrei",
"github_project": "PyZCache",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "zcache"
}