-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update roles cache to use sync.cache
add tests for roles cache add changelog use strings in sync cache tests
- Loading branch information
Showing
4 changed files
with
102 additions
and
77 deletions.
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