README

Simple, non-thread safe LRU cache implementation.

Usage

package main

import (
	"strconv"

	"github.com/alyakimenko/lcache"
)

const cacheSize = 5

func main() {
	cache, _ := lcache.NewLRUCache(cacheSize)
	
	// cache: {0, 1, 2, 3, 4}
	for i := 0; i < 5; i++ {
		cache.Set(strconv.Itoa(i), i)
	}
	
	// cache: {0, 1, 2, 4, 3}
	value, ok := cache.Get("3")
	if !ok {
		// not presented
	}
	
	// cache: {1, 2, 4, 3, 5}
	cache.Set("5")
	
	// cache: {1, 4, 3, 5}
	removed := cache.Remove("2")
	
	// cache: {}
	cache.Clear()
}

Documentation

Overview

Package lcache represents simple, non-thread safe LRU cache implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set sets a cache element value,
	// if it's not presented - inserts new element.
	Set(key, value interface{})

	// Get returns a key's value from the cache.
	Get(key interface{}) (interface{}, bool)

	// Remove removes element from the cache.
	Remove(key interface{}) bool

	// Clears all cache elements.
	Clear()

	// Size returns number of the cache elements.
	Size() int
}

Cache represents basic cache interface.

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache implements a fixed size LRU cache.

func NewLRUCache

func NewLRUCache(size int) (*LRUCache, error)

NewLRUCache returns new LRUCache.

func (*LRUCache) Clear

func (lru *LRUCache) Clear()

Clear completely clears the cache.

func (*LRUCache) Get

func (lru *LRUCache) Get(key interface{}) (interface{}, bool)

Get returns a key's value from the cache.

func (*LRUCache) Remove

func (lru *LRUCache) Remove(key interface{}) bool

Remove removes element from the cache by key. Returns true if the element was removed, false otherwise.

func (*LRUCache) Set

func (lru *LRUCache) Set(key, value interface{})

Set sets a value to the cache.

func (*LRUCache) Size

func (lru *LRUCache) Size() int

Size returns number of the cache elements.