Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Am K committed Sep 27, 2024
1 parent c496e7e commit be0372d
Show file tree
Hide file tree
Showing 20 changed files with 412 additions and 63 deletions.
72 changes: 29 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ d.delete("one") # delete one item from stack
print(d.set("three", 3)) # True
```

# Asynchronous

example asynchronous usage

```python
import asyncio
from zcache import AsyncCache

async def main():
c = await AsyncCache()
await c.set("test", "OK")
print(await c.get("test"))

if __name__ == '__main__':
asyncio.run(main())
```

# Storage and plugins

you can change storage and use plugins, for example:
Expand All @@ -59,57 +76,26 @@ 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

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

1. SmartRequest
1. [SmartRequest](https://github.com/guangrei/zcache/tree/main/tests_smartrequests.py)

`SmartRequest` is Simple HTTP Client with smart caching system provide by `zcache`.
`SmartRequest` is Simple HTTP Client with smart caching system based on `zcache`.

example usage of `SmartRequest(url, cache_path, cache_time, offline_ttl)`:
```python
from zcache.Extras.SmartRequest import SmartRequest
2.[AsyncSmartRequest](https://github.com/guangrei/zcache/tree/main/tests_smartrequests.py)

req = SmartRequest("https://www.example.com", cache_path="/tmp/request1.cache")
print(req.is_loaded_from_cache) # check if response is loaded from cache
response_headers = req.response.get('headers')
response_body = req.response.get('body')
```
to make advance request you can create custom url object with other library, for example:
```python
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/bytes)
"""

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


req = SmartRequest(MyRequest, cache_path="/tmp/request2.cache")
```
`AsyncSmartRequest` is asynchronous version of `SmartRequests`.

> from zcache v1.0.3 SmartRequest body support bytes and already use BytesCachePlugins to store large file/content.
3. [Queue](https://github.com/guangrei/zcache/tree/main/tests_queue.py)

2. Queue
`Queue` is Fifo Queue based on `zcache`.

4. [AsyncQueue](https://github.com/guangrei/zcache/tree/main/tests_queue.py)

`AsyncQueue` is asynchronous version of`zcache`.

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

q = Queue()
id = q.put("test")
q.exists(id)
q.empty()
q.size()
q.get()
```

## License

Expand Down
14 changes: 14 additions & 0 deletions tests/test_async_dicstorage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from unittest import IsolatedAsyncioTestCase
from zcache.Class.AsyncDatabase import AsyncDatabase
from zcache.Storage.AsyncDictStorage import AsyncDictStorage


class DBTest(IsolatedAsyncioTestCase):

async def test_database_or_cache(self):
c = await AsyncDatabase("/tmp/async_dicstorage_test.json", storage=AsyncDictStorage)
await c.reset()

expected = await c.set("foo", "bar")
self.assertEqual(expected, True)
13 changes: 13 additions & 0 deletions tests/test_dictstorage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
import unittest
from zcache.Class.Database import Database
from zcache.Storage.DictStorage import DictStorage


class DictStorageTest(unittest.TestCase):

def test_database_or_cache(self):
c = Database("/tmp/test_dict_storage.json", storage=DictStorage)
c.reset()
self.assertEqual(c.set("foo", "bar"), True)
self.assertEqual(c.size(), 1)
13 changes: 13 additions & 0 deletions tests/test_fcntlstorage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
import unittest
from zcache.Class.Database import Database
from zcache.Storage.FcntlStorage import FcntlStorage


class FcntlStorageTest(unittest.TestCase):

def test_database_or_cache(self):
c = Database("/tmp/test_fcntl_storage.json", storage=FcntlStorage)
c.reset()
self.assertEqual(c.set("foo", "bar"), True)
self.assertEqual(c.size(), 1)
23 changes: 23 additions & 0 deletions zcache/Class/AsyncDatabase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from zcache.Storage.AsyncFileStorage import AsyncFileStorage
from zcache.Interface.Storage import Storage as StorageInterface
from zcache.Interface.Plugins import Plugins as PluginsInterface
Expand Down
23 changes: 23 additions & 0 deletions zcache/Class/Database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from zcache.Storage.BaseFileStorage import BaseFileStorage
from zcache.Interface.Storage import Storage as StorageInterface
from zcache.Interface.Plugins import Plugins as PluginsInterface
Expand Down
23 changes: 23 additions & 0 deletions zcache/Extras/AsyncQueue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from zcache.Storage.AsyncFileStorage import AsyncFileStorage
from zcache.Class.AsyncDatabase import AsyncDatabase
import uuid
Expand Down
4 changes: 2 additions & 2 deletions zcache/Extras/AsyncSmartRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
The MIT License (MIT)
Copyright (c) 2022 PyZCache https://github.com/guangrei/PyZCache
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,7 +31,7 @@
@asyncinit
class AsyncSmartRequest:
"""
A class for making Smart HTTP requests with caching capabilities using PyZCache.
A class for making Smart HTTP requests with caching capabilities using zcache.
"""

async def __init__(
Expand Down
23 changes: 23 additions & 0 deletions zcache/Extras/Queue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from zcache.Storage.BaseFileStorage import BaseFileStorage
from zcache.Class.Database import Database
import uuid
Expand Down
4 changes: 2 additions & 2 deletions zcache/Extras/SmartRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
The MIT License (MIT)
Copyright (c) 2022 PyZCache https://github.com/guangrei/PyZCache
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -29,7 +29,7 @@

class SmartRequest:
"""
A class for making Smart HTTP requests with caching capabilities using PyZCache.
A class for making Smart HTTP requests with caching capabilities using zcache.
"""

def __init__(
Expand Down
23 changes: 23 additions & 0 deletions zcache/Interface/Plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from abc import ABC, abstractmethod


Expand Down
55 changes: 39 additions & 16 deletions zcache/Interface/Storage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# -*-coding:utf8;-*-
from abc import ABC, abstractmethod


class Storage(ABC):
@abstractmethod
def __init__(self, path):
pass

@abstractmethod
def load(self):
pass

@abstractmethod
def save(self, data):
pass
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from abc import ABC, abstractmethod


class Storage(ABC):
@abstractmethod
def __init__(self, path):
pass

@abstractmethod
def load(self):
pass

@abstractmethod
def save(self, data):
pass
23 changes: 23 additions & 0 deletions zcache/Plugins/AsyncBytesCachePlugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# -*-coding:utf8;-*-
"""
The MIT License (MIT)
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from zcache.Interface.Plugins import Plugins
import pickle
import os
Expand Down
Loading

0 comments on commit be0372d

Please sign in to comment.