forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
70 lines (64 loc) · 1.4 KB
/
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
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/memcache"
)
type Cached struct {
Open int
Fixed int
Invalid int
}
func CacheGet(c context.Context, r *http.Request, ns string) (*Cached, error) {
accessLevel := accessLevel(c, r)
key := fmt.Sprintf("%v-%v", ns, accessLevel)
v := new(Cached)
_, err := memcache.Gob.Get(c, key, v)
if err != nil && err != memcache.ErrCacheMiss {
return nil, err
}
if err == nil {
return v, nil
}
if v, err = buildCached(c, ns, accessLevel); err != nil {
return nil, err
}
item := &memcache.Item{
Key: key,
Object: v,
Expiration: time.Hour,
}
if err := memcache.Gob.Set(c, item); err != nil {
return nil, err
}
return v, nil
}
func buildCached(c context.Context, ns string, accessLevel AccessLevel) (*Cached, error) {
v := &Cached{}
bugs, _, err := loadNamespaceBugs(c, ns)
if err != nil {
return nil, err
}
for _, bug := range bugs {
switch bug.Status {
case BugStatusOpen:
if accessLevel < bug.sanitizeAccess(accessLevel) {
continue
}
if len(bug.Commits) == 0 {
v.Open++
} else {
v.Fixed++
}
case BugStatusFixed:
v.Fixed++
case BugStatusInvalid:
v.Invalid++
}
}
return v, nil
}