-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate an ID for each memoiser so they can share cache
Makes cache clearing easier, and then we don't need to add `ResetCache()` to the Renderer interface.
- Loading branch information
1 parent
a0a60ca
commit 0379fc4
Showing
7 changed files
with
22 additions
and
26 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
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 |
---|---|---|
@@ -1,33 +1,43 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
"github.com/bluele/gcache" | ||
|
||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
var renderCache = gcache.New(100).LRU().Build() | ||
|
||
type memoise struct { | ||
Renderer | ||
cache gcache.Cache | ||
id string | ||
} | ||
|
||
// Memoise wraps the renderer in a loving embrace of caching | ||
func Memoise(r Renderer) Renderer { return &memoise{r, gcache.New(10).LRU().Build()} } | ||
func Memoise(r Renderer) Renderer { | ||
return &memoise{ | ||
Renderer: r, | ||
id: fmt.Sprintf("%x", rand.Int63()), | ||
} | ||
} | ||
|
||
// Render produces a set of RenderableNodes given a Report. | ||
// Ideally, it just retrieves it from the cache, otherwise it calls through to | ||
// `r` and stores the result. | ||
func (m *memoise) Render(rpt report.Report) RenderableNodes { | ||
if result, err := m.cache.Get(rpt.ID); err == nil { | ||
key := fmt.Sprintf("%s-%s", rpt.ID, m.id) | ||
if result, err := renderCache.Get(key); err == nil { | ||
return result.(RenderableNodes) | ||
} | ||
output := m.Renderer.Render(rpt) | ||
m.cache.Set(rpt.ID, output) | ||
renderCache.Set(key, output) | ||
return output | ||
} | ||
|
||
// ResetCache blows away the rendered node cache. | ||
func (m *memoise) ResetCache() { | ||
m.cache.Purge() | ||
m.Renderer.ResetCache() | ||
func ResetCache() { | ||
renderCache.Purge() | ||
} |
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