mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
cache: adding factory
This commit is contained in:
parent
ec40e954f2
commit
2f31eb47d4
4 changed files with 49 additions and 0 deletions
1
go.mod
1
go.mod
|
|
@ -10,6 +10,7 @@ require (
|
|||
github.com/eosswedenorg/thalos/api v1.0.0
|
||||
github.com/go-redis/cache/v9 v9.0.0
|
||||
github.com/go-redis/redismock/v9 v9.2.0
|
||||
github.com/karlseguin/typed v1.1.8
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/nikoksr/notify v0.41.0
|
||||
github.com/redis/go-redis/v9 v9.5.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -76,6 +76,8 @@ github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
|||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/karlseguin/typed v1.1.8 h1:ND0eDpwiUFIrm/n1ehxUyh/XNGs9zkYrLxtGqENSalY=
|
||||
github.com/karlseguin/typed v1.1.8/go.mod h1:pZlmYaWQ7MVpwfIOP88fASh3LopVxKeE+uNXW3hQ2D8=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
|
||||
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
|
|
|
|||
26
internal/cache/factory.go
vendored
Normal file
26
internal/cache/factory.go
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/karlseguin/typed"
|
||||
)
|
||||
|
||||
type Factory func(opts typed.Typed) (Store, error)
|
||||
|
||||
var factories = map[string]Factory{
|
||||
"memory": func(opts typed.Typed) (Store, error) {
|
||||
return NewMemoryStore(), nil
|
||||
},
|
||||
}
|
||||
|
||||
func RegisterFactory(driver string, factory Factory) {
|
||||
factories[driver] = factory
|
||||
}
|
||||
|
||||
func Make(driver string, opts typed.Typed) (Store, error) {
|
||||
if factory, ok := factories[driver]; ok {
|
||||
return factory(opts)
|
||||
}
|
||||
return nil, fmt.Errorf("Invalid cache storage: %s", driver)
|
||||
}
|
||||
20
internal/cache/factory_test.go
vendored
Normal file
20
internal/cache/factory_test.go
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package cache_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/eosswedenorg/thalos/internal/cache"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFactory_Make(t *testing.T) {
|
||||
store, err := cache.Make("memory", map[string]any{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, cache.NewMemoryStore(), store)
|
||||
}
|
||||
|
||||
func TestFactory_MakeInvalidDriver(t *testing.T) {
|
||||
store, err := cache.Make("87923yus", map[string]any{})
|
||||
require.Error(t, err)
|
||||
require.Nil(t, store)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue