Skip to content

Commit

Permalink
fix: cache update fails on new entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Vidmar committed Mar 23, 2022
1 parent 445a1e1 commit 73cf10c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
5 changes: 1 addition & 4 deletions erm/templates/contentful_vo_lib_contenttype.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 27 additions & 2 deletions test/testapi/gocontentfulvolib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 3 additions & 6 deletions test/testapi/gocontentfulvolibbrand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
Expand Down
9 changes: 3 additions & 6 deletions test/testapi/gocontentfulvolibcategory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
Expand Down
9 changes: 3 additions & 6 deletions test/testapi/gocontentfulvolibproduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 73cf10c

Please sign in to comment.