-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
647 additions
and
187 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
*.iml | ||
*.iws | ||
|
||
/build | ||
/build | ||
vendor/ |
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
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,6 @@ | ||
package dpcache | ||
|
||
const ( | ||
// HomepageCacheKey is used to cache the rendered homepage | ||
HomepageCacheKey = "homepage-cache" | ||
) |
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,93 @@ | ||
package dpcache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ONSdigital/log.go/log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type DpCacher interface { | ||
Close() | ||
Get(key string) (interface{}, bool) | ||
Set(key string, data interface{}) | ||
AddUpdateFunc(key string, updateFunc func() (string, error)) | ||
StartUpdates(ctx context.Context, channel chan error) | ||
} | ||
|
||
type DpCache struct { | ||
cache sync.Map | ||
updateInterval time.Duration | ||
close chan struct{} | ||
updateFuncs map[string]func() (string, error) | ||
} | ||
|
||
func (dc *DpCache) Get(key string) (interface{}, bool) { | ||
return dc.cache.Load(key) | ||
} | ||
|
||
func (dc *DpCache) Set(key string, data interface{}) { | ||
dc.cache.Store(key, data) | ||
} | ||
|
||
func (dc *DpCache) Close() { | ||
dc.close <- struct{}{} | ||
for key, _ := range dc.updateFuncs { | ||
dc.cache.Store(key, "") | ||
} | ||
dc.updateFuncs = make(map[string]func() (string, error)) | ||
} | ||
|
||
func NewDpCache(updateInterval time.Duration) DpCacher { | ||
return &DpCache{ | ||
cache: sync.Map{}, | ||
updateInterval: updateInterval, | ||
close: make(chan struct{}), | ||
updateFuncs: make(map[string]func() (string, error)), | ||
} | ||
} | ||
|
||
func (dc *DpCache) AddUpdateFunc(key string, updateFunc func() (string, error)) { | ||
dc.updateFuncs[key] = updateFunc | ||
} | ||
|
||
func (dc *DpCache) UpdateContent(ctx context.Context) error { | ||
for key, updateFunc := range dc.updateFuncs { | ||
updatedContent, err := updateFunc() | ||
if err != nil { | ||
return fmt.Errorf("HOMEPAGE_CACHE_UPDATE_FAILED. failed to update homepage cache for %s. error: %v", key, err) | ||
} | ||
dc.Set(key, updatedContent) | ||
} | ||
return nil | ||
} | ||
|
||
func (dc *DpCache) StartUpdates(ctx context.Context, errorChannel chan error) { | ||
ticker := time.NewTicker(dc.updateInterval) | ||
if len(dc.updateFuncs) == 0 { | ||
return | ||
} | ||
|
||
err := dc.UpdateContent(ctx) | ||
if err != nil { | ||
errorChannel <- err | ||
dc.Close() | ||
return | ||
} | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
err := dc.UpdateContent(ctx) | ||
if err != nil { | ||
log.Event(ctx, err.Error(), log.Error(err), log.ERROR) | ||
} | ||
|
||
case <-dc.close: | ||
return | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.