forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit also consolidates all (or the most important) memory caches in Hugo. Fixes gohugoio#7425
- Loading branch information
Showing
20 changed files
with
293 additions
and
334 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2020 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package memcache provides the core memory cache used in Hugo. | ||
package memcache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/BurntSushi/locker" | ||
"github.com/karlseguin/ccache" | ||
) | ||
|
||
// Cache configures a cache. | ||
type Cache struct { | ||
cache *ccache.LayeredCache | ||
|
||
ttl time.Duration | ||
nlocker *locker.Locker | ||
} | ||
|
||
type cacheEntry struct { | ||
size int64 | ||
value interface{} | ||
err error | ||
} | ||
|
||
func (c cacheEntry) Size() int64 { | ||
return c.size | ||
} | ||
|
||
// New creates a new cache. | ||
func New() *Cache { | ||
return &Cache{ | ||
// TODO1 | ||
cache: ccache.Layered(ccache.Configure().MaxSize(100).ItemsToPrune(100)), | ||
ttl: time.Second * 5, | ||
nlocker: locker.NewLocker(), | ||
} | ||
} | ||
|
||
// Clear clears the cache state. | ||
// This method is not thread safe. | ||
func (c *Cache) Clear() { | ||
c.nlocker = locker.NewLocker() | ||
c.cache.Clear() | ||
} | ||
|
||
func (c *Cache) Has(primary, secondary string) bool { | ||
return c.cache.Get(primary, secondary) != nil | ||
} | ||
|
||
func (c *Cache) Get(primary, secondary string) (interface{}, bool) { | ||
v := c.cache.Get(primary, secondary) | ||
if v == nil { | ||
return nil, false | ||
} | ||
return v.Value(), true | ||
} | ||
|
||
func (c *Cache) DeleteAll(primary string) bool { | ||
return c.cache.DeleteAll(primary) | ||
} | ||
|
||
func (c *Cache) Stop() { | ||
c.cache.Stop() | ||
} | ||
|
||
// GetOrCreate tries to get the value with the given cache keys, if not found | ||
// create will be called and cached. | ||
// This method is thread safe. | ||
func (c *Cache) GetOrCreate(primary, secondary string, create func() (interface{}, error)) (interface{}, error) { | ||
if v := c.cache.Get(primary, secondary); v != nil { | ||
entry := v.Value().(cacheEntry) | ||
return entry.value, entry.err | ||
} | ||
|
||
// The provided create function may be a relatively time consuming operation, | ||
// and there will in the commmon case be concurrent requests for the same key'd | ||
// resource, so make sure we pause these until the result is ready. | ||
key := primary + secondary | ||
c.nlocker.Lock(key) | ||
defer c.nlocker.Unlock(key) | ||
|
||
// Try again. | ||
if v := c.cache.Get(primary, secondary); v != nil { | ||
entry := v.Value().(cacheEntry) | ||
return entry.value, entry.err | ||
} | ||
|
||
// Create it and store it in cache. | ||
value, err := create() | ||
|
||
// TODO1 size | ||
c.cache.Set(primary, secondary, cacheEntry{value: value, err: err, size: 1}, c.ttl) | ||
|
||
return value, err | ||
} |
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 was deleted.
Oops, something went wrong.
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
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.