Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/operators/cache/cache.go
2851 views
1
package cache
2
3
import (
4
"regexp"
5
"sync"
6
7
"github.com/Knetic/govaluate"
8
"github.com/projectdiscovery/gcache"
9
)
10
11
var (
12
initOnce sync.Once
13
mu sync.RWMutex
14
15
regexCap = 4096
16
dslCap = 4096
17
18
regexCache gcache.Cache[string, *regexp.Regexp]
19
dslCache gcache.Cache[string, *govaluate.EvaluableExpression]
20
)
21
22
func initCaches() {
23
initOnce.Do(func() {
24
regexCache = gcache.New[string, *regexp.Regexp](regexCap).LRU().Build()
25
dslCache = gcache.New[string, *govaluate.EvaluableExpression](dslCap).LRU().Build()
26
})
27
}
28
29
func SetCapacities(regexCapacity, dslCapacity int) {
30
// ensure caches are initialized under initOnce, so later Regex()/DSL() won't re-init
31
initCaches()
32
33
mu.Lock()
34
defer mu.Unlock()
35
36
if regexCapacity > 0 {
37
regexCap = regexCapacity
38
}
39
if dslCapacity > 0 {
40
dslCap = dslCapacity
41
}
42
if regexCapacity <= 0 && dslCapacity <= 0 {
43
return
44
}
45
// rebuild caches with new capacities
46
regexCache = gcache.New[string, *regexp.Regexp](regexCap).LRU().Build()
47
dslCache = gcache.New[string, *govaluate.EvaluableExpression](dslCap).LRU().Build()
48
}
49
50
func Regex() gcache.Cache[string, *regexp.Regexp] {
51
initCaches()
52
mu.RLock()
53
defer mu.RUnlock()
54
return regexCache
55
}
56
57
func DSL() gcache.Cache[string, *govaluate.EvaluableExpression] {
58
initCaches()
59
mu.RLock()
60
defer mu.RUnlock()
61
return dslCache
62
}
63
64