Coder Social home page Coder Social logo

Comments (9)

swithek avatar swithek commented on June 13, 2024 2

Yep, you can find it here. Sorry for the delay.

from ttlcache.

markandrus avatar markandrus commented on June 13, 2024 1

No, that sounds great! 🙌🏼

from ttlcache.

swithek avatar swithek commented on June 13, 2024

Yes, you are absolutely right! I'm just wondering what would be the best approach to fix this. I think to perform a lazy set of group, a new mutex would have to be introduced as well, which might degrade the performance for no real benefit.

So I'd say we have two options:

  • Expose group, as you said. There would be no breaking changes, but as a user I would expect the library to set this field itself, since there are no singleflight.Group options that I could configure.
  • Create a NewSuppressedLoader func that would set group and return a fully working loader. In this case, the Loader field could also be unexported and set inside the func (a parameter would be accepted for this), although that's a breaking change. On the other hand, since you are the first one to report this, maybe this change, albeit potentially breaking, might not affect that many users, if any at all. This way the design of the package wouldn't be negatively affected by pieces of code that would be left to be fixed in the next major release.

from ttlcache.

markandrus avatar markandrus commented on June 13, 2024

I think I prefer the second option; however, I'd request NewSuppressedLoader to take *singleflight.Group as an option. If the *singleflight.Group is nil, NewSuppressedLoader could create one; otherwise, it could use the provided value.

The reason I think this is desirable is because I am following your suggestion here #74 (comment). I want to be able to do

loader := NewSuppressedLoader[K, V](...)
cache.Get("key", ttlcache.WithLoader[K, V](loader))

sharing the same *singleflight.Group between SuppressedLoaders. WDYT?

from ttlcache.

swithek avatar swithek commented on June 13, 2024

Would the inner loader wrapped by the suppressed loader be also the same in all these places? If so, you could just reuse the same suppressed loader, e.g., attach it to the service that uses the cache:

type Service struct {
     cache *ttlcache.Cache
     loader ttlcache.Loader
}

func New() *Service {
      return Service{
           cache: ttlcache.New(),
           loader: ttlcache.NewSuppressedLoader(...),
      } 
}

func (s *Service) Get1() {
       item := s.cache.Get("key", ttlcache.WithLoader(s.loader))
}

func (s *Service) Get2() {
       item := s.cache.Get("key", ttlcache.WithLoader(s.loader))
}

from ttlcache.

markandrus avatar markandrus commented on June 13, 2024

I do something like this, so I don't believe I can share the loader:

type Service struct {
	cache *ttlcache.Cache
	group *singleflight.Group
}

func (s *Service) Get(ctx context.Context, key K) V {
	var loaded V

	loader := func(cache *ttlcache.Cache, key K) *ttlcache.Item {
		// 1. Make an API call, passing ctx.
		// 2. Set loaded to the API result.
		// 3. Update the cache if the API result can be cached; otherwise, return nil.
	}

	loaderFunc := ttlcache.LoaderFunc(loader)

	suppressedLoader := SuppressedLoader{
		Loader: loaderFunc,
		group:  group,
	}

	if cached := s.cache.Get(key, ttlcache.WithLoader(suppressedLoader)); cached != nil {
		return cached.Value()
	}

	return loaded
}

from ttlcache.

swithek avatar swithek commented on June 13, 2024

I see. In that case, the required changes are as follows:

  • Transform the embedded Loader into an unexported loader Loader field.
  • Create a NewSuppressedLoader(loader Loader, group *singleflight.Group) func. If group is nil, it is automatically created:
func NewSuppressedLoader(loader Loader, group *singleflight.Group) *SuppressedLoader {
    if group == nil {
        group = &singleflight.Group{}
    }
    
    return &SuppressedLoader{
        loader: loader,
        group: group,
    }
}

Have I missed anything?

from ttlcache.

austinbyers avatar austinbyers commented on June 13, 2024

I just ran into this same issue! I love the SuppressedLoader, but it's not in the latest release. Any chance we can get a new release to include this fix?

from ttlcache.

austinbyers avatar austinbyers commented on June 13, 2024

Awesome, thanks for the quick response!

from ttlcache.

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.