Skip to content

Instantly share code, notes, and snippets.

@codebutler
Last active November 13, 2023 17:20
Show Gist options
  • Save codebutler/398042ce27c73431dd40b0d2b775355c to your computer and use it in GitHub Desktop.
Save codebutler/398042ce27c73431dd40b0d2b775355c to your computer and use it in GitHub Desktop.

Proposal for OpenAPI Custom Extension: Automated Client-Side Cache Invalidation

Extension Name: x-cache-invalidates-operation-ids

Purpose

The x-cache-invalidates-operation-ids extension is designed for use in auto-generated client-side code, facilitating automated cache invalidation based on operation interactions. This extension allows API designers to specify operation IDs that should trigger cache invalidation in the client's local cache, ensuring data consistency and up-to-date information in client applications.

Usage

Applied within the paths section of an OpenAPI document, this extension targets individual API operations. It specifies which operations, identified by their operationId, should have their cache invalidated upon the execution of a given operation.

Structure

The x-cache-invalidates-operation-ids field is a list of operation IDs. These IDs are defined elsewhere in the API and indicate which operations' cache should be invalidated when the current operation is executed.

Example

paths:
  /users:
    post:
      operationId: createUser
      summary: Create a new user
      x-cache-invalidates-operation-ids: [listUsers, getUserStats]
    get:
      operationId: listUsers
      summary: List all users
      responses:
        ...

  /users/{userId}:
    put:
      operationId: updateUser
      summary: Update a user's details
      x-cache-invalidates-operation-ids: [getUser, getUserStats]
    get:
      operationId: getUser
      summary: Get a user's details
      responses:
        ...

  /users/stats:
    get:
      operationId: getUserStats
      summary: Get statistics about users
      responses:
        ...

Description

  • After creating a new user (createUser), auto-generated client code will automatically invalidate the cache for listUsers and getUserStats.
  • Updating user details (updateUser) triggers automatic cache invalidation for getUser and getUserStats.

Benefits

  • Automatic Cache Management: Streamlines cache invalidation in client-side applications, reducing manual coding requirements.
  • Data Consistency: Ensures clients always access the latest data by automatically clearing outdated cache entries.
  • Enhanced User Experience: Improves application responsiveness and reliability through efficient data management.

Considerations

  • The extension guides the generation of client code but relies on the client-side code generation tool's ability to interpret and implement this logic.
  • Operation IDs must be consistently unique and meaningful throughout the API for effective implementation of this extension.

Future Enhancements

  • Extend support for conditional cache invalidation based on response data or other operation outcomes.
  • Integration with popular client-side code generation tools to standardize the implementation of this cache invalidation strategy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment