Skip to content

Feature request: Create a dedicated Cache utility in Powertools (based on current idempotency layer) #7069

Description

@walmsles

Use case

Customers are using the Powertools Idempotency utility for caching purposes. Idempotency is a specific implementation that includes a "caching" mechanism via the storage provider classes. Idempotency is a particular use case, and there is inbuilt functionality for dealing with the records in a specific way which I feel is dangerous for caching use cases.

Solution/User Experience

from aws_lambda_powertools.utilities.caching import (
    DynamoDBPersistenceLayer,
    Cache,
)

table = os.getenv("CACHE_TABLE", "")
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
cache =  Cache(persistence_store=peristsance_layer)
my_ttl = 300

def lambda_handler(event: dict, context: LambdaContext):
    try:
        previous_value = cache.get(event.order_id, "default_value")

        payment: Payment = create_subscription_payment(event, previous_value)

        cache.set(order_id, payment.detail, my_ttl)

        return {
            "payment_id": payment.payment_id,
            "message": "success",
            "statusCode": 200,
        }
    except Exception as exc:
        raise PaymentError(f"Error creating payment {str(exc)}")


def create_subscription_payment(event: dict) -> Payment:
    return Payment(**event)

Alternative solutions

Acknowledgment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions