Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve InMemoryLRUCache implementation. #2109

Merged
merged 6 commits into from
Dec 18, 2018

Conversation

abernix
Copy link
Member

@abernix abernix commented Dec 18, 2018

This PR contains a number of commits which make the existing InMemoryLRUCache more whole. The InMemoryLRUCache — which crutches almost entirely on the excellent lru-cache library — is the currently the default cache store which is used for REST Data Sources (i.e. RESTDataSource) and persisted queries — though both of those can be provided more complex cache stores (e.g. distributed cache stores such as Redis/Memcached).

In many cases though, the InMemoryLRUCache is an arguably less complex, yet still powerful, piece of machinery which can still be used to increase the performance of Apollo Server and seems deserving of a more consistent interface with the distributed stores. In that spirit, this PR brings:

  • Support for flush, via lru-cache's reset method.
  • Tests, which were previously missing.
  • Support for ttl expirations (defined during set) via lru-cache's maxAge parameter.

While it is certainly our current implementation, I'm not sure that the true
spirit of an in-memory key-value store is to only map strings to other strings.

While enforcing `<string, string>` might be necessary for some distributed
cache stores, it seems we shouldn't enforce it for `InMemoryLRUCache`.

By changing the `KeyValueCache` to default to `string` but allow other
options, we can allow the user to decide exactly how the RHS of this store
should be typed.

This does require a bit more flexible implementation of the `length`
calculator which is used for cache ejection, but that implementation will
become immediately useful when we start storing parsed ASTs in this KV store.
This implements the `flush` method, which is currently supported by both
Memcached and Redis, into the `InMemoryLRUCCache` cache.
Super nitty, but they are KeyValue stores, not KeyData stores.
This also re-enables the Expiration tests for this library, though I think I
will continue to leave the testsuite decomposed into more granular methods
for readability and future growth.

Note that this doesn't implement the same default `300` second TTL as the
other Memcache and Redis implementations currently do (a very debatable
default we should re-consider in the future).
@abernix abernix merged commit 8a1f999 into master Dec 18, 2018
@abernix abernix deleted the abernix/caching-tweak-prep-two branch December 18, 2018 09:15
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant