Git Product home page Git Product logo

ttlswisscache's Introduction

GoDoc License

ttlcache

About

ttlcache โ€“ is a simple and efficient in-memory key value storage with TTL for each record.

The key is of uint64 type. Library provides wrappers for common types:

  • IntKey
  • ByteKey
  • Int8Key
  • Uint8Key
  • Int16Key
  • Uint16Key
  • Int32Key
  • Uint32Key
  • Int64Key
  • Uint64Key
  • BytesKey ([]byte)
  • StringKey
  • AnyKey (interface{})

AnyKey is not suitable for very intensive usage. Consider writing your own hash function if your keys are complex types, and you faced performance degradation.

Installation

go get -u github.com/loicalleyne/ttlswisscache

Usage

package main

import (
    "github.com/loicalleyne/ttlswisscache"
)

const (
    minute = 60 * time.Second
    hour = 60 * minute
)

func main() {
    // How often we need to stop the world and remove outdated records.
    resolution := minute

    cache := ttlcache.New(resolution)
    cache.Set(ttlcache.StringKey("some key"), "value", hour)
    value, ok := cache.Get(ttlcache.StringKey("some key"))
    if !ok {
        // there is no record with key "some key" in the cache. Probably it has been expired.
    }

    fmt.Println(value.(string)) // This is necessary type assertion, because returned value is of interface{} type.
}

Performance

If you're interested in benchmarks you can check them in repository. Just play with numbers and types and check that library is suitable for your purposes.

go test -bench=. -benchmem

For those of us who wants to get some numbers without downloading unknown stuff (MacBook Pro 16"):

BenchmarkCache_Set_100-16           8959221               125 ns/op
BenchmarkCache_Set_1000-16          9177854               123 ns/op
BenchmarkCache_Set_10000-16         9247304               131 ns/op
BenchmarkCache_Get_100-16           50562800              23.9 ns/op
BenchmarkCache_Get_1000-16          47270793              26.9 ns/op
BenchmarkCache_Get_10000-16         42578484              27.7 ns/op

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.