-
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.
Remove pointer math (comparison) from render caching, as it is unreli…
…able
- Loading branch information
1 parent
a2710ec
commit a0a60ca
Showing
7 changed files
with
42 additions
and
28 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,44 +1,33 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/bluele/gcache" | ||
|
||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
var renderCache = gcache.New(100).LRU().Build() | ||
|
||
type memoise struct { | ||
Renderer | ||
cache gcache.Cache | ||
} | ||
|
||
// Memoise wraps the renderer in a loving embrace of caching | ||
func Memoise(r Renderer) Renderer { return &memoise{r} } | ||
func Memoise(r Renderer) Renderer { return &memoise{r, gcache.New(10).LRU().Build()} } | ||
|
||
// 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 { | ||
key := "" | ||
v := reflect.ValueOf(m.Renderer) | ||
switch v.Kind() { | ||
case reflect.Ptr, reflect.Func: | ||
key = fmt.Sprintf("%s-%x", rpt.ID, v.Pointer()) | ||
default: | ||
return m.Renderer.Render(rpt) | ||
} | ||
if result, err := renderCache.Get(key); err == nil { | ||
if result, err := m.cache.Get(rpt.ID); err == nil { | ||
return result.(RenderableNodes) | ||
} | ||
output := m.Renderer.Render(rpt) | ||
renderCache.Set(key, output) | ||
m.cache.Set(rpt.ID, output) | ||
return output | ||
} | ||
|
||
// ResetCache blows away the rendered node cache. | ||
func ResetCache() { | ||
renderCache.Purge() | ||
func (m *memoise) ResetCache() { | ||
m.cache.Purge() | ||
m.Renderer.ResetCache() | ||
} |
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