GithubHelp home page GithubHelp logo

ndolestudio / lfu-cache Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 1.0 66 KB

Strongly typed least frequently used (LFU) cache in Go with constant time complexity O(1) on all operations

License: MIT License

Go 100.00%
go cache lfu-cache lfu-implementation algorithm

lfu-cache's Introduction

Lest Frequently Used (LFU) Cache

Build Status codecov Go Report Card GitHub contributors GitHub license PkgGoDev

This is an in memory implementation of a least frequently used (LFU) cache in Go with constant time complexity O(1) for Set, Set, and Cache Eviction operations. The least recently used item is evicted in the case where 2 items thems have the same least frequency.

It's based on this paper http://dhruvbird.com/lfu.pdf by some very smart people

Documentation

You can use view the standard documentation on https://pkg.go.dev/github.com/NdoleStudio/lfu-cache. I wrote a beginner friendly blog post here

Install

go get https://github.com/NdoleStudio/lfu-cache/v2

Usage

  • To get started, import the lfu-cache package and create a cache instance. New() returns an ErrInvalidCap error if you input a capacity which is less than or equal to 0.

    import "https://github.com/NdoleStudio/lfu-cache/v2"
    
    // creating the cache with capacity 3
    cache, err := lfucache.New[string, int](3)
    if err != nil {
        // the cap is invalid
    }
    
    // DO NOT DO THIS
    cache := lfucache.Cache{}
  • Inserting a value in the cache. Set() returns ErrInvalidCap if the cache capacity is less than or equal to zero. Ideally you should NEVER get this error

    err := cache.Set("key", "value")
    if err != nil {
        // the cap is invalid
    }
  • Getting a value in from the cache. Get() returns ErrCacheMiss if there is a cache miss

    val, err := cache.Get("key")
    if err != nil {
        // cache miss
    }
    
    or 
    
    if err == lfucache.ErrCacheMiss { 
        // cache miss
    }
    
    println(val) // val is of type `int`
  • There are some helper methods like IsEmpty(), Len(), IsFull Cap()

    // creating the cache with capacity 3
    cache, _ := lfucache.New[string, string](3)
    
    // setting a value
    _ = cache.Set("key", "value")
    
    cache.IsEmpty() // returns false
    cache.Len()     // returns 1 because there is 1 item in the cache
    cache.IsFull()  // returns false because the cache is not full
    cache.Cap()     // returns 3 which is the capacity of the cache

Running Tests

To run tests, cd to the project directory and run

go test -v 

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

lfu-cache's People

Contributors

achoarnold avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

narasimha1997

lfu-cache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.