Debian Codegraph @

resources/page/pages_cache.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package page
15
16 import (
17 "slices"
18 "sync"
19 )
20
21 type pageCacheEntry struct {
22 in []Pages
23 out Pages
24 }
25
26 func (entry pageCacheEntry) matches(pageLists []Pages) bool {
27 if len(entry.in) != len(pageLists) {
28 return false
29 }
30 for i, p := range pageLists {
31 if !pagesEqual(p, entry.in[i]) {
32 return false
33 }
34 }
35
36 return true
37 }
38
39 type pageCache struct {
40 sync.RWMutex
41 m map[string][]pageCacheEntry
42 }
43
44 func newPageCache() *pageCache {
45 return &pageCache{m: make(map[string][]pageCacheEntry)}
46 }
47
48 func (c *pageCache) clear() {
49 c.Lock()
50 defer c.Unlock()
51 c.m = make(map[string][]pageCacheEntry)
52 }
53
54 // get/getP gets a Pages slice from the cache matching the given key and
55 // all the provided Pages slices.
56 // If none found in cache, a copy of the first slice is created.
57 //
58 // If an apply func is provided, that func is applied to the newly created copy.
59 //
60 // The getP variant' apply func takes a pointer to Pages.
61 //
62 // The cache and the execution of the apply func is protected by a RWMutex.
63 func (c *pageCache) get(key string, apply func(p Pages), pageLists ...Pages) (Pages, bool) {
64 return c.getP(key, func(p *Pages) {
65 if apply != nil {
66 apply(*p)
67 }
68 }, pageLists...)
69 }
70
71 func (c *pageCache) getP(key string, apply func(p *Pages), pageLists ...Pages) (Pages, bool) {
72 c.RLock()
73 if cached, ok := c.m[key]; ok {
74 for _, entry := range cached {
75 if entry.matches(pageLists) {
76 c.RUnlock()
77 return entry.out, true
78 }
79 }
80 }
81 c.RUnlock()
82
83 c.Lock()
84 defer c.Unlock()
85
86 // double-check
87 if cached, ok := c.m[key]; ok {
88 for _, entry := range cached {
89 if entry.matches(pageLists) {
90 return entry.out, true
91 }
92 }
93 }
94
95 p := pageLists[0]
96 pagesCopy := slices.Clone(p)
97
98 if apply != nil {
99 apply(&pagesCopy)
100 }
101
102 entry := pageCacheEntry{in: pageLists, out: pagesCopy}
103 if v, ok := c.m[key]; ok {
104 c.m[key] = append(v, entry)
105 } else {
106 c.m[key] = []pageCacheEntry{entry}
107 }
108
109 return pagesCopy, false
110 }
111
112 // pagesEqual returns whether p1 and p2 are equal.
113 func pagesEqual(p1, p2 Pages) bool {
114 if p1 == nil && p2 == nil {
115 return true
116 }
117
118 if p1 == nil || p2 == nil {
119 return false
120 }
121
122 if p1.Len() != p2.Len() {
123 return false
124 }
125
126 if p1.Len() == 0 {
127 return true
128 }
129
130 for i := range p1 {
131 if p1[i] != p2[i] {
132 return false
133 }
134 }
135 return true
136 }