lfu

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2017 License: MIT Imports: 4 Imported by: 0

README

LFU Cache

Latest Version Build Status Go Documentation Coverage Status Go Report Card

LFU cache in Go offering O(1) Get and Insert outlined here.

Install

go get -u github.com/mtchavez/lfu

Usage

package main

import (
    "fmt"
    "github.com/mtchavez/lfu"
)

func main() {
    cache := lfu.NewLFU()

    // Insert
    cache.Insert(42, []byte("user:42:user@example.com"))

    // Insert existing key
    success, err := cache.Insert(42, "Nope")
    if !success {
        fmt.Println("Error inserting:", err)
    }

    var data interface{}
    var e error
    // Get
    data, e = cache.Get(42)
    fmt.Println("Data for 42 is", data)

    // Get not found
    data, e = cache.Get(987654321)
    if e != nil {
        fmt.Println("Error on get:", e)
    }
}

Tests

go test --cover

Benhcmarks

Get and insert methods are benchmarked. Results from OS X with a 2.3 GHz Intel Core i7 on go version go1.8.3 darwin/amd64

# Updated: 2017-08-15

BenchmarkInsert-8                1000000              1860 ns/op
BenchmarkParallelInsert-8        1000000              1861 ns/op
BenchmarkGet_EmptyCache-8        5000000               362 ns/op
BenchmarkGet_AllMisses-8         3000000               732 ns/op
BenchmarkGet_AllHits-8           1000000              1417 ns/op
BenchmarkParallelGet-8           2000000              1405 ns/op

TODO

  • Some kind of eviction

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache used to call core cache methods off of

func NewLFU

func NewLFU() *Cache

NewLFU initializes a new LFU cache and returns it

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, error)

Get takes a key for an item in the cache to look up Returns the data associated with that key and an

Example
cache := NewLFU()
cache.Insert(1, []byte("user:1:aeb31da42fae09afqld"))

// Get key 1
data, _ := cache.Get(1)
fmt.Println("Cached data for key 1:", string(reflect.ValueOf(data).Bytes()))

// Get a not cached key
_, err := cache.Get(42)
fmt.Println("Failed get:", err)
Output:

Cached data for key 1: user:1:aeb31da42fae09afqld
Failed get: Key: 42 not found in cache

func (*Cache) GetLFUItem

func (c *Cache) GetLFUItem() (value interface{}, data interface{})

GetLFUItem returns the key and data of the least frequently updated item in the cache or -1 and nil for not found

func (*Cache) Insert

func (c *Cache) Insert(key interface{}, value interface{}) (bool, error)

Insert takes a key and a value to insert into the cache Returns a boolean for successful insert and an error if failed

Example
cache := NewLFU()
inserted, _ := cache.Insert(1, []byte("user:1:aeb31da42fae09afqld"))
fmt.Println("Inserted:", inserted)

// Insert existing key
_, err := cache.Insert(1, "boom")
fmt.Println("Insert error:", err)
Output:

Inserted: true
Insert error: Key already exists in cache

Jump to

Keyboard shortcuts

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