Skip to content

Commit be0372d

Browse files
author
Am K
committed
update
1 parent c496e7e commit be0372d

20 files changed

+412
-63
lines changed

README.md

+29-43
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ d.delete("one") # delete one item from stack
4646
print(d.set("three", 3)) # True
4747
```
4848

49+
# Asynchronous
50+
51+
example asynchronous usage
52+
53+
```python
54+
import asyncio
55+
from zcache import AsyncCache
56+
57+
async def main():
58+
c = await AsyncCache()
59+
await c.set("test", "OK")
60+
print(await c.get("test"))
61+
62+
if __name__ == '__main__':
63+
asyncio.run(main())
64+
```
65+
4966
# Storage and plugins
5067

5168
you can change storage and use plugins, for example:
@@ -59,57 +76,26 @@ c = Cache(storage=BaseFileStorage, plugins=BytesCachePlugins)
5976
```
6077
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.
6178

62-
## Extras
79+
# Extras
6380

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

66-
1. SmartRequest
83+
1. [SmartRequest](https://github.com/guangrei/zcache/tree/main/tests_smartrequests.py)
6784

68-
`SmartRequest` is Simple HTTP Client with smart caching system provide by `zcache`.
85+
`SmartRequest` is Simple HTTP Client with smart caching system based on `zcache`.
6986

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

74-
req = SmartRequest("https://www.example.com", cache_path="/tmp/request1.cache")
75-
print(req.is_loaded_from_cache) # check if response is loaded from cache
76-
response_headers = req.response.get('headers')
77-
response_body = req.response.get('body')
78-
```
79-
to make advance request you can create custom url object with other library, for example:
80-
```python
81-
from zcache.Extras.SmartRequest import SmartRequest
82-
83-
class MyRequest:
84-
url = "https://www.example.com"
85-
86-
def get():
87-
"""
88-
this method called by SmartRequest to retrieve content.
89-
you can put request logic get, post etc and return tuple(headers=dict, body=str/bytes)
90-
"""
91-
92-
ret = requests.get(MyRequest.url)
93-
return dict(ret.headers), ret.content
94-
95-
96-
req = SmartRequest(MyRequest, cache_path="/tmp/request2.cache")
97-
```
89+
`AsyncSmartRequest` is asynchronous version of `SmartRequests`.
9890

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

101-
2. Queue
93+
`Queue` is Fifo Queue based on `zcache`.
94+
95+
4. [AsyncQueue](https://github.com/guangrei/zcache/tree/main/tests_queue.py)
96+
97+
`AsyncQueue` is asynchronous version of`zcache`.
10298

103-
```python
104-
from zcache.Extras.Queue import Queue
105-
106-
q = Queue()
107-
id = q.put("test")
108-
q.exists(id)
109-
q.empty()
110-
q.size()
111-
q.get()
112-
```
11399

114100
## License
115101

tests/test_async_dicstorage.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import IsolatedAsyncioTestCase
3+
from zcache.Class.AsyncDatabase import AsyncDatabase
4+
from zcache.Storage.AsyncDictStorage import AsyncDictStorage
5+
6+
7+
class DBTest(IsolatedAsyncioTestCase):
8+
9+
async def test_database_or_cache(self):
10+
c = await AsyncDatabase("/tmp/async_dicstorage_test.json", storage=AsyncDictStorage)
11+
await c.reset()
12+
13+
expected = await c.set("foo", "bar")
14+
self.assertEqual(expected, True)

tests/test_dictstorage.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
import unittest
3+
from zcache.Class.Database import Database
4+
from zcache.Storage.DictStorage import DictStorage
5+
6+
7+
class DictStorageTest(unittest.TestCase):
8+
9+
def test_database_or_cache(self):
10+
c = Database("/tmp/test_dict_storage.json", storage=DictStorage)
11+
c.reset()
12+
self.assertEqual(c.set("foo", "bar"), True)
13+
self.assertEqual(c.size(), 1)

tests/test_fcntlstorage.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
import unittest
3+
from zcache.Class.Database import Database
4+
from zcache.Storage.FcntlStorage import FcntlStorage
5+
6+
7+
class FcntlStorageTest(unittest.TestCase):
8+
9+
def test_database_or_cache(self):
10+
c = Database("/tmp/test_fcntl_storage.json", storage=FcntlStorage)
11+
c.reset()
12+
self.assertEqual(c.set("foo", "bar"), True)
13+
self.assertEqual(c.size(), 1)

zcache/Class/AsyncDatabase.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from zcache.Storage.AsyncFileStorage import AsyncFileStorage
326
from zcache.Interface.Storage import Storage as StorageInterface
427
from zcache.Interface.Plugins import Plugins as PluginsInterface

zcache/Class/Database.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from zcache.Storage.BaseFileStorage import BaseFileStorage
326
from zcache.Interface.Storage import Storage as StorageInterface
427
from zcache.Interface.Plugins import Plugins as PluginsInterface

zcache/Extras/AsyncQueue.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from zcache.Storage.AsyncFileStorage import AsyncFileStorage
326
from zcache.Class.AsyncDatabase import AsyncDatabase
427
import uuid

zcache/Extras/AsyncSmartRequest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
The MIT License (MIT)
44
5-
Copyright (c) 2022 PyZCache https://github.com/guangrei/PyZCache
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
@@ -31,7 +31,7 @@
3131
@asyncinit
3232
class AsyncSmartRequest:
3333
"""
34-
A class for making Smart HTTP requests with caching capabilities using PyZCache.
34+
A class for making Smart HTTP requests with caching capabilities using zcache.
3535
"""
3636

3737
async def __init__(

zcache/Extras/Queue.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from zcache.Storage.BaseFileStorage import BaseFileStorage
326
from zcache.Class.Database import Database
427
import uuid

zcache/Extras/SmartRequest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
The MIT License (MIT)
44
5-
Copyright (c) 2022 PyZCache https://github.com/guangrei/PyZCache
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
@@ -29,7 +29,7 @@
2929

3030
class SmartRequest:
3131
"""
32-
A class for making Smart HTTP requests with caching capabilities using PyZCache.
32+
A class for making Smart HTTP requests with caching capabilities using zcache.
3333
"""
3434

3535
def __init__(

zcache/Interface/Plugins.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from abc import ABC, abstractmethod
326

427

zcache/Interface/Storage.py

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
# -*-coding:utf8;-*-
2-
from abc import ABC, abstractmethod
3-
4-
5-
class Storage(ABC):
6-
@abstractmethod
7-
def __init__(self, path):
8-
pass
9-
10-
@abstractmethod
11-
def load(self):
12-
pass
13-
14-
@abstractmethod
15-
def save(self, data):
16-
pass
1+
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
25+
from abc import ABC, abstractmethod
26+
27+
28+
class Storage(ABC):
29+
@abstractmethod
30+
def __init__(self, path):
31+
pass
32+
33+
@abstractmethod
34+
def load(self):
35+
pass
36+
37+
@abstractmethod
38+
def save(self, data):
39+
pass

zcache/Plugins/AsyncBytesCachePlugins.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# -*-coding:utf8;-*-
2+
"""
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2022 zcache https://github.com/guangrei/zcache
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
225
from zcache.Interface.Plugins import Plugins
326
import pickle
427
import os

0 commit comments

Comments
 (0)