-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
93 lines (79 loc) · 2.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"sync"
"github.com/google/go-github/v67/github"
"github.com/xanzy/go-gitlab"
)
const (
githubPullRequestCacheType uint8 = iota
githubSearchResultsCacheType
githubUserCacheType
gitlabUserCacheType
)
type objectCache struct {
mutex *sync.RWMutex
store map[uint8]map[string]any
}
func newObjectCache() *objectCache {
store := make(map[uint8]map[string]any)
store[githubPullRequestCacheType] = make(map[string]any)
store[githubSearchResultsCacheType] = make(map[string]any)
store[githubUserCacheType] = make(map[string]any)
store[gitlabUserCacheType] = make(map[string]any)
return &objectCache{
mutex: new(sync.RWMutex),
store: store,
}
}
func (c objectCache) getGithubPullRequest(query string) *github.PullRequest {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.store[githubPullRequestCacheType][query]; ok {
return pointer(v.(github.PullRequest))
}
return nil
}
func (c objectCache) setGithubPullRequest(query string, result github.PullRequest) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.store[githubPullRequestCacheType][query] = result
}
func (c objectCache) getGithubSearchResults(query string) *github.IssuesSearchResult {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.store[githubSearchResultsCacheType][query]; ok {
return pointer(v.(github.IssuesSearchResult))
}
return nil
}
func (c objectCache) setGithubSearchResults(query string, result github.IssuesSearchResult) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.store[githubSearchResultsCacheType][query] = result
}
func (c objectCache) getGithubUser(username string) *github.User {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.store[gitlabUserCacheType][username]; ok {
return pointer(v.(github.User))
}
return nil
}
func (c objectCache) setGithubUser(username string, user github.User) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.store[gitlabUserCacheType][username] = user
}
func (c objectCache) getGitlabUser(username string) *gitlab.User {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.store[githubUserCacheType][username]; ok {
return pointer(v.(gitlab.User))
}
return nil
}
func (c objectCache) setGitlabUser(username string, user gitlab.User) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.store[githubUserCacheType][username] = user
}