-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
61 lines (53 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
// Copyright 2018 sshfp authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package sshfp
// Cache for DNS SSHFP entries
type Cache interface {
Add(e ...*Entry) error
Get(hostname string, algo ...Algorithm) (Entries, bool)
Remove(e *Entry) error
}
// MemoryCache is a in-memory cache
type MemoryCache struct {
c map[string]Entries
}
// NewMemoryCache creates a new in-memory cache
func NewMemoryCache() (*MemoryCache, error) {
return &MemoryCache{c: make(map[string]Entries)}, nil
}
func (mc *MemoryCache) add(hostname string, e *Entry) {
entries := mc.c[hostname]
if len(entries) == 0 {
entries = Entries{}
mc.c[hostname] = entries
}
mc.c[hostname] = append(mc.c[hostname], e)
}
// Add entry to the cache
func (mc *MemoryCache) Add(e ...*Entry) error {
for _, entry := range e {
mc.add(entry.Hostname, entry)
}
return nil
}
// Get entries from the cache
func (mc *MemoryCache) Get(hostname string, algo ...Algorithm) (Entries, bool) {
entries, ok := mc.c[hostname]
if len(algo) == 1 {
algorithm := uint8(algo[0])
fentries := Entries{}
for _, entry := range entries {
if entry.Algorithm != algorithm {
continue
}
fentries = append(fentries, entry)
}
entries = fentries
}
return entries, ok
}
// Remove entry from the cache
func (mc *MemoryCache) Remove(e *Entry) error {
return nil
}