-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
96 lines (76 loc) · 1.83 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
package awssh
import (
"time"
"github.com/adelowo/onecache"
"github.com/adelowo/onecache/filesystem"
"github.com/aws/aws-sdk-go/aws/credentials"
homedir "github.com/mitchellh/go-homedir"
)
const (
CachePath string = "~/.config/awssh/cache"
)
type Cache struct {
Store *filesystem.FSStore
Marshal *onecache.CacheSerializer
Key string
}
func NewCache(path, key string) (c *Cache, err error) {
fullPath, err := homedir.Expand(path)
if err != nil {
return nil, err
}
store := filesystem.MustNewFSStore(fullPath)
marshal := onecache.NewCacheSerializer()
c = &Cache{
Store: store,
Marshal: marshal,
Key: key,
}
return c, nil
}
func (c *Cache) Save(creds *credentials.Value, expire time.Duration) (err error) {
dataByte, err := c.Marshal.Serialize(&creds)
if err != nil {
return err
}
err = c.Store.Set(c.Key, dataByte, expire)
return err
}
func (c *Cache) Load() (creds *credentials.Value, err error) {
credsByte, err := c.Store.Get(c.Key)
if err != nil {
return nil, err
}
c.Marshal.DeSerialize(credsByte, &creds)
return creds, nil
}
/*
func SaveCache(filePath, key string, creds *credentials.Value, expire time.Duration) (err error) {
marshal := onecache.NewCacheSerializer()
dataByte, err := marshal.Serialize(&creds)
if err != nil {
return err
}
fullPath, err := homedir.Expand(filePath)
if err != nil {
return err
}
store := filesystem.MustNewFSStore(fullPath)
err = store.Set(key, dataByte, expire)
return err
}
func LoadCache(filePath, key string) (creds *credentials.Value, err error) {
fullPath, err := homedir.Expand(filePath)
if err != nil {
return nil, err
}
store := filesystem.MustNewFSStore(fullPath)
credsByte, err := store.Get(key)
if err != nil {
return nil, err
}
marshal := onecache.NewCacheSerializer()
marshal.DeSerialize(credsByte, &creds)
return creds, nil
}
*/