cache

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cache simplifies caching with GC

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Expiration time.Duration
	// contains filtered or unexported fields
}

Cache is a storage for all Item items

Example
package main

import (
	"fmt"
	"time"

	"simonwaldherr.de/go/golibs/cache"
)

func main() {
	c := cache.New(5*time.Second, 1*time.Second)

	c.Add("0", 23)
	c.Add("1", 1*time.Second)
	c.Add("2", "foobar")
	c.Add("3", false)
	c.Add("4", []byte("test"))

	fmt.Println(c.String())

}
Output:

0	23
1	1s
2	foobar
3	false
4	[116 101 115 116]

func New

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

New creates a new Cache

func New2

func New2(expirationTime, cleanupInterval time.Duration, fn func(key string, value interface{})) *Cache

New2 creates a new Cache with a cleaner function

func (*Cache) Add

func (cache *Cache) Add(key string, value interface{}) bool

Add creates an cached item

func (*Cache) Clear

func (cache *Cache) Clear()

Clear removes all items in the cache

func (*Cache) Delete

func (cache *Cache) Delete(key string)

Delete deletes a cached item

func (*Cache) DeleteAllWithFunc

func (cache *Cache) DeleteAllWithFunc(fn func(key string, value interface{}))

DeleteAllWithFunc does the same like DeleteExpiredWithFunc but not just for the expired items, also the non expired

func (*Cache) DeleteExpired

func (cache *Cache) DeleteExpired()

DeleteExpired checks all cache items and deletes the expired items

func (*Cache) DeleteExpiredWithFunc

func (cache *Cache) DeleteExpiredWithFunc(fn func(key string, value interface{}))

DeleteExpiredWithFunc does the same like DeleteExpired but also calls a function for each deleted item

func (*Cache) Export added in v0.10.2

func (cache *Cache) Export(w io.Writer) error

Export all items to a gob buffer

func (*Cache) Get

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

Get returns the value of a cached item or nil if expired

func (*Cache) Import added in v0.10.2

func (cache *Cache) Import(r io.Reader) error

Import all items from a gob buffer

func (*Cache) Set

func (cache *Cache) Set(key string, value interface{})

Set creates an Item in the cache, if there is already an item with that name it get overwritten

func (*Cache) SetWithDuration

func (cache *Cache) SetWithDuration(key string, value interface{}, creation time.Time, duration time.Duration)

SetWithDuration does the same as Set but with an specific expiration date

func (*Cache) Size

func (cache *Cache) Size() int

Size returns the number of cached items it does not check for expired items, so run DeleteExpired before

func (*Cache) String

func (cache *Cache) String() string

String returns all cached values as string (for debugging)

func (*Cache) Time

func (cache *Cache) Time(key string) time.Time

Time returns the creation date of a cached item

func (*Cache) Update

func (cache *Cache) Update(key string, value interface{}) bool

Update changes the value of an key. If the key doesn't exist, it returns false

type Item

type Item struct {
	Object     interface{}
	Creation   time.Time
	Expiration time.Time
}

Item as a single object with an interface as value and two time.Time values for creation and expiration dates

Jump to

Keyboard shortcuts

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