GithubHelp home page GithubHelp logo

timerstore's Introduction

timerstore

A key-value storage with ttl

Timerstore's feature:

  • set a ttl for the key-val store
  • set a callback function which would be called when ttl is expired

Timerstore has two types of implementation: memory and redis

Example for memory:

  // create a memory provider
  m := NewMemProvider() 
  
  // register the memory provider
  RegisterProvider("mem", m)  
  
  // create a timer store with cycle interval is 1 second
  store, _ := NewTimerStore("Test", "mem", 1*time.Second, func(key string, val string) {
    fmt.Printf("key: %s, value: %v expired\n", key, string(val))
    // put your callback code here
  })
  
  // Set a pair of key-value
  store.Set("hello", ("world"), 5) // this would be expired in 5 seconds
  
  // Get the value of the key
  data, ok, err := store.Get("hello")
  
  fmt.Printf("data: %v, ok: %v, err: %v\n", data, ok, err")

Example for redis:

// redis configuration
config := &Config{
		Host:        "",
		Port:        "",
		Password:    "",
		Type:        "cluster",
		PoolSize:    10,
		PoolTimeout: 10,
	}

  // create a redis provider
  r, _ := NewRedisProvider(config)

  // register the redis provider
  RegisterProvider("redis", r)
  
  // create a timer store with cycle interval is 1 second
  store, _ := NewTimerStore("Test", "redis", 1*time.Second, func(key string, val string) {
    fmt.Printf("Call back--->> now: %v,  key: %s, val: %v\n", time.Now().Unix(), key, val)
    // put your callback code here
  })
  
  // Set a pair of key-value
  store.Set("hello", ("world"), 5) // this would be expired in 5 seconds
  
  // Get the value of the key
  data, ok, err := store.Get("hello")
  
  fmt.Printf("data: %v, ok: %v, err: %v\n", data, ok, err")

Error handling is ignored in the examples.

timerstore's People

Contributors

thinkerchi avatar

Stargazers

 avatar

Watchers

 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.