-
Notifications
You must be signed in to change notification settings - Fork 3
/
gfcache.go
77 lines (68 loc) · 2.36 KB
/
gfcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2018 gf Author(https://github.com/snail007/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/snail007/gf.
// Package gfcache provides reading and caching for file contents.
package gfcache
import (
"time"
"github.com/snail007/gf/internal/cmdenv"
"github.com/snail007/gf/os/gcache"
"github.com/snail007/gf/os/gfile"
"github.com/snail007/gf/os/gfsnotify"
)
const (
// Default expire time for file content caching in seconds.
gDEFAULT_CACHE_EXPIRE = 60
)
var (
// Default expire time for file content caching in seconds.
cacheExpire = cmdenv.Get("gf.gfcache.expire", gDEFAULT_CACHE_EXPIRE).Int() * 1000
)
// GetContents returns string content of given file by <path> from cache.
// If there's no content in the cache, it will read it from disk file specified by <path>.
// The parameter <expire> specifies the caching time for this file content in seconds.
func GetContents(path string, duration ...interface{}) string {
return string(GetBinContents(path, duration...))
}
// GetBinContents returns []byte content of given file by <path> from cache.
// If there's no content in the cache, it will read it from disk file specified by <path>.
// The parameter <expire> specifies the caching time for this file content in seconds.
func GetBinContents(path string, duration ...interface{}) []byte {
k := cacheKey(path)
e := cacheExpire
if len(duration) > 0 {
e = getSecondExpire(duration[0])
}
r := gcache.GetOrSetFuncLock(k, func() interface{} {
b := gfile.GetBytes(path)
if b != nil {
// Adding this <path> to gfsnotify,
// it will clear its cache if there's any changes of the file.
_, _ = gfsnotify.Add(path, func(event *gfsnotify.Event) {
gcache.Remove(k)
gfsnotify.Exit()
})
}
return b
}, e*1000)
if r != nil {
return r.([]byte)
}
return nil
}
// getSecondExpire converts parameter <duration> to int type in seconds.
//
// Note that there's some performance cost in type assertion here, but it's valuable.
func getSecondExpire(duration interface{}) int {
if d, ok := duration.(time.Duration); ok {
return int(d.Nanoseconds() / 1000000000)
} else {
return duration.(int)
}
}
// cacheKey produces the cache key for gcache.
func cacheKey(path string) string {
return "gf.gfcache:" + path
}