Skip to main content

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 pymockache.async_.decorators import AsyncCachingWrapper
from pymockache.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymockache-0.0.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymockache-0.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pymockache-0.0.2.tar.gz.

File metadata

  • Download URL: pymockache-0.0.2.tar.gz
  • Upload date:
  • Size: 3.7 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

Hashes for pymockache-0.0.2.tar.gz
Algorithm Hash digest
SHA256 48b8a32451e19fcb56c369c7626e51845ac4d9f246c23f5e64ff507ba8e86e0a
MD5 ddb04bcd8876530bd548d42b7ca34ad4
BLAKE2b-256 a04e023c5c1bb540b1bf6368ac889065609ba8a5bad370b0f6c4c1f5ae64b380

See more details on using hashes here.

File details

Details for the file pymockache-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymockache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 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

Hashes for pymockache-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 21e174b26d7011b2db4a10c3ea522589e8b27c2f48efbe6ab062e83e6ec1fa8a
MD5 c975f281c45b9b513582df9f43f95cf7
BLAKE2b-256 e63f561c023801c92a9f4801152696a5b4f13248d9fbe986f1c3c00ad965f1fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page