Caching: Consolidate resource cache checking and updating in plugin middleware (#67002)
* Update the HandleResourceRequest function to mimic the HandleQueryRequest function * Remove CacheResourceResponse function from interface * revert additional thing I missed
This commit is contained in:
@@ -11,20 +11,16 @@ import (
|
||||
type FakeOSSCachingService struct {
|
||||
calls map[string]int
|
||||
ReturnHit bool
|
||||
ReturnResourceResponse *backend.CallResourceResponse
|
||||
ReturnResourceResponse CachedResourceDataResponse
|
||||
ReturnQueryResponse CachedQueryDataResponse
|
||||
}
|
||||
|
||||
func (f *FakeOSSCachingService) CacheResourceResponse(ctx context.Context, req *backend.CallResourceRequest, resp *backend.CallResourceResponse) {
|
||||
f.calls["CacheResourceResponse"]++
|
||||
}
|
||||
|
||||
func (f *FakeOSSCachingService) HandleQueryRequest(ctx context.Context, req *backend.QueryDataRequest) (bool, CachedQueryDataResponse) {
|
||||
f.calls["HandleQueryRequest"]++
|
||||
return f.ReturnHit, f.ReturnQueryResponse
|
||||
}
|
||||
|
||||
func (f *FakeOSSCachingService) HandleResourceRequest(ctx context.Context, req *backend.CallResourceRequest) (bool, *backend.CallResourceResponse) {
|
||||
func (f *FakeOSSCachingService) HandleResourceRequest(ctx context.Context, req *backend.CallResourceRequest) (bool, CachedResourceDataResponse) {
|
||||
f.calls["HandleResourceRequest"]++
|
||||
return f.ReturnHit, f.ReturnResourceResponse
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ const (
|
||||
)
|
||||
|
||||
type CacheQueryResponseFn func(context.Context, *backend.QueryDataResponse)
|
||||
type CacheResourceResponseFn func(context.Context, *backend.CallResourceResponse)
|
||||
|
||||
type CachedQueryDataResponse struct {
|
||||
// The cached data response associated with a query, or nil if no cached data is found
|
||||
@@ -25,6 +26,15 @@ type CachedQueryDataResponse struct {
|
||||
UpdateCacheFn CacheQueryResponseFn
|
||||
}
|
||||
|
||||
type CachedResourceDataResponse struct {
|
||||
// The cached response associated with a resource request, or nil if no cached data is found
|
||||
Response *backend.CallResourceResponse
|
||||
// A function that should be used to cache a CallResourceResponse for a given resource request.
|
||||
// It can be set to nil by the method implementation (if there is an error, for example), so it should be checked before being called.
|
||||
// Because plugins can send multiple responses asyncronously, the implementation should be able to handle multiple calls to this function for one request.
|
||||
UpdateCacheFn CacheResourceResponseFn
|
||||
}
|
||||
|
||||
func ProvideCachingService() *OSSCachingService {
|
||||
return &OSSCachingService{}
|
||||
}
|
||||
@@ -36,10 +46,7 @@ type CachingService interface {
|
||||
HandleQueryRequest(context.Context, *backend.QueryDataRequest) (bool, CachedQueryDataResponse)
|
||||
// HandleResourceRequest uses a CallResourceRequest to check the cache for any existing results for that request. If none are found, it should return false.
|
||||
// This function may populate any response headers (accessible through the context) with the cache status using the X-Cache header.
|
||||
HandleResourceRequest(context.Context, *backend.CallResourceRequest) (bool, *backend.CallResourceResponse)
|
||||
// CacheResourceResponse is used to cache resource responses for a resource request.
|
||||
// Because plugins can send multiple responses asyncronously, the implementation should be able to handle multiple calls to this function for one request.
|
||||
CacheResourceResponse(context.Context, *backend.CallResourceRequest, *backend.CallResourceResponse)
|
||||
HandleResourceRequest(context.Context, *backend.CallResourceRequest) (bool, CachedResourceDataResponse)
|
||||
}
|
||||
|
||||
// Implementation of interface - does nothing
|
||||
@@ -50,11 +57,8 @@ func (s *OSSCachingService) HandleQueryRequest(ctx context.Context, req *backend
|
||||
return false, CachedQueryDataResponse{}
|
||||
}
|
||||
|
||||
func (s *OSSCachingService) HandleResourceRequest(ctx context.Context, req *backend.CallResourceRequest) (bool, *backend.CallResourceResponse) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s *OSSCachingService) CacheResourceResponse(ctx context.Context, req *backend.CallResourceRequest, resp *backend.CallResourceResponse) {
|
||||
func (s *OSSCachingService) HandleResourceRequest(ctx context.Context, req *backend.CallResourceRequest) (bool, CachedResourceDataResponse) {
|
||||
return false, CachedResourceDataResponse{}
|
||||
}
|
||||
|
||||
var _ CachingService = &OSSCachingService{}
|
||||
|
||||
Reference in New Issue
Block a user