-
Notifications
You must be signed in to change notification settings - Fork 51
/
connectioncache.go
55 lines (45 loc) · 1.02 KB
/
connectioncache.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
package connection
import (
"sync"
)
//TCPCache is an interface to store tcp connections
//keyed with the string.
type TCPCache interface {
Put(string, *TCPConnection)
Get(string) (*TCPConnection, bool)
Remove(string)
Len() int
}
type tcpCache struct {
m map[string]*TCPConnection
sync.RWMutex
}
//NewTCPConnectionCache initializes the tcp connection cache
func NewTCPConnectionCache() TCPCache {
return &tcpCache{m: map[string]*TCPConnection{}}
}
//Put stores the connection object with the key string
func (c *tcpCache) Put(key string, conn *TCPConnection) {
c.Lock()
c.m[key] = conn
c.Unlock()
}
//Get gets the tcp connection object keyed with the key string
func (c *tcpCache) Get(key string) (*TCPConnection, bool) {
c.RLock()
conn, exists := c.m[key]
c.RUnlock()
return conn, exists
}
//Remove remove the connection object keyed with the key string
func (c *tcpCache) Remove(key string) {
c.Lock()
delete(c.m, key)
c.Unlock()
}
func (c *tcpCache) Len() int {
c.Lock()
size := len(c.m)
c.Unlock()
return size
}