Giter Site home page Giter Site logo

stat_buffer's Introduction

StatBuffer

Build Status StatBuffer Version

StatBuffer is an efficient way to maintain a local incrementable count with a given key that can later be flushed to persistent storage. In fast moving systems, this provides a scalable way keep track of counts without putting heavy loads on a database.

Installation

The package can be installed by adding stat_buffer to your list of dependencies in mix.exs:

def deps do
  [
    {:stat_buffer, "~> 1.2"}
  ]
end

Documentation

Please see HexDocs for additional documentation. This readme provides a brief overview, but it is recommended that the docs are used.

Creating a Buffer

We can start off by creating our buffer. This is simply a module that uses StatBuffer and implements the handle_flush/2 callback.

defmodule Buffer do
  use StatBuffer

  def handle_flush(key, counter) do
    # do database stuff...

    # we must return an :ok atom
    :ok
  end
end

We then must add the buffer to our supervision tree.

children = [
  Buffer
]

There are some configruable options available for our buffers. You can read more about them here. These options can be passed when creating our buffer.

  use StatBuffer, interval: 10_000

With our buffer started, we can now increment key counters. A key can be any valid term.

Buffer.increment("mykey") # increments by 1

Buffer.increment("mykey", 10) # increments by 10

And we're done! Our counter will be flushed using our handle_flush/2 callback after the default interval period. Dead counters are automatically removed.

stat_buffer's People

Contributors

nsweeting avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  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.