github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/connection/connectioncache.go (about)

     1  package connection
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  //TCPCache is an interface to store tcp connections
     8  //keyed with the string.
     9  type TCPCache interface {
    10  	Put(string, *TCPConnection)
    11  	Get(string) (*TCPConnection, bool)
    12  	Remove(string)
    13  	Len() int
    14  }
    15  
    16  type tcpCache struct {
    17  	m map[string]*TCPConnection
    18  	sync.RWMutex
    19  }
    20  
    21  //NewTCPConnectionCache initializes the tcp connection cache
    22  func NewTCPConnectionCache() TCPCache {
    23  	return &tcpCache{m: map[string]*TCPConnection{}}
    24  }
    25  
    26  //Put stores the connection object with the key string
    27  func (c *tcpCache) Put(key string, conn *TCPConnection) {
    28  	c.Lock()
    29  	c.m[key] = conn
    30  	c.Unlock()
    31  }
    32  
    33  //Get gets the tcp connection object keyed with the key string
    34  func (c *tcpCache) Get(key string) (*TCPConnection, bool) {
    35  	c.RLock()
    36  	conn, exists := c.m[key]
    37  	c.RUnlock()
    38  
    39  	return conn, exists
    40  }
    41  
    42  //Remove remove the connection object keyed with the key string
    43  func (c *tcpCache) Remove(key string) {
    44  	c.Lock()
    45  	delete(c.m, key)
    46  	c.Unlock()
    47  }
    48  
    49  func (c *tcpCache) Len() int {
    50  	c.Lock()
    51  	size := len(c.m)
    52  	c.Unlock()
    53  
    54  	return size
    55  }