feat: cache bypass to get single entry or asset

This commit is contained in:
Cristian Vidmar 2022-03-15 17:30:02 +01:00
parent a2b22f5d94
commit c5a2757184
3 changed files with 10 additions and 8 deletions

View File

@ -326,9 +326,10 @@ Retrieves all Person entries from the client and returnes a map where the key is
Retrieves Person entries matching the specified query. Retrieves Person entries matching the specified query.
>(cc *ContentfulClient) **GetPersonByID**(id string) (vo *CfPerson, err error) >(cc *ContentfulClient) **GetPersonByID**(id string, forceNoCache ...bool) (vo *CfPerson, err error)
Retrieves the Person entry with the specified ID. Retrieves the Person entry with the specified ID. The optional _forceNoCache_ parameter, if true,
makes the function bypass the existing cache and load a fresh copy of the entry from Contentful.
--- ---
@ -425,9 +426,10 @@ Deletes an asset from the client's cache
Retrieve all assets from a space Retrieve all assets from a space
>(cc *ContentfulClient) **GetAssetByID**(id string) (*contentful.Asset, error) >(cc *ContentfulClient) **GetAssetByID**(id string, forceNoCache ...bool) (*contentful.Asset, error)
Retrieve an asset from a space by its ID Retrieve an asset from a space by its ID. The optional _forceNoCache_ parameter, if true,
makes the function bypass the existing cache and load a fresh copy of the asset from Contentful.
>**NewAssetFromURL**(id string, uploadUrl string, imageFileType string, title string, locale ...string) *contentful.Asset >**NewAssetFromURL**(id string, uploadUrl string, imageFileType string, title string, locale ...string) *contentful.Asset

View File

@ -230,11 +230,11 @@ func (cc *ContentfulClient) GetAllAssets() (map[string]*contentful.Asset, error)
return cc.getAllAssets(true) 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 { if cc == nil || cc.Client == nil {
return nil, errors.New("GetAssetByID: No client available") 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() cc.Cache.assetsGcLock.RLock()
asset, okAsset := cc.Cache.assets[id] asset, okAsset := cc.Cache.assets[id]
cc.Cache.assetsGcLock.RUnlock() cc.Cache.assetsGcLock.RUnlock()

View File

@ -62,11 +62,11 @@ func (cc *ContentfulClient) GetFiltered{{ firstCap $contentType.Sys.ID }}(query
return {{ $contentType.Sys.ID }}Map, nil return {{ $contentType.Sys.ID }}Map, nil
} }
func (cc *ContentfulClient) Get{{ firstCap $contentType.Sys.ID }}ByID(id string) (vo *Cf{{ firstCap $contentType.Sys.ID }}, err error) { func (cc *ContentfulClient) Get{{ firstCap $contentType.Sys.ID }}ByID(id string, forceNoCache ...bool) (vo *Cf{{ firstCap $contentType.Sys.ID }}, err error) {
if cc == nil || cc.Client == nil { if cc == nil || cc.Client == nil {
return nil, errors.New("Get{{ firstCap $contentType.Sys.ID }}ByID: No client available") return nil, errors.New("Get{{ firstCap $contentType.Sys.ID }}ByID: No client available")
} }
if cc.Cache != nil { if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
cc.Cache.entryMaps.{{ $contentType.Sys.ID }}GcLock.RLock() cc.Cache.entryMaps.{{ $contentType.Sys.ID }}GcLock.RLock()
vo, ok := cc.Cache.entryMaps.{{ $contentType.Sys.ID }}[id] vo, ok := cc.Cache.entryMaps.{{ $contentType.Sys.ID }}[id]
cc.Cache.entryMaps.{{ $contentType.Sys.ID }}GcLock.RUnlock() cc.Cache.entryMaps.{{ $contentType.Sys.ID }}GcLock.RUnlock()