diff options
Diffstat (limited to 'tpl/partials')
-rw-r--r-- | tpl/partials/partials.go | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/tpl/partials/partials.go b/tpl/partials/partials.go index d3a75edad..d999041be 100644 --- a/tpl/partials/partials.go +++ b/tpl/partials/partials.go @@ -110,27 +110,30 @@ func (ns *Namespace) IncludeCached(name string, context interface{}, variant ... return ns.getOrCreate(key, name, context) } -func (ns *Namespace) getOrCreate(key, name string, context interface{}) (p interface{}, err error) { - var ok bool +func (ns *Namespace) getOrCreate(key, name string, context interface{}) (interface{}, error) { ns.cachedPartials.RLock() - p, ok = ns.cachedPartials.p[key] + p, ok := ns.cachedPartials.p[key] ns.cachedPartials.RUnlock() if ok { - return + return p, nil } ns.cachedPartials.Lock() - if p, ok = ns.cachedPartials.p[key]; !ok { - ns.cachedPartials.Unlock() - p, err = ns.Include(name, context) + defer ns.cachedPartials.Unlock() - ns.cachedPartials.Lock() - ns.cachedPartials.p[key] = p + // Double-check. + if p, ok = ns.cachedPartials.p[key]; ok { + return p, nil + } + p, err := ns.Include(name, context) + if err != nil { + return nil, err } - ns.cachedPartials.Unlock() - return + ns.cachedPartials.p[key] = p + + return p, nil } |