-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
114 lines (92 loc) · 2.53 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
package main
import (
"bytes"
"errors"
"io"
"net/http"
"strconv"
"strings"
"sync"
)
// An Image is just a slice of bytes containing the data of the image.
type Image []byte
// NewImageReader returns an io.Reader pointing to the image data.
func NewImageReader(img Image) io.Reader {
return bytes.NewReader(img)
}
// ImageCache is an image cache. It is safe for concurrent use by multiple
// goroutines.
type ImageCache struct {
// images contains the images data.
images []Image
// mu guards images.
mutex sync.RWMutex
}
// NewImageCache returns a new ImageCache with the specified capacity.
func NewImageCache(capacity int) *ImageCache {
cache := &ImageCache{
images: make([]Image, capacity),
}
return cache
}
// Set creates an image at index idx copying the specified data.
func (cache *ImageCache) Set(idx int, data []byte) error {
cache.mutex.Lock()
defer cache.mutex.Unlock()
if idx < 0 || idx >= len(cache.images) {
return errors.New("index is out of bounds")
}
img := make(Image, len(data))
copy(img, data)
cache.images[idx] = img
return nil
}
// Get returns a copy of the image at index idx. If the image has not been
// initialized with data, an error is returned.
func (cache *ImageCache) Get(idx int) (Image, error) {
cache.mutex.RLock()
defer cache.mutex.RUnlock()
if idx < 0 || idx >= len(cache.images) {
return nil, errors.New("index is out of bounds")
}
if cache.images[idx] == nil {
return nil, errors.New("uninitialized image")
}
img := make(Image, len(cache.images[idx]))
copy(img, cache.images[idx])
return img, nil
}
// ServeHTTP implements an http.Handler that serves an image from the cache. It
// expects the URL path to have the format "/idx", where idx is an integer that
// specifies the index of the image in the cache.
func (cache *ImageCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logf("%v - %v - %v %v",
r.RemoteAddr, r.UserAgent(), r.Method, r.URL)
path := strings.TrimPrefix(r.URL.Path, "/")
var (
idx int
err error
)
if path == "" {
idx = 0
} else {
idx, err = strconv.Atoi(path)
if err != nil {
logf("Could not parse index (%v)", r.URL.Path)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
img, err := cache.Get(idx)
if err != nil {
logf("Could not get image: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(img)))
if _, err := io.Copy(w, NewImageReader(img)); err != nil {
logf("Could not write image: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}