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
[WIP] feat: add cache provider implementation #4169
Draft
milosgajdos
wants to merge
6
commits into
distribution:main
Choose a base branch
from
milosgajdos:add-cache-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
+622
−307
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d38096a
feat: add cache provider implementation
milosgajdos 30f8cf7
Update registry/storage/cache/provider/cacheprovider.go
milosgajdos 90665be
update: make cacheprovider.Register panic on error
milosgajdos 15bdb51
refactor: complete refactoring of cache providers and cache config
milosgajdos 6e079b8
Update cache.go
milosgajdos 7bf9aae
Update redis.go
milosgajdos File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cacheprovider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/distribution/distribution/v3/registry/storage/cache" | ||
) | ||
|
||
// InitFunc is the type of a CacheProvider factory function and is | ||
// used to register the constructor for different CacheProvider backends. | ||
type InitFunc func(ctx context.Context, options map[string]interface{}) (cache.BlobDescriptorCacheProvider, error) | ||
corhere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var cacheProviders map[string]InitFunc | ||
|
||
// Register is used to register an InitFunc for | ||
// a CacheProvider backend with the given name. | ||
// It's meant to be called from init() function | ||
// of the cache provider. | ||
func Register(name string, initFunc InitFunc) { | ||
if cacheProviders == nil { | ||
cacheProviders = make(map[string]InitFunc) | ||
} | ||
if _, exists := cacheProviders[name]; exists { | ||
panic(fmt.Sprintf("name already registered: %s", name)) | ||
} | ||
|
||
cacheProviders[name] = initFunc | ||
} | ||
|
||
// Get constructs a CacheProvider with the given options using the named backend. | ||
func Get(ctx context.Context, name string, options map[string]interface{}) (cache.BlobDescriptorCacheProvider, error) { | ||
if initFunc, exists := cacheProviders[name]; exists { | ||
return initFunc(ctx, options) | ||
} | ||
return nil, fmt.Errorf("no cache Provider registered with name: %s", name) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how many near-duplicates of this pattern are scattered throughout the codebase. I betcha they could all be replaced with instantiations of a single generic implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with generics, this codebase could use proper massaging.