-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor cache and disable go-chi cache #30417
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,120 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package cache | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
chi_cache "gitea.com/go-chi/cache" //nolint:depguard | ||
) | ||
|
||
type GetJSONError struct { | ||
err error | ||
cachedError string // Golang error can't be stored in cache, only the string message could be stored | ||
} | ||
|
||
func (e *GetJSONError) ToError() error { | ||
if e.err != nil { | ||
return e.err | ||
} | ||
return errors.New("cached error: " + e.cachedError) | ||
} | ||
|
||
type StringCache interface { | ||
Ping() error | ||
|
||
Get(key string) (string, bool) | ||
Put(key, value string, ttl int64) error | ||
Delete(key string) error | ||
IsExist(key string) bool | ||
|
||
PutJSON(key string, v any, ttl int64) error | ||
GetJSON(key string, ptr any) (exist bool, err *GetJSONError) | ||
|
||
ChiCache() chi_cache.Cache | ||
} | ||
|
||
type stringCache struct { | ||
chiCache chi_cache.Cache | ||
} | ||
|
||
func NewStringCache(cacheConfig setting.Cache) (StringCache, error) { | ||
adapter := util.IfZero(cacheConfig.Adapter, "memory") | ||
interval := util.IfZero(cacheConfig.Interval, 60) | ||
cc, err := chi_cache.NewCacher(chi_cache.Options{ | ||
Adapter: adapter, | ||
AdapterConfig: cacheConfig.Conn, | ||
Interval: interval, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &stringCache{chiCache: cc}, nil | ||
} | ||
|
||
func (sc *stringCache) Ping() error { | ||
return sc.chiCache.Ping() | ||
} | ||
|
||
func (sc *stringCache) Get(key string) (string, bool) { | ||
v := sc.chiCache.Get(key) | ||
if v == nil { | ||
return "", false | ||
} | ||
s, ok := v.(string) | ||
return s, ok | ||
} | ||
|
||
func (sc *stringCache) Put(key, value string, ttl int64) error { | ||
return sc.chiCache.Put(key, value, ttl) | ||
} | ||
|
||
func (sc *stringCache) Delete(key string) error { | ||
return sc.chiCache.Delete(key) | ||
} | ||
|
||
func (sc *stringCache) IsExist(key string) bool { | ||
return sc.chiCache.IsExist(key) | ||
} | ||
|
||
const cachedErrorPrefix = "<CACHED-ERROR>:" | ||
|
||
func (sc *stringCache) PutJSON(key string, v any, ttl int64) error { | ||
var s string | ||
switch v := v.(type) { | ||
case error: | ||
s = cachedErrorPrefix + v.Error() | ||
default: | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
return err | ||
} | ||
s = util.UnsafeBytesToString(b) | ||
} | ||
return sc.chiCache.Put(key, s, ttl) | ||
} | ||
|
||
func (sc *stringCache) GetJSON(key string, ptr any) (exist bool, getErr *GetJSONError) { | ||
s, ok := sc.Get(key) | ||
if !ok || s == "" { | ||
return false, nil | ||
} | ||
s, isCachedError := strings.CutPrefix(s, cachedErrorPrefix) | ||
if isCachedError { | ||
return true, &GetJSONError{cachedError: s} | ||
} | ||
if err := json.Unmarshal(util.UnsafeStringToBytes(s), ptr); err != nil { | ||
return false, &GetJSONError{err: err} | ||
} | ||
return true, nil | ||
} | ||
|
||
func (sc *stringCache) ChiCache() chi_cache.Cache { | ||
return sc.chiCache | ||
} |
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.