Skip to main content

Python function caching with compression

Project description

COMPRESSION-CACHE

AsyncPG Version

Установка

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

Uploaded Source

File details

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

File metadata

  • Download URL: compression_cache-0.0.7.tar.gz
  • Upload date:
  • Size: 4.1 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.7.tar.gz
Algorithm Hash digest
SHA256 230e0bec697181d78db8792de562fef748d979cb6a037a4f9546ae314599d66a
MD5 28f43343762bf3bff7ba35968a6e98c3
BLAKE2b-256 c2f41a93cb2468128c29ea580834daa3c5152c2778e75ef36dd5d6e6e009dfb0

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