Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Athroniaeth_keyshield /examples /example_cached.py
| import asyncio | |
| import math | |
| import os | |
| import time | |
| from contextlib import contextmanager | |
| from keyshield import ApiKeyService | |
| from keyshield.hasher.argon2 import Argon2ApiKeyHasher | |
| from keyshield.repositories.in_memory import InMemoryApiKeyRepository | |
| from keyshield.services.cached import CachedApiKeyService | |
| # Set env var to override default pepper | |
| # Using a strong, unique pepper is crucial for security | |
| # Default pepper is insecure and should not be used in production | |
| pepper = os.getenv("SECRET_PEPPER") | |
| hasher = Argon2ApiKeyHasher(pepper=pepper) | |
| # default hasher is Argon2 with a default pepper (to be changed in prod) | |
| repo = InMemoryApiKeyRepository() | |
| def benchmark(n: int): | |
| time_start = time.perf_counter() | |
| yield | |
| time_end = time.perf_counter() | |
| time_elapsed = time_end - time_start | |
| ops_per_sec = math.trunc(n / time_elapsed) | |
| print(f" Elapsed time: {time_elapsed:.6f} seconds ({ops_per_sec:,} ops/sec)\n") | |
| async def main(): | |
| n = 100 | |
| for service in [ | |
| # Must use Argon2 hash each call | |
| ApiKeyService(repo=repo, hasher=hasher), | |
| # Use Argon2 once and cache the result | |
| CachedApiKeyService(repo=repo, hasher=hasher), | |
| ]: | |
| print(f"{service.__class__.__name__}") | |
| print("- First operation (uncached):") | |
| _, api_key = await service.create(name="dev") | |
| with benchmark(1): | |
| await service.verify_key(api_key) | |
| print(f"- Subsequent {n} operations (cached where applicable):") | |
| with benchmark(n): | |
| for _ in range(n): | |
| await service.verify_key(api_key) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 1.67 kB
- Xet hash:
- 5496879183ac6339e7edf4a2c12468d75ebb7137fad821f423169b179f59cbd6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.