-
Notifications
You must be signed in to change notification settings - Fork 2
/
tail.go
149 lines (133 loc) · 2.58 KB
/
tail.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tail
import (
"bytes"
"errors"
"fmt"
"html/template"
"time"
_ "github.com/boltdb/bolt"
_ "github.com/garyburd/redigo/redis"
)
type Asset struct {
ID string
Source string
Data []byte
TTL time.Duration
Clients map[string]*Client
Cache Cache
}
func New(id string, src string, ttl time.Duration, cache Cache) (*Asset, error) {
if cache != nil {
asset := &Asset{
ID: id, Source: src,
TTL: ttl,
Clients: make(map[string]*Client),
Cache: cache,
}
asset.loadAsset()
asset.WatchFile()
asset.cleanClient()
return asset, nil
}
return nil, errors.New("Cache cannot be nil")
}
func (a *Asset) cleanClient() {
go func() {
for {
time.AfterFunc(a.TTL, func() {
for _, c := range a.Clients {
if !c.Valid {
fmt.Printf("Client: %s has expired.\n", c.ID)
err := a.Del(c.ID)
fmt.Println(c.ID, "Deleted")
if err != nil {
fmt.Println(err)
}
}
}
})
time.Sleep(a.TTL)
}
}()
}
func (a *Asset) loadAsset() error {
var err error
a.Data, err = ReadAssetFile(a.Source)
if err != nil {
return err
}
return nil
}
func (a *Asset) Get(id string) []byte {
cid := a.buildId(id)
if a.Clients[cid] == nil {
return nil
}
a.Clients[cid].renewClient()
return a.Cache.Get(cid)
}
func (a *Asset) Set(id string, data []byte) error {
cid := a.buildId(id)
if a.Clients[cid] == nil {
clt := NewClient(cid, a.TTL)
a.Clients[cid] = clt
}
err := a.Cache.Set(cid, data)
if err != nil {
return err
}
return nil
}
func (a *Asset) Update(id string, data []byte) error {
err := a.Cache.Update(id, data)
if err != nil {
return err
}
return nil
}
func (a *Asset) Del(id string) error {
delete(a.Clients, id)
err := a.Cache.Del(id)
if err != nil {
return err
}
return nil
}
func (a *Asset) create(id string, data interface{}) error {
var buff bytes.Buffer
tmp := template.New(a.ID)
tmp.Parse(string(a.Data))
err := tmp.Execute(&buff, data)
if err != nil {
return err
}
err = a.Set(id, buff.Bytes())
if err != nil {
return err
}
cid := a.buildId(id)
clt := NewClient(cid, a.TTL)
a.Clients[cid] = clt
return nil
}
func (a *Asset) Create(id string, data interface{}) error {
err := a.create(id, data)
if err != nil {
return err
}
return nil
}
func (a *Asset) GetOrNew(id string, data interface{}) ([]byte, error) {
cid := a.buildId(id)
if a.Clients[cid] == nil {
err := a.create(id, data)
if err != nil {
return nil, err
}
}
a.Clients[cid].renewClient()
return a.Cache.Get(cid), nil
}
func (a *Asset) buildId(id string) string {
return fmt.Sprintf("%s:%s", a.ID, id)
}