Skip to content

Commit

Permalink
enhancement: move mtimesyncedcache into storage cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Jun 19, 2023
1 parent 99d47da commit 540a3a3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mtimesyncedcache
package cache

import "sync"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mtimesyncedcache
package cache

import (
"sync"
"time"
)

type Cache[K comparable, T any] struct {
type MtimeCache[K comparable, T any] struct {
entries Map[K, *entry[T]]
}

Expand All @@ -16,21 +16,21 @@ type entry[T any] struct {
mu sync.Mutex
}

func New[K comparable, T any]() Cache[K, T] {
return Cache[K, T]{
func NewMtimeCache[K comparable, T any]() MtimeCache[K, T] {
return MtimeCache[K, T]{
entries: Map[K, *entry[T]]{},
}
}

func (c *Cache[K, T]) Store(key K, mtime time.Time, value T) error {
func (c *MtimeCache[K, T]) Store(key K, mtime time.Time, value T) error {
c.entries.Store(key, &entry[T]{
mtime: mtime,
value: value,
})
return nil
}

func (c *Cache[K, T]) Load(key K) (T, bool) {
func (c *MtimeCache[K, T]) Load(key K) (T, bool) {
entry, ok := c.entries.Load(key)
if !ok {
var t T
Expand All @@ -39,7 +39,7 @@ func (c *Cache[K, T]) Load(key K) (T, bool) {
return entry.value, true
}

func (c *Cache[K, T]) LoadOrStore(key K, mtime time.Time, f func() (T, error)) (T, error) {
func (c *MtimeCache[K, T]) LoadOrStore(key K, mtime time.Time, f func() (T, error)) (T, error) {
e, _ := c.entries.LoadOrStore(key, &entry[T]{})

e.mu.Lock()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mtimesyncedcache_test
package cache_test

import (
"errors"
Expand All @@ -7,60 +7,60 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/mtimesyncedcache"
"github.com/cs3org/reva/v2/pkg/storage/cache"
)

var _ = Describe("Mtimesyncedcache", func() {
var _ = Describe("cache.MtimeCache", func() {
var (
cache mtimesyncedcache.Cache[string, string]
mtCache cache.MtimeCache[string, string]

key = "key"
value = "value"
)

BeforeEach(func() {
cache = mtimesyncedcache.New[string, string]()
mtCache = cache.NewMtimeCache[string, string]()
})

Describe("Store", func() {
It("stores a value", func() {
time := time.Now()

err := cache.Store(key, time, value)
err := mtCache.Store(key, time, value)
Expect(err).ToNot(HaveOccurred())
})
})

Describe("Load", func() {
It("loads the stored value", func() {
err := cache.Store(key, time.Now(), value)
err := mtCache.Store(key, time.Now(), value)
Expect(err).ToNot(HaveOccurred())

v, ok := cache.Load(key)
v, ok := mtCache.Load(key)
Expect(ok).To(BeTrue())
Expect(v).To(Equal(value))
})

It("reports when the key doesn't exist", func() {
_, ok := cache.Load("doesnotexist")
_, ok := mtCache.Load("doesnotexist")
Expect(ok).To(BeFalse())
})
})

Describe("LoadOrStore", func() {
It("does not update the cache if the cache is up to date", func() {
cachedTime := time.Now().Add(-1 * time.Hour)
err := cache.Store(key, cachedTime, value)
err := mtCache.Store(key, cachedTime, value)
Expect(err).ToNot(HaveOccurred())

newvalue := "yaaay"
v, err := cache.LoadOrStore(key, cachedTime, func() (string, error) {
v, err := mtCache.LoadOrStore(key, cachedTime, func() (string, error) {
return newvalue, nil
})
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal(value))

v, err = cache.LoadOrStore(key, time.Now().Add(-2*time.Hour), func() (string, error) {
v, err = mtCache.LoadOrStore(key, time.Now().Add(-2*time.Hour), func() (string, error) {
return newvalue, nil
})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -69,11 +69,11 @@ var _ = Describe("Mtimesyncedcache", func() {

It("updates the cache if the cache is outdated", func() {
outdatedTime := time.Now().Add(-1 * time.Hour)
err := cache.Store(key, outdatedTime, value)
err := mtCache.Store(key, outdatedTime, value)
Expect(err).ToNot(HaveOccurred())

newvalue := "yaaay"
v, err := cache.LoadOrStore(key, time.Now(), func() (string, error) {
v, err := mtCache.LoadOrStore(key, time.Now(), func() (string, error) {
return newvalue, nil
})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -82,7 +82,7 @@ var _ = Describe("Mtimesyncedcache", func() {

It("stores the value if the key doesn't exist yet", func() {
newvalue := "yaaay"
v, err := cache.LoadOrStore(key, time.Now(), func() (string, error) {
v, err := mtCache.LoadOrStore(key, time.Now(), func() (string, error) {
return newvalue, nil
})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -93,22 +93,22 @@ var _ = Describe("Mtimesyncedcache", func() {
newTime := time.Now()

newvalue := "yaaay"
v, err := cache.LoadOrStore(key, newTime, func() (string, error) {
v, err := mtCache.LoadOrStore(key, newTime, func() (string, error) {
return newvalue, nil
})
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal(newvalue))

newvalue2 := "asdfasdf"
v, err = cache.LoadOrStore(key, newTime, func() (string, error) {
v, err = mtCache.LoadOrStore(key, newTime, func() (string, error) {
return newvalue2, nil
})
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal(newvalue))
})

It("passes on error from the store func", func() {
v, err := cache.LoadOrStore(key, time.Now(), func() (string, error) {
v, err := mtCache.LoadOrStore(key, time.Now(), func() (string, error) {
return "", errors.New("baa")
})
Expect(v).To(Equal(""))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mtimesyncedcache_test
package cache_test

import (
"testing"
Expand All @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)

func TestMtimesyncedcache(t *testing.T) {
func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Mtimesyncedcache Suite")
RunSpecs(t, "Cache Suite")
}
12 changes: 6 additions & 6 deletions pkg/storage/utils/decomposedfs/decomposedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import (

rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/jellydator/ttlcache/v2"
"github.com/pkg/errors"
microstore "go-micro.dev/v4/store"
"golang.org/x/sync/errgroup"

"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
Expand All @@ -49,7 +54,6 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/lookup"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/metadata"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/migrator"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/mtimesyncedcache"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/options"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/tree"
Expand All @@ -59,10 +63,6 @@ import (
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/store"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/jellydator/ttlcache/v2"
"github.com/pkg/errors"
microstore "go-micro.dev/v4/store"
"golang.org/x/sync/errgroup"
)

// name is the Tracer name used to identify this instrumentation library.
Expand Down Expand Up @@ -101,7 +101,7 @@ type Decomposedfs struct {
cache cache.StatCache

UserCache *ttlcache.Cache
spaceIDCache mtimesyncedcache.Cache[string, map[string]string]
spaceIDCache cache.MtimeCache[string, map[string]string]
}

// NewDefault returns an instance with default components
Expand Down

0 comments on commit 540a3a3

Please sign in to comment.