diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-11-08 10:24:13 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-11-13 14:19:42 +0100 |
commit | f7aeaa61291dd75f92901bcbeecc7fce07a28dec (patch) | |
tree | 409c03e259a38fce2beeab46655fd5108c84cd5c /tpl/data/cache.go | |
parent | 7d78a2afd3c4a6c4af77a4ddcbd2a82f15986048 (diff) |
Add a consolidated file cache
This commits reworks how file caching is performed in Hugo. Now there is only one way, and it can be configured.
This is the default configuration:
```toml
[caches]
[caches.getjson]
dir = ":cacheDir"
maxAge = -1
[caches.getcsv]
dir = ":cacheDir"
maxAge = -1
[caches.images]
dir = ":resourceDir/_gen"
maxAge = -1
[caches.assets]
dir = ":resourceDir/_gen"
maxAge = -1
```
You can override any of these cache setting in your own `config.toml`.
The placeholders explained:
`:cacheDir`: This is the value of the `cacheDir` config option if set (can also be set via OS env variable `HUGO_CACHEDIR`). It will fall back to `/opt/build/cache/hugo_cache/` on Netlify, or a `hugo_cache` directory below the OS temp dir for the others.
`:resourceDir`: This is the value of the `resourceDir` config option.
`maxAge` is the time in seconds before a cache entry will be evicted, -1 means forever and 0 effectively turns that particular cache off.
This means that if you run your builds on Netlify, all caches configured with `:cacheDir` will be saved and restored on the next build. For other CI vendors, please read their documentation. For an CircleCI example, see https://github.com/bep/hugo-sass-test/blob/6c3960a8f4b90e8938228688bc49bdcdd6b2d99e/.circleci/config.yml
Fixes #5404
Diffstat (limited to 'tpl/data/cache.go')
-rw-r--r-- | tpl/data/cache.go | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/tpl/data/cache.go b/tpl/data/cache.go deleted file mode 100644 index 6c4033160..000000000 --- a/tpl/data/cache.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2017 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 data - -import ( - "crypto/md5" - "encoding/hex" - "errors" - "sync" - - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/helpers" - "github.com/spf13/afero" -) - -var cacheMu sync.RWMutex - -// getCacheFileID returns the cache ID for a string. -func getCacheFileID(cfg config.Provider, id string) string { - hash := md5.Sum([]byte(id)) - return cfg.GetString("cacheDir") + hex.EncodeToString(hash[:]) -} - -// getCache returns the content for an ID from the file cache or an error. -// If the ID is not found, return nil,nil. -func getCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool) ([]byte, error) { - if ignoreCache { - return nil, nil - } - - cacheMu.RLock() - defer cacheMu.RUnlock() - - fID := getCacheFileID(cfg, id) - isExists, err := helpers.Exists(fID, fs) - if err != nil { - return nil, err - } - if !isExists { - return nil, nil - } - - return afero.ReadFile(fs, fID) -} - -// writeCache writes bytes associated with an ID into the file cache. -func writeCache(id string, c []byte, fs afero.Fs, cfg config.Provider, ignoreCache bool) error { - if ignoreCache { - return nil - } - - cacheMu.Lock() - defer cacheMu.Unlock() - - fID := getCacheFileID(cfg, id) - f, err := fs.Create(fID) - if err != nil { - return errors.New("Error: " + err.Error() + ". Failed to create file: " + fID) - } - defer f.Close() - - n, err := f.Write(c) - if err != nil { - return errors.New("Error: " + err.Error() + ". Failed to write to file: " + fID) - } - if n == 0 { - return errors.New("No bytes written to file: " + fID) - } - return nil -} - -func deleteCache(id string, fs afero.Fs, cfg config.Provider) error { - return fs.Remove(getCacheFileID(cfg, id)) -} |