-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
110 lines (94 loc) · 2.73 KB
/
cache.go
File metadata and controls
110 lines (94 loc) · 2.73 KB
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
package main
import (
"sync"
"github.com/google/go-github/v74/github"
"github.com/xanzy/go-gitlab"
)
const (
githubBranchesCacheType uint8 = iota
githubPullRequestCacheType
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[githubBranchesCacheType] = make(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) getGithubBranches(query string) []*github.Branch {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.store[githubBranchesCacheType][query]; ok {
return v.([]*github.Branch)
}
return nil
}
func (c objectCache) setGithubBranches(query string, result []*github.Branch) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.store[githubBranchesCacheType][query] = result
}
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
}