Skip to main content

Python function caching with compression

Project description

COMPRESSION-CACHE

Установка

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

Примеры:

Async:

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

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:

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

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.6.tar.gz (4.0 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: compression_cache-0.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 be014c632577730dd70eb9404f2a6dc8bfd262c27816574f629f5286eb36ee8e
MD5 454b35faa488c2c4c220c2b7fe440c8f
BLAKE2b-256 d7f4bb9a19d6ac9be417d556bd0a827edcc1760b68dd2735fe21dc9bfeaa41b5

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