Giter Site home page Giter Site logo

Comments (4)

nicksrandall avatar nicksrandall commented on September 27, 2024 6

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.

simonrycroft avatar simonrycroft commented on September 27, 2024

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.

simonrycroft avatar simonrycroft commented on September 27, 2024

That's awesome. Thanks so much for your help! I'll give this a shot.

from dataloader.

nicksrandall avatar nicksrandall commented on September 27, 2024

I'm going to close this ticket. Feel free to re-open if you need more help.

from dataloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.