Skip to content

ErfanMomeniii/lru

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go version license version

lru

lru is a lightweight package that implements lru cache algorithm in Go.

Documentation

Install

go get github.com/erfanmomeniii/lru

Next, include it in your application:

import "github.com/erfanmomeniii/lru"

Quick Start

The following example illustrates how to use this package

package main

import (
	"fmt"
	"github.com/erfanmomeniii/lru"
)

func main() {
	l := lru.New(2)
	// 2 is the size of lru (default 2000)
	
	l.Set("a", 12)
	l.Set("b", 13)
	
	fmt.Println(l.Get("a"))
	// 12
	
	l.Set("c", "hi")
	
	fmt.Println(l.Get("c"))
	// hi
	
	fmt.Println(l.Get("a"))
	// nil
}

Usage

type Lru

type Lru struct {
q    *queue.Queue
m    map[any]any
size int64
}

Lru is an instantiation of the lru.

func New

func New(size ...int64) *Lru

New creates a new instance of a lru.

func Set

func (l *Lru) Set(key any, value any)

Set adds or updates with inputted key and value.

func Get

func (l *Lru) Get(key any) any

Get returns the value associated with the inputted key.

func Has

func (l *Lru) Has(key any) bool 

Has checks whether the key exists or not.

Contributing

Pull requests are welcome. For changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.