Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rmsj/cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Least Recently Used Cache Exercise

This is a simple, in memory implementation of a Least Recently Used Cache.

Assumptions and Limitations

  • For the purpose of this exercise the implementation has been simplified to integer values only.
  • The cache values are kept in memory

API

Put

Accepts a key and a value to be stored on the cache. If cache is about to go over capacity, the least recently used entry is removed and the new entry is added.

Get

Returns the value of the specified given index or -1 if not found.

Basic Usage Example

package main

import (
	"fmt"
	"github.com/rmsj/cache"
)

func main() {

	c, err := cache.NewLRUCache(10)
	if err != nil {
		panic(err)
	}
	
	c.Put(1, 20)

	// print random first name
	fmt.Println(c.Get(1)) // should output 20

	fmt.Println(c.Get(2)) // should output -1
}

About

Least Recently Used Cache Exercise

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages