Skip to content

adrienmo/buffer

Repository files navigation

buffer

Travis Coveralls Hex.pm Ebert

Provide read and write buffers for Elixir. Use-case examples:

  • Read buffer for a RDBMS.
  • Write buffer for statsd counters.
  • Write buffer to do batch API calls.

Description

buffer is a library to create read or write buffer easily.

Usage

Add buffer to your project

Update your applications list to include the buffer project:

def application do
  [applications: [:buffer]]
end

Add the buffers to the supervisor when your application starts

def start(_type, _args) do
  Buffer.Supervisor.start_child(BufferKeyListLimit)
  MyApplication.Supervisor.start_link()
end

Write Buffer for KeyList

## Declaration of the buffer
defmodule BufferKeyListLimit do
  use Buffer.Write.KeyList, interval: 1000, limit: 10
  def write(keylists) do
    ## Write here your flushing function
  end
end

## Usage
BufferKeyListLimit.add(:key1, "value1")
BufferKeyListLimit.add(:key1, "value2")
BufferKeyListLimit.add(:key2, "value3")

## The write function will receive this value
[{:key1, ["value1", "value2"]}, {:key2, ["value3"]}]

## API
BufferKeyListLimit.sync()
BufferKeyListLimit.sync(:key1)

Write Buffer for Counters

## Declaration of the buffer
defmodule BufferCount do
  use Buffer.Write.Count, interval: 1000
  def write(counters) do
    ## Write here your flushing function
  end
end

## Usage
BufferCount.incr(:key1)
BufferCount.incr(:key1)
BufferCount.incr(:key2, 10)
BufferCount.incr(:key2, 15)

## The write function will receive this value
[{:key1, 2}, {:key2, 25}]

## API
BufferCount.sync()

Read Buffer

## Declaration of the buffer
defmodule BufferRead do
  use Buffer.Read, interval: 1000, timeout: 5_000, synchronize: true
  def read() do
    ## Write here your reading function
    [{:key1, "value1"}, {:key2, "value2"}]
  end

  def on_element_updated(keys) do
    #new key is added to the list, do something:
    spawn(fn() -> IO.inspect keys end)
  end

  def updated?(el1, el2) do
    #Customize function that determine if the object for one key has been
    # updated
    el1 != el2
  end
end

## Options

- synchronize: is used to force sync when the value is nil.
- timeout: timeout for the read function
- interval: auto sync interval

## Usage
"value1" = BufferRead.read(:key1)

## API
BufferRead.sync()

About

Provide read and write buffers for Elixir

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages