github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/cache.go (about)

     1  package utils
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/projecteru2/core/engine"
     7  
     8  	"github.com/patrickmn/go-cache"
     9  )
    10  
    11  // EngineCache connections
    12  // otherwise they'll leak
    13  type EngineCache struct {
    14  	cache *cache.Cache
    15  }
    16  
    17  // NewEngineCache creates Cache instance
    18  func NewEngineCache(expire time.Duration, cleanupInterval time.Duration) *EngineCache {
    19  	return &EngineCache{
    20  		cache: cache.New(expire, cleanupInterval),
    21  	}
    22  }
    23  
    24  // Set connection with host
    25  func (c *EngineCache) Set(endpoint string, client engine.API) {
    26  	c.cache.Set(endpoint, client, cache.DefaultExpiration)
    27  }
    28  
    29  // Get connection by host
    30  func (c *EngineCache) Get(endpoint string) engine.API {
    31  	e, found := c.cache.Get(endpoint)
    32  	if found {
    33  		return e.(engine.API)
    34  	}
    35  	return nil
    36  }
    37  
    38  // Delete connection by host
    39  func (c *EngineCache) Delete(host string, _ ...string) {
    40  	c.cache.Delete(host)
    41  }