cache

package
v12.58.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Package cache provides a simple in-memory key:value cache

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is cache instance

func New

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

New creates new cache instance

Example
cache := New(time.Second, time.Minute)

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

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

ABCD

func (*Cache) Delete

func (s *Cache) Delete(key string)

Delete removes item from cache

Example
cache := New(time.Second, time.Minute)

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

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

<nil>

func (*Cache) Expired

func (s *Cache) Expired() int

Expired returns number of exipred items in cache

Example
cache := New(time.Second, time.Minute)

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

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

0

func (*Cache) Flush

func (s *Cache) Flush()

Flush removes all data from cache

Example
cache := New(time.Second, time.Minute)

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

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

<nil>

func (*Cache) Get

func (s *Cache) Get(key string) interface{}

Get returns item from cache or nil

Example
cache := New(time.Second, time.Minute)

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

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

ABCD

func (*Cache) GetWithExpiration

func (s *Cache) GetWithExpiration(key string) (interface{}, time.Time)

GetWithExpiration returns item from cache and expiration date or nil

Example
cache := New(time.Second, time.Minute)

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

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

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

func (*Cache) Has

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

Has returns true if cache contains data for given key

Example
cache := New(time.Second, time.Minute)

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

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

true

func (*Cache) Set

func (s *Cache) Set(key string, data interface{})

Set adds or updates item in cache

Example
cache := New(time.Second, time.Minute)

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

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

ABCD

func (*Cache) Size

func (s *Cache) Size() int

Size returns number of items in cache

Example
cache := New(time.Second, time.Minute)

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

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

1

Jump to

Keyboard shortcuts

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