lrucache

package
v0.0.0-...-50b27ac Latest Latest
Warning

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

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

README

lrucache

В этой задаче нужно написать простой Least recently used cache.

LRU cache - это key-value storage фиксированного размера, реализующий операции:

  • set(k, v) - обновляет хранимое по ключу k значение. В случае, если операция приводит к превышению размера кэша, из того удаляется значение по самому "старому" ключу.
  • get(k) -> v, ok - возвращает значение, хранимое по ключу k.

Обе функции set и get обновляют access time ключа.

В файле cache.go задан интерфейс Cache с подробным описанием всех методов.

Нужно написать реализацию и конструктор, принимающий размер кэша:

func New(cap int) Cache

Ссылки

  1. container/list: https://golang.org/pkg/container/list/
  2. wiki: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get returns value associated with the key.
	//
	// The second value is a bool that is true if the key exists in the cache,
	// and false if not.
	Get(key int) (int, bool)
	// Set updates value associated with the key.
	//
	// If there is no key in the cache new (key, value) pair is created.
	Set(key, value int)
	// Range calls function f on all elements of the cache
	// in increasing access time order.
	//
	// Stops earlier if f returns false.
	Range(f func(key, value int) bool)
	// Clear removes all keys and values from the cache.
	Clear()
}

func New

func New(cap int) Cache

Jump to

Keyboard shortcuts

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