Comments (4)
As of v5, we only support in memory caches because we cache the Thunk
(function), and not the value it holds. The reason for this limitation is because we uses the cache for both caching AND batching items. Eventually, I would like to decouple the batching cache from the value cache because it would allow more flexible caching strategies like the one you are suggesting.
That said, I do have a potential solution that I can offer you. I would suggest that you move your Redis logic into the batchFn
so that you can retrieve items in bulk and fall back to more expensive calls only when you have to.
For example you could do something like:
func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
results := make([]*dataloader.Result, len(keys))
// get what you can from redis
values, _ := redisClient.MGet(...keys.Keys()).Result()
// make a list of everything that was not found in redis
var cacheMisses map[int]string
for i := range keys {
if values[i] == redis.Nil {
cacheMisses[i] = keys[i].String()
} else {
results[i] = &dataloader.Result{values[i], nil}
}
}
// get the missing items from more expensive location (like DB)
for idx, key := range cacheMisses {
value, err := db.GetValues(key) // Pseudo code!
redisClient.Set(key, value)
results[idx] = &dataloader.Result{value, err}
}
return results
}
This solution is nice because you can potentially batch both expensive DB calls and Redis calls which, depending on use case, COULD increase performance from one-off get calls.
from dataloader.
Just to be clear, I need to store a text representation of the value I'm caching, so I'm trying to extract the value so I can marshal it. The go-redis.Set() func will only accept a string as the value.
from dataloader.
That's awesome. Thanks so much for your help! I'll give this a shot.
from dataloader.
I'm going to close this ticket. Feel free to re-open if you need more help.
from dataloader.
Related Issues (20)
- Metrics for cache hit rate etc. HOT 1
- Loader not batching properly HOT 4
- Tracing interface makes proper use of Links impossible HOT 3
- Non-memory cache and close context
- Question: why return a thunk? HOT 1
- Why is the return type of NewBatchFunc *Load[K,V]? HOT 1
- Passing query information to batch function? HOT 2
- How do dataloaders solve the 1-to-many relationship? HOT 1
- TestLoader/allows_clearAll_values_in_cache fails non-deterministically
- How to implement BigCache? HOT 1
- Can I use this with gqlgen? HOT 33
- What context is passed into batchFn? HOT 2
- Add generic support on next release? HOT 4
- Example with a database and graphql HOT 3
- Slice of Pointers (?)
- how to use redis as cache HOT 3
- expired DB certificate
- NoCache option doesn't seems to work in v6.0.0 HOT 3
- About caching in tutorial HOT 1
- Preventing panic after cancelled context HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataloader.