Go API Documentation

github.com/TykTechnologies/tyk/internal/cache

No package summary is available.

Package

Files: 4. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const (
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to NewCache().
	DefaultExpiration = 0
)

Types

Cache

Cache holds key-value pairs with a TTL.

Field name Field type Comment
expiration

time.Duration

expiration (<= 0 means never expire).

janitor

*Janitor

janitor holds a clean up goroutine

mu

sync.RWMutex

cache items and protecting mutex

items

map[string]Item

No comment on field.
type Cache struct {
	// expiration (<= 0 means never expire).
	expiration	time.Duration

	// janitor holds a clean up goroutine
	janitor	*Janitor

	// cache items and protecting mutex
	mu	sync.RWMutex
	items	map[string]Item
}

Item

This type doesn't have documentation.

Field name Field type Comment
Object

any

No comment on field.
Expiration

int64

No comment on field.
type Item struct {
	Object		any
	Expiration	int64
}

Janitor

Janitor is responsible for performing periodic cleanup operations.

Field name Field type Comment
Interval

time.Duration

No comment on field.
stop

chan bool

No comment on field.
type Janitor struct {
	Interval	time.Duration
	stop		chan bool
}

Repository

Repository interface is the API signature for an object cache.

Field name Field type Comment
type

any

No comment on field.
type Repository interface {
	Get(string) (interface{}, bool)
	Set(string, interface{}, int64)
	Delete(string)
	Count() int
	Flush()

	Close()
}

repository

This type doesn't have documentation.

Field name Field type Comment
cache

*Cache

The underlying cache driver.

defaultExpiration

int64

Default expiration interval, in seconds.

cleanupInterval

int64

Clean up interval, in seconds.

type repository struct {
	// The underlying cache driver.
	cache	*Cache

	// Default expiration interval, in seconds.
	defaultExpiration	int64

	// Clean up interval, in seconds.
	cleanupInterval	int64
}

Functions

func New

New creates a new cache instance.

func New(defaultExpiration, cleanupInterval int64) Repository {
	var (
		defaultExpirationDuration	= time.Duration(defaultExpiration) * time.Second
		cleanupIntervalDuration		= time.Duration(cleanupInterval) * time.Second
	)

	return &repository{
		defaultExpiration:	defaultExpiration,
		cleanupInterval:	cleanupInterval,
		cache:			NewCache(defaultExpirationDuration, cleanupIntervalDuration),
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: time.Duration, time.Second.

func NewCache

NewCache creates a new *Cache for storing items with a TTL.

func NewCache(expiration, cleanupInterval time.Duration) *Cache {
	if expiration == 0 {
		expiration = -1
	}

	cache := &Cache{
		items:		make(map[string]Item),
		expiration:	expiration,
	}

	if cleanupInterval > 0 {
		// Cache with a cleanup janitor.
		cache.janitor = NewJanitor(cleanupInterval, cache.Cleanup)
	}

	return cache
}

Cognitive complexity: 5, Cyclomatic complexity: 3

func NewJanitor

NewJanitor returns a new Janitor that performs cleanup at the specified interval.

func NewJanitor(interval time.Duration, cleanup func()) *Janitor {
	janitor := &Janitor{
		Interval:	interval,
		stop:		make(chan bool, 1),
	}

	go janitor.Run(cleanup)

	return janitor
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func (*Cache) Cleanup

Cleanup will delete all expired items from the cache map.

func (c *Cache) Cleanup() {
	now := time.Now().UnixNano()

	c.mu.Lock()
	for k, v := range c.items {
		if v.Expiration > 0 && now > v.Expiration {
			delete(c.items, k)
		}
	}
	c.mu.Unlock()
}

Cognitive complexity: 5, Cyclomatic complexity: 4

Uses: time.Now.

func (*Cache) Close

Close implements an io.Closer; Invoke it to cancel the cleanup goroutine.

func (c *Cache) Close() {
	if c.janitor != nil {
		c.janitor.Close()
		c.janitor = nil
	}
	c.Flush()
}

Cognitive complexity: 2, Cyclomatic complexity: 2

func (*Cache) Count

Count returns the number of items in cache, including expired items. Expired items get cleaned up by the janitor periodically.

func (c *Cache) Count() int {
	c.mu.RLock()
	n := len(c.items)
	c.mu.RUnlock()

	return n
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Cache) Delete

Delete an item from the cache. Does nothing if the key is not in the cache.

func (c *Cache) Delete(k string) {
	c.mu.Lock()
	delete(c.items, k)
	c.mu.Unlock()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Cache) Flush

Flush deletes all items from the cache.

func (c *Cache) Flush() {
	c.mu.Lock()
	c.items = map[string]Item{}
	c.mu.Unlock()
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func (*Cache) Get

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (c *Cache) Get(k string) (any, bool) {
	c.mu.RLock()

	item, found := c.items[k]
	if !found {
		c.mu.RUnlock()
		return nil, false
	}

	if item.Expiration > 0 {
		if time.Now().UnixNano() > item.Expiration {
			c.mu.RUnlock()
			return nil, false
		}
	}

	c.mu.RUnlock()
	return item.Object, true
}

Cognitive complexity: 6, Cyclomatic complexity: 4

Uses: time.Now.

func (*Cache) Items

Items copies all unexpired items in the cache into a new map and returns it.

func (c *Cache) Items() map[string]Item {
	c.mu.RLock()
	defer c.mu.RUnlock()

	m := make(map[string]Item, len(c.items))

	now := time.Now().UnixNano()
	for k, v := range c.items {
		if v.Expiration > 0 && now > v.Expiration {
			continue
		}
		m[k] = v
	}

	return m
}

Cognitive complexity: 5, Cyclomatic complexity: 4

Uses: time.Now.

func (*Cache) Set

Add an item to the cache, replacing any existing item. If the duration is 0, the cache's expiration time is used. If it is -1, the item never expires.

func (c *Cache) Set(k string, x any, d time.Duration) {
	var e int64
	if d == 0 {
		d = c.expiration
	}
	if d > 0 {
		e = time.Now().Add(d).UnixNano()
	}
	c.mu.Lock()
	c.items[k] = Item{
		Object:		x,
		Expiration:	e,
	}
	c.mu.Unlock()
}

Cognitive complexity: 5, Cyclomatic complexity: 3

Uses: time.Now.

func (*Janitor) Close

Close stops the janitor from performing further cleanup operations.

func (j *Janitor) Close() {
	j.stop <- true
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Janitor) Run

Run starts the janitor which calls the provided cleanup function at every interval.

func (j *Janitor) Run(cleanup func()) {
	ticker := time.NewTicker(j.Interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			cleanup()
		case <-j.stop:
			close(j.stop)
			return
		}
	}
}

Cognitive complexity: 5, Cyclomatic complexity: 4

Uses: time.NewTicker.

func (*repository) Close

Close will stop background cleanup and clear the cache for garbage collection. Close also flushes the data in the cache.

func (r *repository) Close() {
	r.cache.Close()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*repository) Count

Count returns number of items in the cache.

func (r *repository) Count() int {
	return r.cache.Count()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*repository) Delete

Delete cache item by key.

func (r *repository) Delete(key string) {
	r.cache.Delete(key)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*repository) Flush

Flush flushes all the items from the cache.

func (r *repository) Flush() {
	r.cache.Flush()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*repository) Get

Get retrieves a cache item by key.

func (r *repository) Get(key string) (interface{}, bool) {
	return r.cache.Get(key)
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func (*repository) Set

Set writes a cache item with a timeout in seconds. If timeout is zero, the default expiration for the repository instance will be used.

func (r *repository) Set(key string, value interface{}, timeout int64) {
	if timeout <= 0 {
		timeout = r.defaultExpiration
	}
	r.cache.Set(key, value, time.Duration(timeout)*time.Second)
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Uses: time.Duration, time.Second.

func (Item) Expired

Returns true if the item has expired.

func (item Item) Expired() bool {
	if item.Expiration <= 0 {
		return false
	}
	return time.Now().UnixNano() > item.Expiration
}

Cognitive complexity: 2, Cyclomatic complexity: 2

Uses: time.Now.

Tests

Files: 3. Third party imports: 1. Imports from organisation: 0. Tests: 4. Benchmarks: 0.

Test functions

TestCache

References: assert.NotNil.

TestCache_Expired

References: assert.Equal, assert.False, testing.T.

TestItem_Expired

References: assert.Equal, testing.T, time.Minute, time.Now.

TestJanitor

References: assert.NotEqual, atomic.AddInt32, atomic.LoadInt32, time.Millisecond, time.Sleep.