-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: secondary cache loader #123
Conversation
43a651d
to
c63d67a
Compare
Codecov Report
@@ Coverage Diff @@
## master #123 +/- ##
==========================================
+ Coverage 95.10% 95.31% +0.20%
==========================================
Files 67 70 +3
Lines 1697 1772 +75
Branches 183 208 +25
==========================================
+ Hits 1614 1689 +75
Misses 81 81
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
c63d67a
to
458b5c8
Compare
458b5c8
to
cf92fec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very exciting!
packages/entity-secondary-cache-redis/src/RedisSecondaryEntityCache.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a subset of cases where we have unique composite keys (UNIQUE (a, b)
), we could tell the Entity framework that the pair (a, b)
is cacheable and have the cache invalidated when the same way single-field keys get their cache entries invalidated. I think this autoinvalidation could be built on top of the more flexible, generic secondary cacher in this PR.
(Autoinvalidation would not work for the case like "ORDER BY created_at DESC LIMIT 1" where there is no unique key.)
I think it is good the secondary cache has a separate TTL. We always can pass in the same TTL as the main cache adapter context.
packages/entity-secondary-cache-redis/src/RedisSecondaryEntityCache.ts
Outdated
Show resolved
Hide resolved
packages/entity-secondary-cache-redis/src/RedisSecondaryEntityCache.ts
Outdated
Show resolved
Hide resolved
...econdary-cache-redis/src/__integration-tests__/RedisSecondaryEntityCache-integration-test.ts
Outdated
Show resolved
Hide resolved
packages/entity/src/__tests__/EntitySecondaryCacheLoader-test.ts
Outdated
Show resolved
Hide resolved
...econdary-cache-redis/src/__integration-tests__/RedisSecondaryEntityCache-integration-test.ts
Outdated
Show resolved
Hide resolved
To accomplish this I think a generalized approach would be better than separate implementation. I could see it being done by factoring out what already exists in the loader into things like The tough thing will be figuring out what the set of keys to auto-invalidate are:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explicit null
s for lookup misses hopefully will be useful, especially from a typechecking perspective.
Why
This PR adds a concept,
EntitySecondaryCacheLoader
that allows for arbitrary entity caching and retrieval based on arbitrary load params and configurable backing store load.The idea is that there are cases in which the standard entity caching mechanism can't be used, generally loads using
loadManyByFieldEqualityConjunctionAsync
orloadManyByRawWhereClauseAsync
(both with limit 1), that a cache would still be useful; most commonly hot/critical paths where DB indexes aren't sufficient.This PR adds a general purpose read-through cache for an arbitrary load of a single entity field object by any means, which then constructs and authorizes that entity as normal. Note that cache invalidation cannot be inferred for this cacher, so it must be done manually in consumer code.
For example, Expo needs to load the most recent manifest for an app. To do this,
loadManyByFieldEqualityConjunctionAsync
with an order by timestamp and limit 1 is used. With this PR, a customEntitySecondaryCacheLoader
can be created and used at the callsite, and it can be invalidated manually when appropriate (when new manifest is written to the DB).Test Plan
Run all tests (including some new ones).