-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from dwango/feature/issue-74
Implement cache
- Loading branch information
Showing
24 changed files
with
1,019 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,6 @@ go.work | |
|
||
# goreleaser | ||
dist/ | ||
|
||
# temporary files | ||
tmp/ |
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
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,60 @@ | ||
/** | ||
* Copyright 2024 DWANGO Co., Ltd. | ||
* | ||
* 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 | ||
* | ||
* https://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 client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dwango/yashiro/internal/client/cache" | ||
"github.com/dwango/yashiro/internal/values" | ||
) | ||
|
||
type clientWithCache struct { | ||
client Client | ||
cache cache.Cache | ||
} | ||
|
||
func newClientWithCache(client Client, cache cache.Cache) Client { | ||
return &clientWithCache{ | ||
client: client, | ||
cache: cache, | ||
} | ||
} | ||
|
||
// GetValues implements Client. | ||
func (c *clientWithCache) GetValues(ctx context.Context, ignoreNotFound bool) (values.Values, error) { | ||
val, expired, err := c.cache.Load(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if cache is empty, get values from external store. | ||
if len(val) == 0 || expired { | ||
val, err = c.client.GetValues(ctx, ignoreNotFound) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// save values to cache | ||
if expired { | ||
if err := c.cache.Save(ctx, val); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return val, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2024 DWANGO Co., Ltd. | ||
* | ||
* 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 | ||
* | ||
* https://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 cache | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/dwango/yashiro/internal/values" | ||
"github.com/dwango/yashiro/pkg/config" | ||
) | ||
|
||
var ( | ||
ErrInvalidCacheType = errors.New("invalid cache type") | ||
) | ||
|
||
type Cache interface { | ||
// Load returns values from cache and whether or not cache is expired. If cache is empty, | ||
// returned values is empty and expired=true. | ||
Load(ctx context.Context) (values.Values, bool, error) | ||
|
||
// Save saves values to cache. | ||
Save(ctx context.Context, val values.Values) error | ||
} | ||
|
||
func New(cfg config.CacheConfig) (Cache, error) { | ||
switch cfg.Type { | ||
case config.CacheTypeUnspecified, config.CacheTypeMemory: | ||
return newMemoryCache() | ||
case config.CacheTypeFile: | ||
return newFileCache(cfg.File) | ||
default: | ||
return nil, fmt.Errorf("%w: %s", ErrInvalidCacheType, cfg.Type) | ||
} | ||
} |
Oops, something went wrong.