fix: cache update fails on new entries

This commit is contained in:
Cristian Vidmar 2022-03-23 09:04:16 +01:00
parent 445a1e16c1
commit 73cf10c5f0
6 changed files with 38 additions and 25 deletions

View File

@ -637,10 +637,7 @@ func (cc *ContentfulClient) cacheAll{{ firstCap $contentType.Sys.ID }}(ctx conte
for _, {{ $contentType.Sys.ID }} := range all{{ firstCap $contentType.Sys.ID }} {
if cc.Cache != nil {
existing{{ firstCap $contentType.Sys.ID }}, err := cc.Get{{ firstCap $contentType.Sys.ID }}ByID({{ $contentType.Sys.ID }}.Sys.ID)
if err != nil {
return nil, err
}
if existing{{ firstCap $contentType.Sys.ID }}.Sys.Version > {{ $contentType.Sys.ID }}.Sys.Version {
if err == nil && existing{{ firstCap $contentType.Sys.ID }} != nil && existing{{ firstCap $contentType.Sys.ID }}.Sys.Version > {{ $contentType.Sys.ID }}.Sys.Version {
return nil, fmt.Errorf("cache update canceled because {{ firstCap $contentType.Sys.ID }} entry %s is newer in cache", {{ $contentType.Sys.ID }}.Sys.ID)
}
}

View File

@ -11,7 +11,7 @@ import (
"github.com/foomo/gocontentful/erm"
)
var VERSION = "v1.0.7"
var VERSION = "v1.0.8"
var Usage = func() {
fmt.Printf("\nSYNOPSIS\n")

View File

@ -71,6 +71,14 @@ type ContentTypeResult struct {
References map[string][]EntryReference
}
type ContentTypeInfo struct {
ContentType string
Title string
Description string
}
type ContentTypeInfoMap map[string]ContentTypeInfo
type EntryReference struct {
ContentType string
ID string
@ -142,6 +150,23 @@ var (
)
var spaceContentTypes = []string{ContentTypeBrand, ContentTypeCategory, ContentTypeProduct}
var SpaceContentTypeInfoMap = ContentTypeInfoMap{
"brand": ContentTypeInfo{
ContentType: "brand",
Title: "Brand",
Description: "",
},
"category": ContentTypeInfo{
ContentType: "category",
Title: "Category",
Description: "",
},
"product": ContentTypeInfo{
ContentType: "product",
Title: "Product",
Description: "",
},
}
func (cc *ContentfulClient) BrokenReferences() (brokenReferences []BrokenReference) {
if cc.Cache == nil {
@ -235,11 +260,11 @@ func (cc *ContentfulClient) GetAllAssets() (map[string]*contentful.Asset, error)
return cc.getAllAssets(true)
}
func (cc *ContentfulClient) GetAssetByID(id string) (*contentful.Asset, error) {
func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*contentful.Asset, error) {
if cc == nil || cc.Client == nil {
return nil, errors.New("GetAssetByID: No client available")
}
if cc.Cache != nil && cc.Cache.assets != nil {
if cc.Cache != nil && cc.Cache.assets != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
cc.Cache.assetsGcLock.RLock()
asset, okAsset := cc.Cache.assets[id]
cc.Cache.assetsGcLock.RUnlock()

View File

@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredBrand(query *contentful.Query) (voMap map
return brandMap, nil
}
func (cc *ContentfulClient) GetBrandByID(id string) (vo *CfBrand, err error) {
func (cc *ContentfulClient) GetBrandByID(id string, forceNoCache ...bool) (vo *CfBrand, err error) {
if cc == nil || cc.Client == nil {
return nil, errors.New("GetBrandByID: No client available")
}
if cc.Cache != nil {
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
cc.Cache.entryMaps.brandGcLock.RLock()
vo, ok := cc.Cache.entryMaps.brand[id]
cc.Cache.entryMaps.brandGcLock.RUnlock()
@ -725,10 +725,7 @@ func (cc *ContentfulClient) cacheAllBrand(ctx context.Context, resultChan chan<-
for _, brand := range allBrand {
if cc.Cache != nil {
existingBrand, err := cc.GetBrandByID(brand.Sys.ID)
if err != nil {
return nil, err
}
if existingBrand.Sys.Version > brand.Sys.Version {
if err == nil && existingBrand != nil && existingBrand.Sys.Version > brand.Sys.Version {
return nil, fmt.Errorf("cache update canceled because Brand entry %s is newer in cache", brand.Sys.ID)
}
}

View File

@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredCategory(query *contentful.Query) (voMap
return categoryMap, nil
}
func (cc *ContentfulClient) GetCategoryByID(id string) (vo *CfCategory, err error) {
func (cc *ContentfulClient) GetCategoryByID(id string, forceNoCache ...bool) (vo *CfCategory, err error) {
if cc == nil || cc.Client == nil {
return nil, errors.New("GetCategoryByID: No client available")
}
if cc.Cache != nil {
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
cc.Cache.entryMaps.categoryGcLock.RLock()
vo, ok := cc.Cache.entryMaps.category[id]
cc.Cache.entryMaps.categoryGcLock.RUnlock()
@ -489,10 +489,7 @@ func (cc *ContentfulClient) cacheAllCategory(ctx context.Context, resultChan cha
for _, category := range allCategory {
if cc.Cache != nil {
existingCategory, err := cc.GetCategoryByID(category.Sys.ID)
if err != nil {
return nil, err
}
if existingCategory.Sys.Version > category.Sys.Version {
if err == nil && existingCategory != nil && existingCategory.Sys.Version > category.Sys.Version {
return nil, fmt.Errorf("cache update canceled because Category entry %s is newer in cache", category.Sys.ID)
}
}

View File

@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredProduct(query *contentful.Query) (voMap m
return productMap, nil
}
func (cc *ContentfulClient) GetProductByID(id string) (vo *CfProduct, err error) {
func (cc *ContentfulClient) GetProductByID(id string, forceNoCache ...bool) (vo *CfProduct, err error) {
if cc == nil || cc.Client == nil {
return nil, errors.New("GetProductByID: No client available")
}
if cc.Cache != nil {
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
cc.Cache.entryMaps.productGcLock.RLock()
vo, ok := cc.Cache.entryMaps.product[id]
cc.Cache.entryMaps.productGcLock.RUnlock()
@ -1107,10 +1107,7 @@ func (cc *ContentfulClient) cacheAllProduct(ctx context.Context, resultChan chan
for _, product := range allProduct {
if cc.Cache != nil {
existingProduct, err := cc.GetProductByID(product.Sys.ID)
if err != nil {
return nil, err
}
if existingProduct.Sys.Version > product.Sys.Version {
if err == nil && existingProduct != nil && existingProduct.Sys.Version > product.Sys.Version {
return nil, fmt.Errorf("cache update canceled because Product entry %s is newer in cache", product.Sys.ID)
}
}