lrucache

package module
v0.0.0-...-1d3f338 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: MIT Imports: 3 Imported by: 0

README

🗃️ lru-cache

Build Status Go Report Card codecov license

logo

Go simple LRU in memory cache

A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn't been used for the longest amount of time.

  • Strengths:
    • Super fast accesses. LRU caches store items in order from most-recently used to least-recently used. That means both can be accessed in O(1)O(1) time.
    • Super fast updates. Each time an item is accessed, updating the cache takes O(1) time.

📖 ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

📚 Documentation

For examples visit godoc#pkg-examples

For GoDoc reference, visit pkg.go.dev

🚏 HOW TO USE

🚅 Benchmark

CPU: 3,3 GHz Intel Core i7

RAM: 16 GB 2133 MHz LPDDR3

➜  lru-cache git:(master) go test -bench=. -cpu=4 -benchmem
goos: darwin
goarch: amd64
pkg: github.com/vardius/lru-cache
BenchmarkRead-4         23051137                50.1 ns/op             0 B/op          0 allocs/op
BenchmarkWrite-4        23097510                51.8 ns/op             0 B/op          0 allocs/op
PASS
ok  	github.com/vardius/lru-cache	2.713s

🏫 Basic example

package main

import (
	"fmt"
	"log"

    lrucache "github.com/vardius/lru-cache"
)

func main() {
	c, err := lrucache.New("example-cache", 10*lrucache.MB)
	if err != nil {
		log.Fatal(err)
		return
	}

	item, err := c.Get("test")
	if err != nil {
		log.Fatal(err)
		return
	}

	if item == nil {
		if err = c.Set("test", []byte("value")); err != nil {
			log.Fatal(err)
			return
		}
	}

	got, err := c.Get("test")
	if err != nil {
		log.Fatal(err)
		return
	}

	fmt.Println(string(got))

	// Output:
	// value
}

📜 License

This package is released under the MIT license. See the complete license in the package

Documentation

Overview

Package lrucache provides simple in memory cache system wit limit

Example
package main

import (
	"fmt"
	"log"

	lrucache "github.com/vardius/lru-cache"
)

func main() {
	c, err := lrucache.New("example-cache", 10*lrucache.MB)
	if err != nil {
		log.Fatal(err)
		return
	}

	item, err := c.Get("test")
	if err != nil {
		log.Fatal(err)
		return
	}

	if item == nil {
		if err = c.Set("test", []byte("value")); err != nil {
			log.Fatal(err)
			return
		}
	}

	got, err := c.Get("test")
	if err != nil {
		log.Fatal(err)
		return
	}

	fmt.Println(string(got))

}
Output:

value

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteSize

type ByteSize float64
const (
	KB ByteSize = 1 << (10 * iota)
	MB
	GB
	TB
	PB
	EB
	ZB
	YB
)

type Cache

type Cache interface {
	// Sets value to cache
	Set(key string, value []byte) error
	// Gets value from cache
	Get(key string) ([]byte, error)
}

Cache provides set and get functionality

func New

func New(name string, maxSize ByteSize) (Cache, error)

New creates new cache

Jump to

Keyboard shortcuts

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