I have a scenario where I am using CloudFront to cache a user specific API response. The data in the API response will not change very often and is used across multiple places in my app. However, when the data DOES change, I would like the user to be able to hit a "refresh" button that forces CloudFront to go get new content and then cache this new content.
I have looked at CloudFront and the Cache-Control: no-cache and Cache-Control: max-age=0 headers, but as far as I can tell these are ignored by CloudFront.
I have also looked at invalidating cache based on a specific key, but this functionality does not seem to be supported (path based invalidation only which would affect all users instead of a specific one)
Changing one of the caching keys (a query string parameter for instance) to force a new request is also a possibility, but would require me to keep track of/manage caching keys on the client side in order to be able to access the cached response every time.
So I am down to trying to figure out how to do this with Lambda@Edge, but am struggling to find any examples of if this is possible.
CloudFront has worked great so far for my needs. I just need to be able to force it to refresh.
Is there anyway to accomplish this using Lambda? Or some other way I have not found (aside from writing my own caching mechanism using a database/redis/memcached/etc)?
I understand you want to cache a call to an API and the user identity is part of the cache key (ex. https://myapi/rsc?user=id).
You also want to control when the client is using the cached version or call the API to fetch a new result.
IMHO the best approach is to use versioning and add a new query parameter representing the version of your result (ex. https://myapi/rsc?user=id&version=v).
You can create a new API to update or retrieve the current version (ex. https://myapi/version). When the user hits refresh you call the API and increment the version. Then you use this new version for your API calls.