-
Notifications
You must be signed in to change notification settings - Fork 0
/
tohtml.go
59 lines (46 loc) · 930 Bytes
/
tohtml.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
package tohtml
import (
"sync"
"github.com/bozso/gotoolbox/hash"
tth "github.com/buildkite/terminal-to-html/v3"
)
type Renderer interface {
Render([]byte) []byte
}
type cache map[hash.ID64][]byte
type Cache struct {
cache
hasher hash.Hasher
}
func NewCache(h hash.Hasher) (c Cache) {
return Cache {
hasher: h,
cache: make(cache),
}
}
func (c *Cache) Render(b []byte) (out []byte) {
c.hasher.Reset()
c.hasher.Write(b)
id := c.hasher.Sum64()
out, ok := c.cache[id]
if ok {
return
}
out = tth.Render(b)
c.cache[id] = out
return
}
func (c Cache) WithMutex() (m MutexCache) {
return MutexCache {
Cache: c,
}
}
type MutexCache struct {
Cache
mutex sync.Mutex
}
func (m *MutexCache) Render(b []byte) (out []byte) {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.Cache.Render(b)
}