forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_cache.go
155 lines (129 loc) · 3.46 KB
/
http_cache.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package ravendb
import (
//"fmt"
"math"
"sync"
"sync/atomic"
"time"
)
// equivalent of com.google.common.cache.Cache, specialized for String -> HttpCacheItem mapping
// TODO: match semantics
type genericCache struct {
softValues bool
maximumWeight int
weighter func(string, *httpCacheItem) int
data *sync.Map
}
func (c *genericCache) size() int {
// this is probably only ever going to be called from tests
// fine to do O(N) work here
length := 0
c.data.Range(func(_, _ interface{}) bool {
length++
return true
})
return length
}
func (c *genericCache) invalidateAll() {
c.data = &sync.Map{}
}
func (c *genericCache) getIfPresent(uri string) *httpCacheItem {
item, found := c.data.Load(uri)
if !found {
return nil
}
return item.(*httpCacheItem)
}
func (c *genericCache) put(uri string, i *httpCacheItem) {
//fmt.Printf("genericCache.put(): url: %s, changeVector: %s, len(result): %d\n", uri, *i.changeVector, len(i.payload))
// TODO: probably implement cache eviction
c.data.Store(uri, i)
}
type httpCache struct {
items *genericCache
generation int32 // atomic
}
func (c *httpCache) incGeneration() {
atomic.AddInt32(&c.generation, 1)
}
func (c *httpCache) getGeneration() int {
v := atomic.LoadInt32(&c.generation)
return int(v)
}
func newHttpCache(size int) *httpCache {
if size == 0 {
size = 1 * 1024 * 1024 // TODO: check what is default size of com.google.common.cache.Cache is
}
cache := &genericCache{
softValues: true,
maximumWeight: size,
weighter: func(k string, v *httpCacheItem) int {
return len(v.payload) + 20
},
data: &sync.Map{ },
}
return &httpCache{
items: cache,
}
}
func (c *httpCache) GetNumberOfItems() int {
return c.items.size()
}
func (c *httpCache) close() {
c.items.invalidateAll()
c.items = nil
}
func (c *httpCache) set(url string, changeVector *string, result []byte) {
httpCacheItem := newHttpCacheItem()
httpCacheItem.changeVector = changeVector
httpCacheItem.payload = result
httpCacheItem.cache = c
httpCacheItem.generation = c.getGeneration()
c.items.put(url, httpCacheItem)
}
// returns cacheItem, changeVector and response
func (c *httpCache) get(url string) (*releaseCacheItem, *string, []byte) {
item := c.items.getIfPresent(url)
if item != nil {
//fmt.Printf("HttpCache.get(): found url: %s, changeVector: %s, len(payload): %d\n", url, *item.changeVector, len(item.payload))
return newReleaseCacheItem(item), item.changeVector, item.payload
}
//fmt.Printf("HttpCache.get(): didn't find url: %s\n", url)
return newReleaseCacheItem(nil), nil, nil
}
func (c *httpCache) setNotFound(url string) {
//fmt.Printf("HttpCache.setNotFound(): url: %s\n", url)
httpCacheItem := newHttpCacheItem()
s := "404 response"
httpCacheItem.changeVector = &s
httpCacheItem.cache = c
httpCacheItem.generation = c.getGeneration()
c.items.put(url, httpCacheItem)
}
type releaseCacheItem struct {
item *httpCacheItem
}
func newReleaseCacheItem(item *httpCacheItem) *releaseCacheItem {
return &releaseCacheItem{
item: item,
}
}
func (i *releaseCacheItem) notModified() {
if i.item != nil {
i.item.lastServerUpdate = time.Now()
}
}
func (i *releaseCacheItem) getAge() time.Duration {
if i.item == nil {
return time.Duration(math.MaxInt64)
}
return time.Since(i.item.lastServerUpdate)
}
func (i *releaseCacheItem) getMightHaveBeenModified() bool {
currGen := i.item.generation
itemGen := i.item.cache.getGeneration()
return currGen != itemGen
}
func (i *releaseCacheItem) close() {
// no-op
}