Caching for separated functions
Project description
Pymockache
Caching lib, which makes possible to cache function result. It works like mock, but mocking is static result. Library writes first computed result of function to caching backend and restores result from it for subsequent function calls with the same properties.
First of all it was made for async and redis as backend.
How to run redis into docker:
docker run --name caching-redis -p 6379:6379 -d redis
How to use:
from lib.async_.decorators import AsyncCachingWrapper
from lib.async_.backends.redis import AsyncRedisCachingBackend
import aiohttp
import asyncio
import logging
import time
logger = logging.getLogger(__name__)
caching_backend = AsyncRedisCachingBackend(
redis_host="redis://localhost:6379/0"
)
@AsyncCachingWrapper(
backend=caching_backend,
sign_variables=["page_url"],
expire_milliseconds=100000,
logger=logger,
log_active=True
)
async def get_page_text(page_url) -> str:
"""
Function returns text of http response from page_url.
"""
async with aiohttp.ClientSession() as session:
async with session.get(page_url) as response:
await asyncio.sleep(2) # Little sleep for clarity of the result
html = await response.text()
return html
async def main():
urls_list = [
"https://www.google.com/",
"https://www.python.org",
"https://www.google.com/",
"https://www.python.org"
]
for url in urls_list:
t0 = time.time()
text = await get_page_text(page_url=url)
t1 = time.time()
delta = t1 - t0
print(f"Url: {url}. Text length: {len(text)}. Total time: {str(delta)}")
if __name__ == '__main__':
asyncio.run(main())
Result:
Url: https://www.google.com/. Text length: 13337. Total time: 2.237496852874756
Url: https://www.python.org. Text length: 50053. Total time: 2.20656418800354
Url: https://www.google.com/. Text length: 13337. Total time: 0.010967254638671875
Url: https://www.python.org. Text length: 50053. Total time: 0.012981891632080078
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymockache-0.0.1.tar.gz.
File metadata
- Download URL: pymockache-0.0.1.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8e00e3d5eab90fff9d93e14b32363adf4bc428e263ce29a754d8efe1ba686a4
|
|
| MD5 |
226d3f0301bb5ede45671d00c2ae16f8
|
|
| BLAKE2b-256 |
2fb2325f6e76a35746cb298daa433225cf7f5614903132407f7389e2d68c5896
|
File details
Details for the file pymockache-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pymockache-0.0.1-py3-none-any.whl
- Upload date:
- Size: 2.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0841cd83a7804935c2a42ce1da53ade2efd7f5a9c01e6652958272ff0c158ece
|
|
| MD5 |
940860f2a9aecbffb14dfffa0e8335f4
|
|
| BLAKE2b-256 |
c93f0ff2bc40bd69e11c62100a418c2b35ce9514a0b37c2f74e2bc6dc9746e42
|