diff --git a/pkg/auth/mocks/service.go b/pkg/auth/mocks/service.go index 53ecf5b2..261075f1 100644 --- a/pkg/auth/mocks/service.go +++ b/pkg/auth/mocks/service.go @@ -5,6 +5,7 @@ // // mockgen -destination=mocks/service.go -package=mocks github.com/go-sigma/sigma/pkg/auth Service // + // Package mocks is a generated GoMock package. package mocks diff --git a/pkg/auth/mocks/service_factory.go b/pkg/auth/mocks/service_factory.go index 49534b24..141495ec 100644 --- a/pkg/auth/mocks/service_factory.go +++ b/pkg/auth/mocks/service_factory.go @@ -5,6 +5,7 @@ // // mockgen -destination=mocks/service_factory.go -package=mocks github.com/go-sigma/sigma/pkg/auth ServiceFactory // + // Package mocks is a generated GoMock package. package mocks diff --git a/pkg/configs/configuration.go b/pkg/configs/configuration.go index e1f69903..8ca86c1a 100644 --- a/pkg/configs/configuration.go +++ b/pkg/configs/configuration.go @@ -114,7 +114,7 @@ type ConfigurationCacheRedis struct { // ConfigurationCacheInmemory ... type ConfigurationCacheInmemory struct { - Size int64 `yaml:"size"` + Size int `yaml:"size"` } // ConfigurationCacheDatabase ... diff --git a/pkg/configs/default.go b/pkg/configs/default.go index b584d424..6965be30 100644 --- a/pkg/configs/default.go +++ b/pkg/configs/default.go @@ -53,4 +53,10 @@ func defaultSettings() { if configuration.Daemon.Gc.Retention == 0 { configuration.Daemon.Gc.Retention = 72 * time.Hour } + if configuration.Cache.Inmemory.Size == 0 { + configuration.Cache.Inmemory.Size = 10240 + } + if configuration.Cache.Ttl == 0 { + configuration.Cache.Ttl = time.Second * 30 + } } diff --git a/pkg/modules/cacher/inmemory/inmemory.go b/pkg/modules/cacher/inmemory/inmemory.go index ec7e2c14..fe481b8b 100644 --- a/pkg/modules/cacher/inmemory/inmemory.go +++ b/pkg/modules/cacher/inmemory/inmemory.go @@ -41,7 +41,7 @@ type cacher[T any] struct { // New returns a new Cacher. func New[T any](config configs.Configuration, prefix string, fetcher definition.Fetcher[T]) (definition.Cacher[T], error) { - cache, err := lru.New2Q[string, ValueWithTtl[T]](10240) + cache, err := lru.New2Q[string, ValueWithTtl[T]](config.Cache.Inmemory.Size) if err != nil { return nil, err } @@ -86,14 +86,17 @@ func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) { } return result, nil } - if result.Ttl != nil && result.Ttl.After(time.Now()) { + if result.Ttl == nil { + return result.Value, nil + } + if result.Ttl.After(time.Now()) { return result.Value, nil } err := c.Del(ctx, key) if err != nil { return result.Value, err } - return result.Value, nil + return c.Get(ctx, key) } // Del deletes the value corresponding to the given key from the cache. diff --git a/pkg/modules/cacher/inmemory/inmemory_test.go b/pkg/modules/cacher/inmemory/inmemory_test.go new file mode 100644 index 00000000..33c0829b --- /dev/null +++ b/pkg/modules/cacher/inmemory/inmemory_test.go @@ -0,0 +1,80 @@ +// Copyright 2023 sigma +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package inmemory + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/go-sigma/sigma/pkg/configs" +) + +func fetcher1(key string) (string, error) { + return "new-val", nil +} + +func TestNew(t *testing.T) { + config := configs.Configuration{ + Cache: configs.ConfigurationCache{ + Ttl: time.Second * 3, + Inmemory: configs.ConfigurationCacheInmemory{ + Size: 1000, + }, + }, + } + cacher, err := New(config, "test", fetcher1) + assert.NoError(t, err) + assert.NotNil(t, cacher) + + ctx := context.Background() + err = cacher.Set(ctx, "test", "test") + assert.NoError(t, err) + + res, err := cacher.Get(ctx, "test") + assert.NoError(t, err) + assert.Equal(t, "test", res) + + err = cacher.Del(ctx, "test") + assert.NoError(t, err) + + res, err = cacher.Get(ctx, "test") + assert.NoError(t, err) + assert.Equal(t, "new-val", res) + + err = cacher.Set(ctx, "m-test", "m-val") + assert.NoError(t, err) + + for i := 0; i < 1024; i++ { + err = cacher.Set(ctx, fmt.Sprintf("key-%d", i), "val") + assert.NoError(t, err) + } + + res, err = cacher.Get(ctx, "m-test") + assert.NoError(t, err) + assert.Equal(t, "new-val", res) + + err = cacher.Set(ctx, "expire-key", "expire-val", time.Second) + assert.NoError(t, err) + + time.Sleep(time.Second * 3) + + res, err = cacher.Get(ctx, "expire-key") + assert.NoError(t, err) + assert.Equal(t, "new-val", res) +}