Giter Site home page Giter Site logo

go-cache's Introduction

go-cache

This repository contains caching related go packages

FetcherLRU

In memory LRU cache that satifies an specific use case where the cache is filled by demand by an OnFetch func:

type FetchFunc func() (value interface{}, err error)

The getter method for this cache requires a key and a FetchFunc that will be executed when the key is not found or has expired, the OnFetch result will be stored in cache for that key.

  • Its concurrent safe
  • Gets on expired keys cause an update but are non-blocking, the old value is returned
  • Gets on missing keys are blocked until the OnFetch finishes
  • Ensures that only one OnFetch func is executed per key at the same time
  • Empty items are cached (nil values returned by a FetchFunc)
  • Easy to implement :)

Example of use where a service is wrapped into a cache layer using FetcherLRU:

    type Service interface {
        ExpensiveCall(k string) (v interface{}, err error)
    }

    type CachedService struct {
        c *FetcherLru
        s Service
    }

    var _ Service = &CachedService{}

    func (s *CachedService) ExpensiveCall(k string) (interface{}, error) {
        onFetch := func() (interface{}, error) {
            return s.s.ExpensiveCall(k)
        }
        
        return s.c.GetOrFetch(k, onFetch)
    }

go-cache's People

Contributors

arquio avatar pperaltaisern avatar

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.