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.
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
Please see HexDocs for additional documentation. This readme provides a brief overview, but it is recommended that the docs are used.
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.