-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cacher for gorm to reduce the load of db
Signed-off-by: Gaius <[email protected]>
- Loading branch information
Showing
5 changed files
with
110 additions
and
6 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,82 @@ | ||
/* | ||
* Copyright 2024 The Dragonfly Authors | ||
* | ||
* 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 database | ||
|
||
import ( | ||
"context" | ||
|
||
caches "github.com/go-gorm/caches/v4" | ||
lru "github.com/hashicorp/golang-lru/v2" | ||
) | ||
|
||
// defaultCacheSize is the default size of the cache. | ||
const defaultCacheSize = 1024 | ||
|
||
// cacher is a cache implementation using LRU for gorm. | ||
type cacher struct { | ||
// store is the LRU cache. | ||
store *lru.Cache[string, any] | ||
} | ||
|
||
// newCacher creates a new cacher. | ||
func newCacher() (caches.Cacher, error) { | ||
store, err := lru.New[string, any](defaultCacheSize) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cacher{store}, nil | ||
} | ||
|
||
// Get impl should check if a specific key exists in the cache and return its value | ||
// look at Query.Marshal. | ||
func (c *cacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) { | ||
val, ok := c.store.Get(key) | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
if err := q.Unmarshal(val.([]byte)); err != nil { | ||
return nil, err | ||
} | ||
|
||
return q, nil | ||
} | ||
|
||
// Store impl should store a cached representation of the val param | ||
// look at Query.Unmarshal. | ||
func (c *cacher) Store(ctx context.Context, key string, val *caches.Query[any]) error { | ||
res, err := val.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.store.Add(key, res) | ||
return nil | ||
} | ||
|
||
// Invalidate impl should invalidate all cached values. It will be called when | ||
// INSERT / UPDATE / DELETE queries are sent to the DB. | ||
func (c *cacher) Invalidate(ctx context.Context) error { | ||
var err error | ||
c.store, err = lru.New[string, any](defaultCacheSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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