https://github.com/nsweeting/stat_buffer
StatBuffer provides an efficient way to maintain persistable stat counts.
https://github.com/nsweeting/stat_buffer
buffer counter elixir ets phoenix stats
Last synced: about 1 year ago
JSON representation
StatBuffer provides an efficient way to maintain persistable stat counts.
- Host: GitHub
- URL: https://github.com/nsweeting/stat_buffer
- Owner: nsweeting
- License: mit
- Created: 2018-04-28T18:45:20.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-25T20:35:06.000Z (over 6 years ago)
- Last Synced: 2025-04-29T11:44:04.351Z (about 1 year ago)
- Topics: buffer, counter, elixir, ets, phoenix, stats
- Language: Elixir
- Homepage:
- Size: 44.9 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StatBuffer
[](https://travis-ci.org/nsweeting/stat_buffer)
[](https://hex.pm/packages/stat_buffer)
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`:
```elixir
def deps do
[
{:stat_buffer, "~> 1.2"}
]
end
```
## Documentation
Please see [HexDocs](https://hexdocs.pm/stat_buffer/StatBuffer.html#content) 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.
```elixir
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.
```elixir
children = [
Buffer
]
```
There are some configruable options available for our buffers. You can read more about them [here](https://hexdocs.pm/stat_buffer/StatBuffer.html#module-options). These options can be passed when creating our buffer.
```elixir
use StatBuffer, interval: 10_000
```
With our buffer started, we can now increment key counters. A key can be any valid term.
```elixir
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.