memory

package
v13.15.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package memory provides cache with in-memory storage

Index

Examples

Constants

View Source
const MIN_CLEANUP_INTERVAL = cache.MILLISECOND

MIN_CLEANUP_INTERVAL is minimal cleanup interval

View Source
const MIN_EXPIRATION = cache.MILLISECOND

MIN_EXPIRATION is minimal expiration duration

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is in-memory cache instance

func New

func New(config Config) (*Cache, error)

New creates new cache instance

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

fmt.Println(c.Get("test"))
Output:

ABCD

func (*Cache) Delete

func (c *Cache) Delete(key string) bool

Delete removes item from cache

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")
c.Delete("test")

fmt.Println(c.Get("test"))
Output:

<nil>

func (*Cache) Expired

func (c *Cache) Expired() int

Expired returns number of expired items in cache

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

fmt.Println(c.Expired())
Output:

0

func (*Cache) Flush

func (c *Cache) Flush() bool

Flush removes all data from cache

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")
c.Flush()

fmt.Println(c.Get("test"))
Output:

<nil>

func (*Cache) Get

func (c *Cache) Get(key string) any

Get returns item from cache or nil

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

fmt.Println(c.Get("test"))
Output:

ABCD

func (*Cache) GetExpiration

func (c *Cache) GetExpiration(key string) time.Time

GetWithExpiration returns item expiration date

func (*Cache) GetWithExpiration

func (c *Cache) GetWithExpiration(key string) (any, time.Time)

GetWithExpiration returns item from cache and expiration date or nil

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

item, exp := c.GetWithExpiration("test")

fmt.Println(item, exp.String())
Output:

func (*Cache) Has

func (c *Cache) Has(key string) bool

Has returns true if cache contains data for given key

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

fmt.Println(c.Has("test"))
Output:

true

func (*Cache) Set

func (c *Cache) Set(key string, data any, expiration ...time.Duration) bool

Set adds or updates item in cache

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")
c.Set("test", "ABCD", 15*cache.MINUTE)

fmt.Println(c.Get("test"))
Output:

ABCD

func (*Cache) Size

func (c *Cache) Size() int

Size returns number of items in cache

Example
c, _ := New(Config{
	DefaultExpiration: cache.SECOND,
	CleanupInterval:   cache.MINUTE,
})

c.Set("test", "ABCD")

fmt.Println(c.Size())
Output:

1

type Config

type Config struct {
	DefaultExpiration cache.Duration
	CleanupInterval   cache.Duration
}

Config is cache configuration

func (Config) Validate

func (c Config) Validate() error

Validate validates cache configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL