-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
123 lines (100 loc) · 2.76 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
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
package spotify
import (
"fmt"
"sync"
)
type Cache interface {
// Playlist cache methods
Add(playlistId string, track *ImmutableSpotifyTrack) error
ReplaceAll(playlistId string, tracks []*ImmutableSpotifyTrack) error
Remove(playlistId string, track *ImmutableSpotifyTrack) error
Get(playlistId string) []*ImmutableSpotifyTrack
IsCached(playlistId string) bool
}
type spotifyCache struct {
playlist map[string]*playlistCache
playlistLock sync.RWMutex
}
type playlistCache struct {
tracks []*ImmutableSpotifyTrack
tracksLock sync.RWMutex
}
func newCache() Cache {
playlist := make(map[string]*playlistCache)
return &spotifyCache{
playlist: playlist,
}
}
func (s *spotifyCache) getOrCreate(playlistId string) *playlistCache {
s.playlistLock.Lock()
defer s.playlistLock.Unlock()
p, ok := s.playlist[playlistId]
if ok {
return p
} else {
p = &playlistCache{}
s.playlist[playlistId] = p
return p
}
}
func (s *spotifyCache) Add(playlistId string, track *ImmutableSpotifyTrack) error {
return s.getOrCreate(playlistId).add(track)
}
func (s *spotifyCache) Get(playlistId string) []*ImmutableSpotifyTrack {
return s.getOrCreate(playlistId).get()
}
func (s *spotifyCache) ReplaceAll(playlistId string, tracks []*ImmutableSpotifyTrack) error {
return s.getOrCreate(playlistId).replaceAll(tracks)
}
func (s *spotifyCache) Remove(playlistId string, track *ImmutableSpotifyTrack) error {
return s.getOrCreate(playlistId).remove(track)
}
func (s *spotifyCache) IsCached(playlistId string) bool {
return s.getOrCreate(playlistId).size() > 0
}
func (p *playlistCache) add(track *ImmutableSpotifyTrack) error {
if track == nil {
return fmt.Errorf("Cannot add nil track")
}
p.tracksLock.Lock()
defer p.tracksLock.Unlock()
p.tracks = append(p.tracks, track)
return nil
}
func (p *playlistCache) get() []*ImmutableSpotifyTrack {
p.tracksLock.RLock()
defer p.tracksLock.RUnlock()
return append([]*ImmutableSpotifyTrack{}, p.tracks...)
}
func (p *playlistCache) size() int {
p.tracksLock.RLock()
defer p.tracksLock.RUnlock()
return len(p.tracks)
}
func (p *playlistCache) replaceAll(tracks []*ImmutableSpotifyTrack) error {
p.tracksLock.Lock()
defer p.tracksLock.Unlock()
p.tracks = append([]*ImmutableSpotifyTrack{}, tracks...)
return nil
}
func (p *playlistCache) remove(track *ImmutableSpotifyTrack) error {
if track == nil {
return fmt.Errorf("Cannot remove nil track")
}
p.tracksLock.Lock()
defer p.tracksLock.Unlock()
i := 0
for _, t := range p.tracks {
// if ids are different in place copy to the new array
if t.Id() != track.Id() {
p.tracks[i] = t
i++
}
}
// if the length did not change noting was discarded
if len(p.tracks) == i {
return fmt.Errorf("Track %q to remove was not found", track)
}
p.tracks = p.tracks[:i]
return nil
}