-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/Optional Database Caching #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Valentin-Vi
wants to merge
20
commits into
develop
Choose a base branch
from
feature/cache-abstraction-layer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a37196f
Implemented optional thread-safe caching to database
Valentin-Vi 3dc3601
Implemented optional thread-safe caching for database
Valentin-Vi 5749d44
Formatted
Valentin-Vi 0b552c6
Wrote more tests to satisfy -gtl 90 percent test coverage
Valentin-Vi 2fcf809
Checks in TestGoCache_Get_UnmarshalError test were flawd. It intended…
Valentin-Vi 46bc3ff
Ran and [0;34mFormatting Go code...[0m
Valentin-Vi 8a43f6b
Removed default config from default.yaml and defined it as constant v…
Valentin-Vi 4f4406d
Removed unnecessary in-memory cache implementation
Valentin-Vi ce9db6f
Removed unnecessary tests for removed in-memory cache feature
Valentin-Vi ffbcdc7
Added integration tests
Valentin-Vi af918ed
Minor changes ;)
Valentin-Vi e6de5d6
Minor changes ;)
Valentin-Vi 6640ae9
Streamlined cached_repository.go
Valentin-Vi 2b1f675
The cached repository now uses dependency injection for its base repo…
Valentin-Vi 9066ae8
Removed comments from tests
Valentin-Vi cf9df25
Added Where method in internal/core/database/cache/cached_repository.…
Valentin-Vi 9fe2e14
Took test coverage of internal/core/database/cache/cached_repository.…
Valentin-Vi 4217642
Removed Watcher logging at internal/core/database/cache/cached_reposi…
Valentin-Vi a898f2e
Merge branch 'develop' into feature/cache-abstraction-layer
Valentin-Vi bf248c0
Eliminated use of reflection, replacing it with a param which asks …
Valentin-Vi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,49 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/rabbytesoftware/quiver/internal/core/database/cache" | ||
| interfaces "github.com/rabbytesoftware/quiver/internal/core/database/interface" | ||
| "github.com/rabbytesoftware/quiver/internal/core/database/repository" | ||
|
|
||
| dberr "github.com/rabbytesoftware/quiver/internal/core/database/error" | ||
| ) | ||
|
|
||
| type DatabaseBuilder[T any] struct { | ||
| name string | ||
| cacheConfig *cache.CacheConfig | ||
| extractKey func(entity *T) (string, error) | ||
| } | ||
|
|
||
| func NewDatabaseBuilder[T any]( | ||
| ctx context.Context, | ||
| name string, | ||
| ) *DatabaseBuilder[T] { | ||
| return &DatabaseBuilder[T]{ | ||
| name: name, | ||
| } | ||
| } | ||
|
|
||
| func (b *DatabaseBuilder[T]) WithCache(config cache.CacheConfig, extractKey func(entity *T) (string, error)) *DatabaseBuilder[T] { | ||
| b.cacheConfig = &config | ||
| b.extractKey = extractKey | ||
| return b | ||
| } | ||
|
|
||
| func (b *DatabaseBuilder[T]) Build() (interfaces.RepositoryInterface[T], error) { | ||
| if b.name == "" { | ||
| return nil, dberr.ErrNameRequired | ||
| } | ||
|
|
||
| repo, err := repository.NewRepository[T](b.name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if b.cacheConfig != nil && b.cacheConfig.IsValid() { | ||
| return cache.NewCachedRepository[T](repo, *b.cacheConfig, b.extractKey) | ||
| } | ||
|
|
||
| return repo, nil | ||
| } |
This file contains hidden or 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,202 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/rabbytesoftware/quiver/internal/core/database/cache" | ||
| interfaces "github.com/rabbytesoftware/quiver/internal/core/database/interface" | ||
| ) | ||
|
|
||
| type BuilderTestEntity struct { | ||
| ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id"` | ||
| Name string `gorm:"not null" json:"name"` | ||
| } | ||
|
|
||
| func (BuilderTestEntity) TableName() string { | ||
| return "builder_test_entities" | ||
| } | ||
|
|
||
| // builderTestEntityExtractKey extracts the ID as a string for cache keys | ||
| func builderTestEntityExtractKey(entity *BuilderTestEntity) (string, error) { | ||
| if entity == nil { | ||
| return "", nil | ||
| } | ||
| return entity.ID.String(), nil | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Build_WithoutCache(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| db, err := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_no_cache"). | ||
| Build() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Build_WithCache(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| cacheConfig := cache.CacheConfig{ | ||
| DefaultTTL: 5 * time.Minute, | ||
| CleanupInterval: 1 * time.Minute, | ||
| } | ||
|
|
||
| db, err := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_with_cache"). | ||
| WithCache(cacheConfig, builderTestEntityExtractKey). | ||
| Build() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Build_WithDefaultCacheConfig(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| cacheConfig := cache.DefaultCacheConfig() | ||
|
|
||
| db, err := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_default_cache"). | ||
| WithCache(cacheConfig, builderTestEntityExtractKey). | ||
| Build() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| entity := &BuilderTestEntity{ID: uuid.New(), Name: "Test"} | ||
| created, err := db.Create(ctx, entity) | ||
| require.NoError(t, err) | ||
|
|
||
| retrieved, err := db.GetByID(ctx, created.ID) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, created.Name, retrieved.Name) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Build_InvalidCacheConfig_FallsBackToNonCached(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| cacheConfig := cache.CacheConfig{ | ||
| DefaultTTL: 0, | ||
| CleanupInterval: time.Minute, | ||
| } | ||
|
|
||
| db, err := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_invalid_cache"). | ||
| WithCache(cacheConfig, builderTestEntityExtractKey). | ||
| Build() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| entity := &BuilderTestEntity{ID: uuid.New(), Name: "Test"} | ||
| created, err := db.Create(ctx, entity) | ||
| require.NoError(t, err) | ||
|
|
||
| retrieved, err := db.GetByID(ctx, created.ID) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, created.Name, retrieved.Name) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Chaining(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| cacheConfig := cache.CacheConfig{ | ||
| DefaultTTL: 5 * time.Minute, | ||
| CleanupInterval: 1 * time.Minute, | ||
| } | ||
|
|
||
| builder := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_chaining") | ||
| builder = builder.WithCache(cacheConfig, builderTestEntityExtractKey) | ||
| db, err := builder.Build() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_Build_ReturnsRepositoryInterface(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| db, err := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_interface").Build() | ||
|
|
||
| require.NoError(t, err) | ||
|
|
||
| var _ interfaces.RepositoryInterface[BuilderTestEntity] = db | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestNewDatabase_StillWorks(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| db, err := NewDatabase[BuilderTestEntity](ctx, "test_backwards_compat") | ||
|
|
||
| require.NoError(t, err) | ||
| assert.NotNil(t, db) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db.Close() | ||
| }) | ||
| } | ||
|
|
||
| func TestDatabaseBuilder_MultipleBuilds(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| t.Setenv("QUIVER_DATABASE_PATH", tempDir) | ||
|
|
||
| builder := NewDatabaseBuilder[BuilderTestEntity](ctx, "test_multiple") | ||
|
|
||
| db1, err1 := builder.Build() | ||
| db2, err2 := builder.Build() | ||
|
|
||
| require.NoError(t, err1) | ||
| require.NoError(t, err2) | ||
| assert.NotNil(t, db1) | ||
| assert.NotNil(t, db2) | ||
|
|
||
| assert.NotEqual(t, db1, db2) | ||
|
|
||
| t.Cleanup(func() { | ||
| _ = db1.Close() | ||
| _ = db2.Close() | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.