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

[QUESTION]: Is there any way to implement an expired map counter with ristretto? #360

Open
pthethanh opened this issue Oct 2, 2023 · 0 comments
Labels
kind/question Something requiring a response.

Comments

@pthethanh
Copy link

Question.

I wonder if there is any way that I could implement a counter with ristretto without wrap it around a sync.Mutex?
For now, if I want to implement a expired map counter with ristretto, I have to do this:

c, err := ristretto.NewCache(&ristretto.Config{
		MaxCost:     100,
		NumCounters: 1000,
		BufferItems: 64,
	})
	if err != nil {
		panic(err)
	}

	wg := sync.WaitGroup{}
	wg.Add(100)
	mux := new(sync.Mutex)
	for i := 0; i < 100; i++ {
		go func() {
			defer wg.Done()
			mux.Lock()
			v, ok := c.Get("key")
			if ok {
				v, _ := v.(int64)
				v++
				c.SetWithTTL("key", v, 1, 30*time.Second)
				c.Wait()
			} else {
				v := int64(1)
				c.SetWithTTL("key", v, 1, 30*time.Second)
				c.Wait()
			}
			mux.Unlock()
		}()
	}
	wg.Wait()
	fmt.Println(c.Get("key"))

I wonder what is the proper way to implement the counter correctly? I can replace int64 by atomic.Int64 but it also requires initial all the keys in advance.

So I wonder if atomic operations will be supported in the near future? like LoadOrStore(), CompareAndSwap(), CompareAndDelete()

@pthethanh pthethanh added the kind/question Something requiring a response. label Oct 2, 2023
@pthethanh pthethanh changed the title [QUESTION]: Is there any way for implement an expired map counter with ristretto? [QUESTION]: Is there any way to implement an expired map counter with ristretto? Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Something requiring a response.
Projects
None yet
Development

No branches or pull requests

1 participant