github.com/TykTechnologies/tyk/internal/memorycache
No package summary is available.
Package
Files: 4. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.
Types
Bucket
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
| capacity |
|
No comment on field. |
| remaining |
|
No comment on field. |
| reset |
|
No comment on field. |
| rate |
|
No comment on field. |
| mutex |
|
No comment on field. |
type Bucket struct {
capacity uint
remaining uint
reset time.Time
rate time.Duration
mutex sync.Mutex
}
BucketStorage
BucketStorage is a non thread-safe in-memory leaky bucket factory.
| Field name | Field type | Comment |
|---|---|---|
| buckets |
|
No comment on field. |
type BucketStorage struct {
buckets *Cache
}
Cache
Cache is a synchronised map of items that auto-expire once stale
| Field name | Field type | Comment |
|---|---|---|
| mutex |
|
No comment on field. |
| ttl |
|
No comment on field. |
| items |
|
No comment on field. |
type Cache struct {
mutex sync.RWMutex
ttl time.Duration
items map[string]*Item
}
Item
Item represents a record in the cache map.
| Field name | Field type | Comment |
|---|---|---|
|
No comment on field. | |
| data |
|
No comment on field. |
| expires |
|
No comment on field. |
type Item struct {
sync.RWMutex
data *Bucket
expires *time.Time
}
Functions
func New
New initializes the in-memory bucket store.
func New(ctx context.Context) *BucketStorage {
return &BucketStorage{
buckets: NewCache(ctx, 10*time.Minute),
}
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func NewCache
NewCache is a helper to create instance of the Cache struct. The ctx is used to cancel the TTL map cleanup goroutine.
func NewCache(ctx context.Context, duration time.Duration) *Cache {
cache := &Cache{
ttl: duration,
items: map[string]*Item{},
}
go cache.startCleanupTimer(ctx)
return cache
}
Cognitive complexity: 2, Cyclomatic complexity: 1
func (*Bucket) Add
Add to the bucket.
func (b *Bucket) Add(amount uint) (model.BucketState, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
if time.Now().After(b.reset) {
b.reset = time.Now().Add(b.rate)
b.remaining = b.capacity
}
if amount > b.remaining {
return model.BucketState{Capacity: b.capacity, Remaining: b.remaining, Reset: b.reset}, model.ErrBucketFull
}
b.remaining -= amount
return model.BucketState{Capacity: b.capacity, Remaining: b.remaining, Reset: b.reset}, nil
}
Cognitive complexity: 6, Cyclomatic complexity: 3
func (*Bucket) Capacity
func (b *Bucket) Capacity() uint {
return b.capacity
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*Bucket) Remaining
Remaining space in the bucket.
func (b *Bucket) Remaining() uint {
return b.remaining
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*Bucket) Reset
Reset returns when the bucket will be drained.
func (b *Bucket) Reset() time.Time {
return b.reset
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*BucketStorage) Create
Create a bucket.
func (s *BucketStorage) Create(name string, capacity uint, rate time.Duration) (model.Bucket, error) {
b, ok := s.buckets.Get(name)
if ok {
return b, nil
}
b = &Bucket{
capacity: capacity,
remaining: capacity,
reset: time.Now().Add(rate),
rate: rate,
}
s.buckets.Set(name, b)
return b, nil
}
Cognitive complexity: 3, Cyclomatic complexity: 2
func (*Cache) Count
Count returns the number of items in the cache (helpful for tracking memory leaks)
func (cache *Cache) Count() int {
cache.mutex.RLock()
count := len(cache.items)
cache.mutex.RUnlock()
return count
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*Cache) Get
Get is a thread-safe way to lookup items Every lookup, also touches the item, hence extending it's life
func (cache *Cache) Get(key string) (data *Bucket, found bool) {
cache.mutex.Lock()
item, exists := cache.items[key]
if !exists || item.expired() {
data = &Bucket{}
found = false
} else {
item.touch(cache.ttl)
data = item.data
found = true
}
cache.mutex.Unlock()
return
}
Cognitive complexity: 5, Cyclomatic complexity: 3
func (*Cache) Set
Set is a thread-safe way to add new items to the map
func (cache *Cache) Set(key string, data *Bucket) {
cache.mutex.Lock()
item := &Item{data: data}
item.touch(cache.ttl)
cache.items[key] = item
cache.mutex.Unlock()
}
Cognitive complexity: 1, Cyclomatic complexity: 1
Private functions
func cleanup
cleanup ()
func clear
clear ()
func startCleanupTimer
startCleanupTimer (ctx context.Context)
References: time.NewTicker, time.Second.
func expired
expired () bool
References: time.Now.
func touch
touch (duration time.Duration)
References: time.Now.
Tests
Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 1. Benchmarks: 0.