-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cache.go
62 lines (52 loc) · 1.38 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
package dinero
import (
"fmt"
"time"
cache "github.com/patrickmn/go-cache"
)
// CacheService handles in-memory caching of our rates.
type CacheService struct {
client *Client
store *cache.Cache
}
// NewCacheService creates a new handler for this service.
func NewCacheService(
client *Client,
store *cache.Cache,
) *CacheService {
return &CacheService{
client,
store,
}
}
// Get will return our in-memory stored currency/rates.
func (s *CacheService) Get(base string, date time.Time) (*RateResponse, bool) {
if x, found := s.store.Get(getCacheKey(base, date)); found {
return x.(*RateResponse), found
}
return nil, false
}
// Store will store our currency/rates in-memory.
func (s *CacheService) Store(rsp *RateResponse, date time.Time) {
// Set a stored timestamp.
rsp.Timestamp = time.Now().Unix()
s.store.Set(
getCacheKey(rsp.Base, date),
rsp,
cache.DefaultExpiration,
)
}
// IsExpired checks whether the rate stored is expired.
func (s *CacheService) IsExpired(base string, date time.Time) bool {
if _, found := s.store.Get(getCacheKey(base, date)); found {
return false
}
return true
}
// Expire will expire the cache for a given base currency.
func (s *CacheService) Expire(base string, date time.Time) {
s.store.Delete(getCacheKey(base, date))
}
func getCacheKey(base string, date time.Time) string {
return fmt.Sprintf("%s_%s", base, date.Format("2006-01-02"))
}