-
Notifications
You must be signed in to change notification settings - Fork 186
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
update roles cache to use sync.cache #1367
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Enhancement: use sync.cache for roles cache | ||
|
||
Tags: ocis-pkg | ||
|
||
update ocis-pkg/roles cache to use ocis-pkg/sync cache | ||
|
||
https://github.com/owncloud/ocis/pull/1367 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,35 @@ | ||
package roles | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/owncloud/ocis/ocis-pkg/sync" | ||
settings "github.com/owncloud/ocis/settings/pkg/proto/v0" | ||
) | ||
|
||
// entry extends a bundle and adds a TTL | ||
type entry struct { | ||
*settings.Bundle | ||
inserted time.Time | ||
} | ||
|
||
// cache is a cache implementation for roles, keyed by roleIDs. | ||
type cache struct { | ||
entries map[string]entry | ||
size int | ||
ttl time.Duration | ||
m sync.Mutex | ||
sc sync.Cache | ||
ttl time.Duration | ||
} | ||
|
||
// newCache returns a new instance of Cache. | ||
func newCache(size int, ttl time.Duration) cache { | ||
func newCache(capacity int, ttl time.Duration) cache { | ||
return cache{ | ||
size: size, | ||
ttl: ttl, | ||
entries: map[string]entry{}, | ||
sc: sync.NewCache(capacity), | ||
} | ||
} | ||
|
||
// get gets a role-bundle by a given `roleID`. | ||
func (c *cache) get(roleID string) *settings.Bundle { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
|
||
if _, ok := c.entries[roleID]; ok { | ||
return c.entries[roleID].Bundle | ||
if ce := c.sc.Load(roleID); ce != nil { | ||
return ce.V.(*settings.Bundle) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// set sets a roleID / role-bundle. | ||
func (c *cache) set(roleID string, value *settings.Bundle) { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
|
||
if !c.fits() { | ||
c.evict() | ||
} | ||
|
||
c.entries[roleID] = entry{ | ||
value, | ||
time.Now(), | ||
} | ||
} | ||
|
||
// evict frees memory from the cache by removing entries that exceeded the cache TTL. | ||
func (c *cache) evict() { | ||
for i := range c.entries { | ||
if c.entries[i].inserted.Add(c.ttl).Before(time.Now()) { | ||
delete(c.entries, i) | ||
} | ||
} | ||
} | ||
|
||
// fits returns whether the cache fits more entries. | ||
func (c *cache) fits() bool { | ||
return c.size > len(c.entries) | ||
} | ||
c.sc.Store(roleID, value, time.Now().Add(c.ttl)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package roles | ||
|
||
import ( | ||
settings "github.com/owncloud/ocis/settings/pkg/proto/v0" | ||
"github.com/stretchr/testify/assert" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func cacheRunner(size int, ttl time.Duration) (*cache, func(f func(v string))) { | ||
c := newCache(size, ttl) | ||
run := func(f func(v string)) { | ||
wg := sync.WaitGroup{} | ||
for i := 0; i < size; i++ { | ||
wg.Add(1) | ||
go func(i int) { | ||
f(strconv.Itoa(i)) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
return &c, run | ||
} | ||
|
||
func BenchmarkCache(b *testing.B) { | ||
b.ReportAllocs() | ||
size := 1024 | ||
c, cr := cacheRunner(size, 100 * time.Millisecond) | ||
|
||
cr(func(v string) { c.set(v, &settings.Bundle{})}) | ||
cr(func(v string) { c.get(v)}) | ||
} | ||
|
||
func TestCache(t *testing.T) { | ||
size := 1024 | ||
ttl := 100 * time.Millisecond | ||
c, cr := cacheRunner(size, ttl) | ||
|
||
cr(func(v string) { | ||
c.set(v, &settings.Bundle{Id: v}) | ||
}) | ||
|
||
assert.Equal(t, "50", c.get("50").Id, "it returns the right bundle") | ||
assert.Nil(t, c.get("unknown"), "unknown bundle ist nil") | ||
|
||
time.Sleep(ttl + 1) | ||
// roles cache has no access to evict, adding new items triggers a cleanup | ||
c.set("evict", nil) | ||
assert.Nil(t, c.get("50"), "old bundles get removed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation/formatting is broken here 😅