cache

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 3 Imported by: 0

README

cache

Godoc Report Tests Coverage Sponsor

Cache arbitrary data with an expiration time.

Features

  • 0 dependencies
  • About 100 lines of code
  • 100% test coverage

Usage

// New cache
c := cache.New(5 * time.Minute)

// Put something into the cache
c.Set("a", "b", 1 * time.Minute)

// Read from the cache
obj, found := c.Get("a")

// Convert the type
fmt.Println(obj.(string))

Benchmarks

BenchmarkGet-12         300000000                3.88 ns/op            0 B/op          0 allocs/op
BenchmarkSet-12         10000000               183 ns/op              48 B/op          2 allocs/op
BenchmarkNew-12         10000000               112 ns/op             352 B/op          5 allocs/op

Style

Please take a look at the style guidelines if you'd like to make a pull request.

Sponsors

Cedric Fung Scott Rayapoullé Eduard Urbach
Cedric Fung Scott Rayapoullé Eduard Urbach

Want to see your own name here?

Documentation

Index

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 stores arbitrary data with expiration time.

func New

func New(cleaningInterval time.Duration) *Cache

New creates a new cache that asynchronously cleans expired entries after the given time passes.

func (*Cache) Close

func (cache *Cache) Close()

Close closes the cache and frees up resources.

func (*Cache) Delete

func (cache *Cache) Delete(key interface{})

Delete deletes the key and its value from the cache.

func (*Cache) Get

func (cache *Cache) Get(key interface{}) (interface{}, bool)

Get gets the value for the given key.

func (*Cache) Hits

func (cache *Cache) Hits() int64

Hits returns the number of cache hits.

func (*Cache) Misses

func (cache *Cache) Misses() int64

Misses returns the number of times a key was not found in the cache.

func (*Cache) Range

func (cache *Cache) Range(f func(key, value interface{}) bool)

Range calls f sequentially for each key and value present in the cache. If f returns false, range stops the iteration.

func (*Cache) Set

func (cache *Cache) Set(key interface{}, value interface{}, duration time.Duration)

Set sets a value for the given key with an expiration duration. If the duration is 0 or less, it will be stored forever.

Jump to

Keyboard shortcuts

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