package
0.0.0-20220819124347-62f5189ff8cd
Repository: https://github.com/monaco-io/lib.git
Documentation: pkg.go.dev

# README

lib/lru

thread safe LRU cache.

example

package main

import (
	"log"

	"github.com/monaco-io/lib/lru"
)

var instance = lru.New[int, int](100, time.Second)

func main() {
	i := 1
	//set key value pairs
	instance.Set(i, i)

	//get val from mem
	v, ok := instance.Get(i)
	log.Println(i, v, ok)

}

example with callback


import (
	"context"
	"log"

	"github.com/monaco-io/lib/lru"
)

var instance = lru.NewC(100, time.Second, func(context.Context, int) (int, error) {
	return 10086, nil
})

func main() {
	i := 1
	//set key value pairs
	instance.Set(i, i)

	//get val from mem
	v, ok := instance.GetC(context.Background(), i)
	log.Println(i, v, ok)
}

example concurrent

package main

import (
	"log"
	"math"
	"time"

	"github.com/monaco-io/lib/lru"
	"golang.org/x/sync/errgroup"
)

var instance = lru.New[int, int](100, time.Second*5)

func main() {
	c1 := time.After(time.Second * 3)
	c2 := time.After(time.Second * 10)
	f1 := func() error {
		for {
			select {
			case <-c1:
				return nil
			default:
				for i := 0; i <= math.MaxInt8; i++ {
					instance.Set(i, i)
				}
			}
		}
	}
	f2 := func() error {
		for {
			select {
			case <-c2:
				return nil
			default:
				for i := 0; i <= math.MaxInt8; i++ {
					v, ok := instance.Get(i)
					log.Println(i, v, ok)
				}
			}
		}
	}
	var eg errgroup.Group
	eg.Go(f1)
	eg.Go(f2)
	eg.Wait()
}


# Functions

New creates a new Cache.
No description provided by the author

# Structs

Cache is an LRU cache.
No description provided by the author

# Interfaces

ICache is the interface for thread safe LRU cache.
No description provided by the author