https://github.com/stevegrossi/hedwig_brain
Simple key-value persistence for Hedwig responders.
https://github.com/stevegrossi/hedwig_brain
elixir hedwig
Last synced: over 1 year ago
JSON representation
Simple key-value persistence for Hedwig responders.
- Host: GitHub
- URL: https://github.com/stevegrossi/hedwig_brain
- Owner: stevegrossi
- Created: 2017-02-05T13:48:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-08T14:15:50.000Z (over 9 years ago)
- Last Synced: 2025-03-09T06:01:55.039Z (over 1 year ago)
- Topics: elixir, hedwig
- Language: Elixir
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hedwig Brain
Simple key-value persistence for Hedwig responders, `Hedwig.Brain` stores lists of arbitrary terms under binary keys. Optionally, you may persist state to Redis for data durability across crashes and restarts.
## Usage
```elixir
alias Hedwig.Brain
# Store lists under binary keys:
iex> Brain.set("people", ["Ada", "Babbage"])
# Retrieve lists by key:
iex> Brain.get("people")
["Ada", "Babbage"]
# Delete lists. Lists at missing keys are empty:
iex> Brain.delete("people")
iex> Brain.get("people")
[]
# Append items to lists:
iex> Brain.add("people", %{first: "Ada", last: "Byron"})
iex> Brain.get("people")
%{first: "Ada", last: "Byron"}
# Delete items from lists:
iex> Brain.set("people", ["Ada", "Babbage"])
iex> Brain.remove("people", "Ada")
iex> Brain.get("people")
"Babbage"
# Retrieve items by index:
iex> Brain.set("people", ["Ada", "Babbage"])
iex> Brain.at_index("people", 0)
"Ada"
```
## Installation
Add `hedwig_brain` as a dependency in `mix.exs`:
```elixir
def deps do
[
{:hedwig_brain, "~> 0.1"}
]
end
```
And run `mix deps.get`. Then, start the `Brain` process manually:
```elixir
{:ok, pid} = Hedwig.Brain.start_link
```
or as part of your application’s supervision tree:
```elixir
children = [
worker(Hedwig.Brain, [])
]
```
Note that the `Brain` process is named, so you cannot run more than one instance of it.
## In Production
By default, the `Brain` uses an in-memory `ProcessStore`—implemented as an `Agent`—to hold state. As a result, all state is lost when the process crashes or is restarted. To persist state to disk so that it survives restarts, you can configure the `Brain` to store state in Redis:
config :hedwig_brain, :store, Hedwig.Brain.RedisStore
The `RedisStore` will expect Redis to be running on the default host and port, `localhost:6379`. This can be overridden by either the `REDIS_URL` environment variable, or else `config :hedwig_brain, :redis_url` in the configuration file.
## Testing
The tests for `hedwig_brain` require Redis to be running on the default host and port. They call `FLUSHDB` on database 3 before each test. The default Redis database is 0, but know that if you’re using database 3 locally, its data *will be wiped* by `mix test`.