Skip to content

Simple key-value persistence for Hedwig responders.

Notifications You must be signed in to change notification settings

stevegrossi/hedwig_brain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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:

def deps do
  [
    {:hedwig_brain, "~> 0.1"}
  ]
end

And run mix deps.get. Then, start the Brain process manually:

{:ok, pid} = Hedwig.Brain.start_link

or as part of your application’s supervision tree:

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.

About

Simple key-value persistence for Hedwig responders.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published