-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This may speed up queries on slower systems, or systems with bad IO. Ref: #392 Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
- Loading branch information
1 parent
88a0c59
commit 06bb971
Showing
16 changed files
with
904 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,69 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/go-gorm/caches/v4" | ||
) | ||
|
||
var ErrInvalidDataInCache = errors.New("invalid cache data") | ||
|
||
type memoryCacher struct { | ||
store *sync.Map | ||
} | ||
|
||
func (c *memoryCacher) init() { | ||
if c.store == nil { | ||
c.store = &sync.Map{} | ||
} | ||
} | ||
|
||
func (c *memoryCacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) { | ||
val, ok := c.store.Load(key) | ||
if !ok { | ||
// Not stored in cache | ||
return nil, nil //nolint:nilnil | ||
} | ||
|
||
b, ok := val.([]byte) | ||
if !ok { | ||
return nil, ErrInvalidDataInCache // invalid data stored in cache | ||
} | ||
|
||
if err := q.Unmarshal(b); err != nil { | ||
return nil, fmt.Errorf("%w: %s", ErrInvalidDataInCache, err) // invalid data stored in cache | ||
} | ||
|
||
return q, nil | ||
} | ||
|
||
func (c *memoryCacher) Store(ctx context.Context, key string, val *caches.Query[any]) error { | ||
res, err := val.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.store.Store(key, res) | ||
|
||
return nil | ||
} | ||
|
||
func (c *memoryCacher) Invalidate(ctx context.Context) error { | ||
c.store = &sync.Map{} | ||
return nil | ||
} | ||
|
||
func NewMemoryCache() *caches.Caches { | ||
c := &memoryCacher{} | ||
c.init() | ||
|
||
cachesPlugin := &caches.Caches{Conf: &caches.Config{ | ||
Cacher: c, | ||
Easer: true, | ||
}} | ||
|
||
return cachesPlugin | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.