apicache-plus icon indicating copy to clipboard operation
apicache-plus copied to clipboard

How to replace existing cached data for endpoint?

Open AllanCochrane opened this issue 2 years ago • 0 comments

I would like to replace the existing data returned by a Redis-cached endpoint.

I have a background process that is given the key for the cache as generated by a call to getKey(), so I should be using the same key as apicache-plus does.

My issue is that the although the background process inserts new data into the cache, when the endpoint is hit again then no data is returned, it's as if the cacheObject does not contain the data:

Printing out the cache object that is stored by the initial cache of the endpoint gives:

[apicache] AC: cacheObject = { headers: { 'x-powered-by': 'Express', 'access-control-allow-origin': '*', 'content-type': 'application/json; charset=utf-8', etag: 'W/"a11-7HqZ9k3jIh29rVTC8CNeY8qUBTE"', 'cache-control': 'max-age=30, must-revalidate', vary: 'Accept-Encoding', 'content-encoding': 'br' }, 'data-extra-pttl': 238, status: 200, encoding: 'binary', 'data-token': 'a668dc91-4bca-42a0-8b46-7ddc467f2034', timestamp: 1664485660278, key: 'get/http://localhost/api/v1/dashboard/net?branch=baz&project=bar&user=foo{}' }

The endpoint returns the expected cache data.

But when I call apicache.set(key,data) with the same key as in the structure above then the cache object becomes:

[apicache] AC: cacheObject = {"key":"get/http://localhost/api/v1/dashboard/net?branch=baz&project=bar&user=foo{}","data":{"computedAt":1664485680700,"data":[{...}]}}

and the endpoint returns nothing at all.

The newly stored data doesn't have same shape as the data that was originally stored.

So, how should we use apicache.set() to update the cached data of an endpoint?

Thanks

AllanCochrane avatar Sep 29 '22 21:09 AllanCochrane