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

Add Len() to common interface #44

Merged
merged 4 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Interface[K comparable, V any] interface {
Keys() []K
// Delete deletes the item with provided key from the cache.
Delete(key K)
// Len returns the number of items in the cache.
Len() int
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add method of the Cache struct because this is breaking change.

I think good like below:

Len() int { return len(c.Keys()) }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that this my change below:

func (c *Cache[K, V]) Len() int {
	c.mu.Lock()
	defer c.mu.Unlock()
	return c.cache.Len()
}

should be changed to:

func (c *Cache[K, V]) Len() int {
	return len(c.Keys())
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@norri Yes!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think performance for c.cache.Len() is better than len(c.Keys()) because of for example sorting is not needed and all policies support Len(). However I can make the change if you think that it's better.

Copy link
Owner

@Code-Hex Code-Hex Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you measure the performance?
And also please implement all cache policies not just simple 🙏

I think this discussion is required all cache policies results.

I think performance for c.cache.Len() is better than len(c.Keys())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to test the performance. All other policies implement already Len() so no changes needed for those.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added benchmark test for both implementations and using just Len() is quite match faster:

$ go test -benchmem -bench BenchmarkLenWithKeys ./policy/simple
goos: darwin
goarch: amd64
pkg: github.com/Code-Hex/go-generics-cache/policy/simple
cpu: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
BenchmarkLenWithKeys-8           3777514               310.7 ns/op           104 B/op          3 allocs/op
PASS
ok      github.com/Code-Hex/go-generics-cache/policy/simple     1.640s
$ go test -benchmem -bench BenchmarkJustLen ./policy/simple
goos: darwin
goarch: amd64
pkg: github.com/Code-Hex/go-generics-cache/policy/simple
cpu: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
BenchmarkJustLen-8      1000000000               0.3075 ns/op          0 B/op          0 allocs/op
PASS

}

var (
Expand Down Expand Up @@ -253,6 +255,13 @@ func (c *Cache[K, V]) Delete(key K) {
c.cache.Delete(key)
}

// Len returns the number of items in the cache.
func (c *Cache[K, V]) Len() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.cache.Len()
}

// Contains reports whether key is within cache.
func (c *Cache[K, V]) Contains(key K) bool {
c.mu.Lock()
Expand Down
10 changes: 10 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ func ExampleCache_Keys() {
// [a b c]
}

func ExampleCache_Len() {
c := cache.New(cache.AsLFU[string, int]())
c.Set("a", 1)
c.Set("b", 1)
c.Set("c", 1)
fmt.Println(c.Len())
// Output:
// 3
}

func ExampleCache_Contains() {
c := cache.New(cache.AsLRU[string, int]())
c.Set("a", 1)
Expand Down
5 changes: 5 additions & 0 deletions policy/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ func (c *Cache[K, _]) Keys() []K {
func (c *Cache[K, V]) Delete(key K) {
delete(c.items, key)
}

// Len returns the number of items in the cache.
func (c *Cache[K, V]) Len() int {
return len(c.items)
}