Fix image cache eviction for sites with subdir in baseURL

Fixes #5006
This commit is contained in:
Bjørn Erik Pedersen 2018-07-29 14:26:45 +02:00
parent 0cae1cf828
commit 786f72302f
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 13 additions and 2 deletions

View file

@ -322,7 +322,7 @@ func (c *PageCollections) clearResourceCacheForPage(page *Page) {
first := page.Resources[0]
dir := path.Dir(first.RelPermalink())
dir = strings.TrimPrefix(dir, page.LanguagePrefix())
// This is done to keep the memory usage in check when doing live reloads.
dir = strings.TrimPrefix(dir, page.s.BaseURL.Path())
page.s.ResourceSpec.DeleteCacheByPrefix(dir)
}
}

View file

@ -33,7 +33,7 @@ type imageCache struct {
func (c *imageCache) isInCache(key string) bool {
c.mu.RLock()
_, found := c.store[key]
_, found := c.store[c.normalizeKey(key)]
c.mu.RUnlock()
return found
}
@ -41,6 +41,7 @@ func (c *imageCache) isInCache(key string) bool {
func (c *imageCache) deleteByPrefix(prefix string) {
c.mu.Lock()
defer c.mu.Unlock()
prefix = c.normalizeKey(prefix)
for k := range c.store {
if strings.HasPrefix(k, prefix) {
delete(c.store, k)
@ -48,6 +49,16 @@ func (c *imageCache) deleteByPrefix(prefix string) {
}
}
func (c *imageCache) normalizeKey(key string) string {
// It is a path with Unix style slashes and it always starts with a leading slash.
key = filepath.ToSlash(key)
if !strings.HasPrefix(key, "/") {
key = "/" + key
}
return key
}
func (c *imageCache) clear() {
c.mu.Lock()
defer c.mu.Unlock()