cache

package
v12.20.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides simple in-memory key:value store

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is cache store

func New

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

New creates new store instance

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

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

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

ABCD

func (*Store) Delete

func (s *Store) Delete(key string)

Delete removes item from cache

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

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

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

<nil>

func (*Store) Flush

func (s *Store) Flush()

Flush removes all data from cache

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

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

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

<nil>

func (*Store) Get

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

Get returns item from cache or nil

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

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

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

ABCD

func (*Store) GetWithExpiration

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

GetWithExpiration returns item from cache and expiration date or nil

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

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

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

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

func (*Store) Has

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

Has returns true if store contains data for given key

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

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

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

true

func (*Store) Set

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

Set adds or updates item in store

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

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

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

ABCD

Jump to

Keyboard shortcuts

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