Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Am K committed Sep 16, 2024
1 parent 50f23d2 commit ecc47d2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
57 changes: 42 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
[![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)
[![status workflow test](https://github.com/guangrei/zcache/actions/workflows/python-app.yml/badge.svg)](https://github.com/guangrei/zcache/actions)
[![status workflow build](https://github.com/guangrei/zcache/actions/workflows/release_to_pypi.yml/badge.svg)](https://github.com/guangrei/zcache/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
zcache is pure python implementation of key value Cache/Database with abstract storage and plugins support.

## Installation
```
Expand Down Expand Up @@ -51,13 +46,30 @@ d.delete("one") # delete one item from stack
print(d.set("three", 3)) # True
```

# SmartRequest
# Storage and plugins

you can change storage and use plugins, for example:

```python
from zcache import Cache
from zcache.Plugins.BytesCachePlugins import BytesCachePlugins
from zcache.Storage.BaseFileStorage import BaseFileStorage

c = Cache(storage=BaseFileStorage, plugins=BytesCachePlugins)
```
see list current available [storage](https://github.com/guangrei/zcache/tree/main/zcache/Storage) and [plugins](https://github.com/guangrei/zcache/tree/main/zcache/Plugins), you can also create your own storage and plugins.

## Extras

[extras](https://github.com/guangrei/zcache/tree/main/zcache/Extras) is several function based on zcache.

1. 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
from zcache.Extras.SmartRequest import SmartRequest

req = SmartRequest("https://www.example.com", cache_path="/tmp/request1.cache")
print(req.is_loaded_from_cache) # check if response is loaded from cache
Expand All @@ -66,25 +78,40 @@ response_body = req.response.get('body')
```
to make advance request you can create custom url object with other library, for example:
```python
from zcache import SmartRequest
import requests
from zcache.Extras.SmartRequest import SmartRequest

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)
you can put request logic get, post etc and return tuple(headers=dict, body=str/bytes)
"""

ret = requests.get(MyRequest.url)
return dict(ret.headers), ret.text
return dict(ret.headers), ret.content


req = SmartRequest(MyRequest, cache_path="/tmp/request2.cache")
```
note: caching for request media/binary content is possible with `base64` encode.

> from zcache 1.0.3 SmartRequest body support str & bytes and already use BytesCachePlugins to store large file/content.
2. Queue

```python
from zcache.Extras.Queue import Queue

q = Queue()
q.put("test", id="123") # id must be a string and optional (default random uuid)
q.exists("123")
q.peek() # view top item without enqueue
q.empty()
q.size()
q.get()
```

## License

MIT
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
license='MIT',
author="guangrei",
author_email="myawn@pm.me",
description="Key Value Database/Cache with multiple storage and plugins",
description="Key Value Database/Cache with abstract storage and plugins",
long_description=long_description,
long_description_content_type='text/markdown',
keywords="cache key value file json",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def _makeRequest(self, url, cache_name, cache):
if not isinstance(url, str):
try:
headers, body = url.get()
assert type(body) == str
return {"headers": headers, "body": body}
except BaseException as e:
if cache.has(cache_name+"_offline"):
Expand All @@ -67,7 +66,7 @@ def _makeRequest(self, url, cache_name, cache):
try:
response = request.urlopen(url)
headers, body = (dict(response.info()), response.read())
return {"headers": headers, "body": body.decode('utf-8')}
return {"headers": headers, "body": body}
except BaseException as e:
if cache.has(cache_name+"_offline"):
return False
Expand Down
Empty file added zcache/Extras/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions zcache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*-coding:utf8;-*-
from zcache.Class.Database import Database
from zcache.Class.Queue import Queue # noqa
from zcache.Class.SmartRequest import SmartRequest # noqa
from zcache.Extras.Queue import Queue # noqa
from zcache.Extras.SmartRequest import SmartRequest # noqa


Cache = Database
Expand Down

0 comments on commit ecc47d2

Please sign in to comment.