Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Last active August 29, 2016 21:30
Show Gist options
  • Save adamzaninovich/c1914392c783815cf6b10f3307897658 to your computer and use it in GitHub Desktop.
Save adamzaninovich/c1914392c783815cf6b10f3307897658 to your computer and use it in GitHub Desktop.
defmodule MyApp.Redis do
@moduledoc "State management using redis for persistence"
alias MyApp.Redis.Connection
@doc "Starts the redis conection and registers the process"
def start_link(redis_connection_string \\ "redis://localhost") do
Redix.start_link(redis_connection_string, name: MyApp.Redis.Connection)
end
@doc "Sets a value to a key in redis"
def put(key, val) do
Redix.command!(Connection, ["SET", key, val])
end
@doc "Returns the value for a key from redis"
def get(key, default \\ nil) do
Connection
|> Redix.command!(["get", key])
|> case do
nil -> default
value -> value
end
end
@doc "Deletes a key in redis"
def delete(key) do
Redix.command!(Connection, ["DEL", key])
end
@doc "pulls out all keys and puts them into an elixir Map"
def get_all do
keys
|> Stream.map(fn(key) -> {key, get(key)} end)
|> Enum.into(%{})
end
@doc "Gets all keys from redis as a list of strings"
def keys do
Redix.command!(Connection, ["KEYS", "*"])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment