Skip to content
Snippets Groups Projects
Select Git revision
  • 082559c5f6d3ee92bc3ad02ae80c300c8e883044
  • master default protected
  • feature/PRXS-3383-CollectionsSort
  • feature/2781-SpacesLoggingMiddleware
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • PRXS-3421-RecursiveReferences
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

manager_service.pb.go

Blame
  • cache.go 2.03 KiB
    package cache
    
    import (
    	"fmt"
    	"time"
    
    	"git.perx.ru/perxis/perxis-go/pkg/service"
    	lru "github.com/hashicorp/golang-lru/v2"
    	"go.uber.org/zap"
    )
    
    const (
    	defaultCacheSize = 1000
    	defaultTTL       = 30 * time.Second
    )
    
    type Cache interface {
    	Set(key, value any) error
    	Get(key any) (any, error)
    	Remove(key any) error
    }
    
    var _ Cache = &MemoryCache{}
    
    type MemoryCache struct {
    	cache  *lru.Cache[interface{}, interface{}]
    	ttl    time.Duration
    	logger *zap.Logger
    }
    
    type item struct {
    	value     interface{}
    	expiredAt time.Time
    }
    
    func NewCache(size int, ttl time.Duration, opts ...interface{}) *MemoryCache {
    	if size == 0 {
    		size = defaultCacheSize
    	}
    	if ttl == 0 {
    		ttl = defaultTTL
    	}
    	c, err := lru.New[interface{}, interface{}](size)
    	if err != nil {
    		panic(err)
    	}
    	ch := &MemoryCache{
    		cache:  c,
    		ttl:    ttl,
    		logger: zap.NewNop(),
    	}
    
    	for _, o := range opts {
    		switch p := o.(type) {
    		case *zap.Logger:
    			ch.logger = p
    		}
    	}
    
    	ch.logger = ch.logger.Named("Cache")
    
    	return ch
    }
    
    func (c *MemoryCache) Set(key, value interface{}) (err error) {
    	c.cache.Add(key, &item{value: value, expiredAt: time.Now().Add(c.ttl)})
    	c.logger.Debug("Set", zap.String("key", fmt.Sprintf("%v", key)), zap.String("ptr", fmt.Sprintf("%p", value)))
    	return nil
    }
    
    func (c *MemoryCache) Get(key interface{}) (value interface{}, err error) {
    	val, ok := c.cache.Get(key)
    	if ok {
    		v := val.(*item)
    		if v.expiredAt.Before(time.Now()) {
    			_ = c.Remove(key)
    			c.logger.Debug("Expired", zap.String("key", fmt.Sprintf("%v", key)), zap.String("ptr", fmt.Sprintf("%p", v.value)))
    			return nil, service.ErrNotFound
    		}
    		c.logger.Debug("Hit", zap.String("key", fmt.Sprintf("%v", key)), zap.String("ptr", fmt.Sprintf("%p", v.value)))
    		return v.value, nil
    	}
    	c.logger.Debug("Miss", zap.String("key", fmt.Sprintf("%v", key)))
    	return nil, service.ErrNotFound
    }
    
    func (c *MemoryCache) Remove(key interface{}) (err error) {
    	present := c.cache.Remove(key)
    	c.logger.Debug("Remove", zap.String("key", fmt.Sprintf("%v", key)))
    
    	if !present {
    		err = service.ErrNotFound
    	}
    
    	return
    }