mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-14 22:17:37 +00:00
feat: project cache (#48)
* feat: eventstore repository * fix: remove gorm * version * feat: pkg * feat: add some files for project * feat: eventstore without eventstore-lib * rename files * gnueg * fix: key json * fix: add object * fix: change imports * fix: internal models * fix: some imports * fix: global model * fix: add some functions on repo * feat(eventstore): sdk * fix(eventstore): search query * fix(eventstore): rename app to eventstore * delete empty test * remove unused func * merge master * fix(eventstore): tests * fix(models): delete unused struct * fix: some funcitons * feat(eventstore): implemented push events * fix: move project eventstore to project package * fix: change project eventstore funcs * feat(eventstore): overwrite context data * fix: change project eventstore * fix: add project repo to mgmt server * feat(types): SQL-config * fix: commented code * feat(eventstore): options to overwrite editor * feat: auth interceptor and cockroach migrations * fix: migrations * fix: fix filter * fix: not found on getbyid * fix: add sequence * fix: add some tests * fix(eventstore): nullable sequence * fix: add some tests * merge * fix: add some tests * fix(migrations): correct statements for sequence * fix: add some tests * fix: add some tests * fix: changes from mr * Update internal/eventstore/models/field.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix(eventstore): code quality * fix: add types to aggregate/Event-types * fix(eventstore): rename modifier* to editor* * fix(eventstore): delete editor_org * fix(migrations): remove editor_org field, rename modifier_* to editor_* * fix: generate files * fix(eventstore): tests * fix(eventstore): rename modifier to editor * fix(migrations): add cluster migration, fix(migrations): fix typo of host in clean clsuter * fix(eventstore): move health * fix(eventstore): AggregateTypeFilter aggregateType as param * code quality * feat: add member funcs * feat: add member model * feat: add member events * feat: add member repo model * fix: project member funcs * fix: add tests * fix: add tests * feat: implement member requests * fix: merge master * fix: read existing in project repo * fix: fix tests * feat: add internal cache * feat: add cache mock * fix: return values of cache mock * fix: add cache config * fix: use eventstore sdk * Update internal/project/model/project_member.go Co-Authored-By: Silvan <silvan.reusser@gmail.com> * fix: use get project func * fix: return err not nil * fix: change err types * Update internal/cache/bigcache/cache.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix: config * fix: config * resolve conversations * fix: mr changes * fix: fix decode of bigcache * feat: test caches * fix: remove unnecessary code Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
This commit is contained in:
11
internal/cache/fastcache/config.go
vendored
Normal file
11
internal/cache/fastcache/config.go
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
package fastcache
|
||||
|
||||
import "github.com/caos/zitadel/internal/cache"
|
||||
|
||||
type Config struct {
|
||||
MaxCacheSizeInByte int
|
||||
}
|
||||
|
||||
func (c *Config) NewCache() (cache.Cache, error) {
|
||||
return NewFastcache(c)
|
||||
}
|
56
internal/cache/fastcache/fastcache.go
vendored
Normal file
56
internal/cache/fastcache/fastcache.go
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
package fastcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"reflect"
|
||||
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
)
|
||||
|
||||
type Fastcache struct {
|
||||
cache *fastcache.Cache
|
||||
}
|
||||
|
||||
func NewFastcache(config *Config) (*Fastcache, error) {
|
||||
return &Fastcache{
|
||||
cache: fastcache.New(config.MaxCacheSizeInByte),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Set(key string, object interface{}) error {
|
||||
if key == "" || reflect.ValueOf(object).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-87dj3", "key or value should not be empty")
|
||||
}
|
||||
var b bytes.Buffer
|
||||
enc := gob.NewEncoder(&b)
|
||||
if err := enc.Encode(object); err != nil {
|
||||
return errors.ThrowInvalidArgument(err, "FASTC-RUyxI", "unable to encode object")
|
||||
}
|
||||
fc.cache.Set([]byte(key), b.Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Get(key string, ptrToObject interface{}) error {
|
||||
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-di8es", "key or value should not be empty")
|
||||
}
|
||||
data := fc.cache.Get(nil, []byte(key))
|
||||
if len(data) == 0 {
|
||||
return errors.ThrowNotFound(nil, "FASTC-xYzSm", "key not found")
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(b)
|
||||
|
||||
return dec.Decode(ptrToObject)
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Delete(key string) error {
|
||||
if key == "" {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-lod92", "key should not be empty")
|
||||
}
|
||||
fc.cache.Del([]byte(key))
|
||||
return nil
|
||||
}
|
216
internal/cache/fastcache/fastcache_test.go
vendored
Normal file
216
internal/cache/fastcache/fastcache_test.go
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
package fastcache
|
||||
|
||||
import (
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
Test string
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
type args struct {
|
||||
cache *Fastcache
|
||||
key string
|
||||
value *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "set cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "key empty",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "set cache nil value",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set(tt.args.key, tt.args.value)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil {
|
||||
tt.args.cache.Get(tt.args.key, tt.res.result)
|
||||
if tt.res.result == nil {
|
||||
t.Errorf("got wrong result should get result: %v ", err)
|
||||
}
|
||||
}
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Fastcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "get cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{Test: "Test"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no value",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Get(tt.args.key, tt.args.getValue)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil && !reflect.DeepEqual(tt.args.getValue, tt.res.result) {
|
||||
t.Errorf("got wrong result expected: %v actual: %v", tt.res.result, tt.args.getValue)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Fastcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "delete cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Delete(tt.args.key)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user