Skip to main content

Python function caching with compression

Project description

COMPRESSION-CACHE

Установка

  1. Установка библиотеки:
    pip install compression-cache
    

Примеры:

Async:

Пример асинхронного кэширования можно найти в файле example.py.

import asyncio, faker, random
from typing import Dict, List, Union
from compression_cache import CacheTTL


async def get_accounts(count_account: int) -> List[Dict[str, Union[str, int]]]:
 print(f"Get new list accounts count_account: {count_account}")
 fake = faker.Faker()
 accounts: List[Dict[str, Union[str, int]]] = []
 for _ in range(count_account):
     account = {
         "id": random.randint(1000, 9999),
         "name": fake.user_name(),
         "first_name": fake.first_name(),
         "last_name": fake.last_name(),
     }
     accounts.append(account) # type: ignore
 return accounts


@CacheTTL(ttl=60 * 5, key_args=["count_account"], compressor_level=3)
async def async_function(count_account: int) -> List[Dict[str, Union[str, int]]]:
 return await get_accounts(count_account=count_account)


async def main():
 for count_account in [10, 20, 10, 20]:
     print(f"count_account: {count_account}")
     await async_function(count_account=count_account)


asyncio.run(main())

Sync:

Пример синхронного кэширования можно найти в файле example.py.

import faker, random
from typing import Dict, List, Union
from compression_cache import CacheTTL


def get_accounts(count_account: int) -> List[Dict[str, Union[str, int]]]:
 print(f"Get new list accounts count_account: {count_account}")
 fake = faker.Faker()
 accounts: List[Dict[str, Union[str, int]]] = []
 for _ in range(count_account):
     account = {
         "id": random.randint(1000, 9999),
         "name": fake.user_name(),
         "first_name": fake.first_name(),
         "last_name": fake.last_name(),
     }
     accounts.append(account) # type: ignore
 return accounts


@CacheTTL(ttl=60 * 5, key_args=["count_account"], compressor_level=3)
def async_function(count_account: int) -> List[Dict[str, Union[str, int]]]:
 return get_accounts(count_account=count_account)


def main():
 for count_account in [10, 20, 10, 20]:
     print(f"count_account: {count_account}")
     async_function(count_account=count_account)


main()

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

compression_cache-0.0.5.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file compression_cache-0.0.5.tar.gz.

File metadata

  • Download URL: compression_cache-0.0.5.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for compression_cache-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3b4792e043ac21ca8f3cc321a4e16baa210d1494ccb96ab853aae462dd60719a
MD5 572d549fcaed56fce6e373d2f0f42564
BLAKE2b-256 caa4a7fa188443128100978a0695fe98b626153637f053eb2ef51a7cf53277f4

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