-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
115 lines (97 loc) · 2.67 KB
/
cache.go
File metadata and controls
115 lines (97 loc) · 2.67 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
111
112
113
114
115
package main
import (
"fmt"
"strconv"
"sync"
"github.com/coma-toast/ResumAPI/internal/utils"
"github.com/coma-toast/ResumAPI/pkg/candidate"
log "github.com/sirupsen/logrus"
)
// Cache is the cache for storing episode lists
type Cache struct {
Name string
path string
candidateState map[string]candidate.Candidate
mutex *sync.Mutex
}
// Initialize the cache so it doesn't panic when trying to assign to the map when the map is nil
func (c *Cache) Initialize(path string) error {
log.Debugf("Initializing caches")
c.mutex = &sync.Mutex{}
c.path = path
cache.candidateState = make(map[string]candidate.Candidate)
err := utils.ReadJSONFile(fmt.Sprintf("%s/candidate.json", c.path), &cache.candidateState)
if err != nil {
return err
}
return nil
}
// Set sets the cache
func (c *Cache) SetCandidate(key string, value candidate.Candidate) error {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Debugf("Setting cache: %s\n", key)
c.candidateState[key] = value
err := utils.WriteJSONFile(fmt.Sprintf("%s/candidate.json", c.path), c.candidateState)
if err != nil {
return err
}
return nil
}
func (c *Cache) AddCandidate(value candidate.Candidate) (int, error) {
allCandidates := c.GetAllCandidates()
id := len(allCandidates) + 1
value.ID = id
key := strconv.Itoa(id)
c.mutex.Lock()
defer c.mutex.Unlock()
log.Debugf("Setting cache: %s\n", key)
c.candidateState[key] = value
err := utils.WriteJSONFile(fmt.Sprintf("%s/candidate.json", c.path), c.candidateState)
if err != nil {
return id, err
}
return id, nil
}
// Set sets the cache
func (c *Cache) DeleteCandidate(key string) error {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Debugf("Deleting cache: %s\n", key)
if c.IsSet(key) {
delete(c.candidateState, key)
}
err := utils.WriteJSONFile(fmt.Sprintf("%s/candidate.json", c.path), c.candidateState)
if err != nil {
return err
}
return nil
}
// Get retrieves data from the cache
func (c *Cache) GetCandidate(key string) candidate.Candidate {
log.Debugf("Getting cache: %s\n", key)
c.mutex.Lock()
defer c.mutex.Unlock()
return c.candidateState[key]
}
func (c *Cache) GetCandidateByID(id int) candidate.Candidate {
log.Debugf("Getting cache: %s\n", id)
allCandidates := c.GetAllCandidates()
for _, candidate := range allCandidates {
if candidate.ID == id {
return candidate
}
}
return candidate.Candidate{}
}
// GetAll retrieves all data from the cache
func (c *Cache) GetAllCandidates() map[string]candidate.Candidate {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.candidateState
}
// IsSet determines if the candidate exists already
func (c *Cache) IsSet(key string) bool {
_, ok := c.candidateState[key]
return ok
}