Flux Redis v0.0.2 FluxRedis.HashManager View Source

Manage Redis Hash data as a JSON storage.

After defining hash_storages on your FluxRedis application configuration, you can use the functions of this module to manage hashes and hash fields as JSON storage.

A Redis hash maintain a JSON data in each field. Each hash field name is defined according to the keys defined by a hash in hash_storages.

For more information about application configuration, check FluxRedis main module and homepage.

Command Options

  • :no_reply? - If true, the function returns :ok after successfully send the request to Redis. Disabled (false) in get-type functions and defaults to true in set-type functions. Accepts boolean/0.

  • :timeout - Milliseconds to wait for a request sending. Defaults to 5_000. Accepts integer/0 or :infinity.

Link to this section Summary

Functions

Count the number of fields in a hash.

Count the number of fields in a hash.

Removes one or more fields from a hash.

Removes one or more fields from a hash.

Retrieves a field from a hash.

Retrieves a field from a hash.

Retrieves all fields in a hash.

Retrieves all fields in a hash.

Retrieves one or more fields in a hash.

Retrieves one or more fields in a hash.

Retrieves one or more fields that match key values pattern in a hash.

Retrieves one or more fields that match key values pattern in a hash.

Check if field exists in a hash.

Check if field exists in a hash.

Add or update a field in a hash.

Add or update a field in a hash.

Add or update one or more fields in a hash.

Add or update one or more fields in a hash.

Link to this section Types

Link to this type

key_data()

View Source
key_data() :: atom() | integer() | float() | String.t()
Link to this type

key_data_list()

View Source
key_data_list() :: [[key_data()] | key_data()]

Link to this section Functions

Link to this function

count(hash, opts \\ [])

View Source (since 0.0.2)
count(atom(), keyword()) :: {:ok, integer()} | {:error, FluxRedis.Error.t()}

Count the number of fields in a hash.

Returns {:ok, integer} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [users: :id]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> users = [%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]
...> FluxRedis.HashManager.set_many(:users, users)
...> FluxRedis.HashManager.count(:users)
{:ok, 2}
Link to this function

count!(hash, opts \\ [])

View Source (since 0.0.2)
count!(atom(), keyword()) :: integer()

Count the number of fields in a hash.

Similar to count/2, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [users: :id]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> users = [%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]
...> FluxRedis.HashManager.set_many(:users, users)
...> FluxRedis.HashManager.count!(:users)
2
Link to this function

delete(hash, key_data, opts \\ [no_reply?: true])

View Source (since 0.0.2)
delete(atom(), [key_data()] | key_data(), keyword()) ::
  :ok | {:error, FluxRedis.Error.t()}

Removes a field from a hash.

Returns :ok in case of success or {:error, %FluxRedis.Error{}} otherwise.

By default, the option no_reply? is set to true.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.delete(:cars, ["Honda", "Civic"])
:ok
Link to this function

delete!(hash, key_data, opts \\ [no_reply?: true])

View Source (since 0.0.2)
delete!(atom(), [key_data()] | key_data(), keyword()) :: :ok

Removes a field from a hash.

Similar to delete/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.delete(:cars, ["Honda", "Civic"])
:ok
Link to this function

delete_many(hash, key_data_list, opts \\ [no_reply?: true])

View Source (since 0.0.2)
delete_many(atom(), key_data_list(), keyword()) ::
  :ok | {:ok, integer()} | {:error, FluxRedis.Error.t()}

Removes one or more fields from a hash.

Returns :ok in case of successful request and no_reply? is set to true.

Returns {:ok, amount_removed} in case of success and no_reply? is set to false.

Returns {:error, %FluxRedis.Error{}} on failure.

By default, the option no_reply? is set to true.

Parameters

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> animals_keys = [["Chordata", "Canidae"], ["Chordata", "Felidae"]]
...> alias FluxRedis.HashManager
...> HashManager.delete_many(:animals, animals_keys, no_reply?: false)
{:ok, 2}
Link to this function

delete_many!(hash, key_data_list, opts \\ [no_reply?: true])

View Source (since 0.0.2)
delete_many!(atom(), key_data_list(), keyword()) :: :ok | integer()

Removes one or more fields from a hash.

Similar to delete_many/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> animals_keys = [["Chordata", "Canidae"], ["Chordata", "Felidae"]]
...> alias FluxRedis.HashManager
...> HashManager.delete_many!(:animals, animals_keys, no_reply?: false)
2
Link to this function

get(hash, key_data, opts \\ [])

View Source (since 0.0.1)
get(atom(), [key_data()] | key_data(), keyword()) ::
  {:ok, map()} | {:error, FluxRedis.Error.t()}

Retrieves a field from a hash.

Returns {:ok, field} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.get(:cars, ["Honda", "Civic"])
{:ok, %{brand: "Honda", model: "Civic", year: 2019}}
Link to this function

get!(hash, key_data, opts \\ [])

View Source (since 0.0.2)
get!(atom(), [key_data()] | key_data(), keyword()) :: map()

Retrieves a field from a hash.

Similar to get/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.get!(:cars, ["Honda", "Civic"])
%{brand: "Honda", model: "Civic", year: 2019}
Link to this function

get_all(hash, opts \\ [])

View Source (since 0.0.1)
get_all(atom(), keyword()) :: {:ok, [map()]} | {:error, FluxRedis.Error.t()}

Retrieves all fields in a hash.

Returns {:ok, fields} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [users: :id]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> users = [%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]
...> FluxRedis.HashManager.set_many(:users, users)
...> FluxRedis.HashManager.get_all(:users)
{:ok, [%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]}
Link to this function

get_all!(hash, opts \\ [])

View Source (since 0.0.2)
get_all!(atom(), keyword()) :: [map()]

Retrieves all fields in a hash.

Similar to get_all/2, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [users: :id]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> users = [%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]
...> FluxRedis.HashManager.set_many(:users, users)
...> FluxRedis.HashManager.get_all!(:users)
[%{id: 1, name: "John"}, %{id: 2, name: "Doe"}]
Link to this function

get_many(hash, key_data_list, opts \\ [])

View Source (since 0.0.1)
get_many(atom(), key_data_list(), keyword()) ::
  {:ok, [map()]} | {:error, FluxRedis.Error.t()}

Retrieves one or more fields in a hash.

Returns {:ok, fields} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> animals_keys = [["Chordata", "Canidae"], ["Chordata", "Felidae"]]
...> FluxRedis.HashManager.get_many(:animals, animals_keys)
{
  :ok,
  [
   %{family: "Canidae", phylum: "Chordata"},
   %{family: "Felidae", phylum: "Chordata"}
  ]
}
Link to this function

get_many!(hash, key_data_list, opts \\ [])

View Source (since 0.0.2)
get_many!(atom(), key_data_list(), keyword()) :: [map()]

Retrieves one or more fields in a hash.

Similar to get_many/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> animals_keys = [["Chordata", "Canidae"], ["Chordata", "Felidae"]]
...> FluxRedis.HashManager.get_many!(:animals, animals_keys)
[
  %{family: "Canidae", phylum: "Chordata"},
  %{family: "Felidae", phylum: "Chordata"}
]
Link to this function

get_match(hash, key_data, opts \\ [])

View Source (since 0.0.2)
get_match(atom(), [key_data()] | key_data(), keyword()) ::
  {:ok, [map()]} | {:error, FluxRedis.Error.t()}

Retrieves one or more fields that match key values pattern in a hash.

Returns {:ok, fields} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> pattern = ["*", "Can*"]
...> FluxRedis.HashManager.get_match(:animals, pattern)
{:ok, [%{family: "Canidae", phylum: "Chordata"}]}
Link to this function

get_match!(hash, key_data, opts \\ [])

View Source (since 0.0.2)
get_match!(atom(), [key_data()] | key_data(), keyword()) :: [map()]

Retrieves one or more fields that match key values pattern in a hash.

Similar to get_match/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [animals: [:phylum, :family]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> animals = [
...>   %{family: "Canidae", phylum: "Chordata"},
...>   %{family: "Felidae", phylum: "Chordata"}
...> ]
...> FluxRedis.HashManager.set_many(:animals, animals)
...> pattern = ["*", "Can*"]
...> FluxRedis.HashManager.get_match!(:animals, pattern)
[%{family: "Canidae", phylum: "Chordata"}]
Link to this function

has(hash, key_data, opts \\ [])

View Source (since 0.0.2)
has(atom(), [key_data()] | key_data(), keyword()) ::
  {:ok, boolean()} | {:error, FluxRedis.Error.t()}

Check if field exists in a hash.

Returns {:ok, boolean} in case of success or {:error, %FluxRedis.Error{}} otherwise.

This function always sets :no_reply? option to false.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.has(:cars, ["Honda", "Civic"])
{:ok, true}
...> FluxRedis.HashManager.has(:cars, ["Honda", "City"])
{:ok, false}
Link to this function

has?(hash, key_data, opts \\ [])

View Source (since 0.0.2)
has?(atom(), [key_data()] | key_data(), keyword()) :: boolean()

Check if field exists in a hash.

Similar to has/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car)
...> FluxRedis.HashManager.has?(:cars, ["Honda", "Civic"])
true
...> FluxRedis.HashManager.has?(:cars, ["Honda", "City"])
false
Link to this function

set(hash, data, opts \\ [no_reply?: true])

View Source (since 0.0.1)
set(atom(), map(), keyword()) ::
  :ok | {:ok, :set | :not_changed} | {:error, FluxRedis.Error.t()}

Add or update a field in a hash.

Returns :ok in case of successful request and no_reply? is set to true.

Returns {:ok, :set} or {:ok, :not_changed} in case of success and no_reply? is set to false.

Returns {:error, %FluxRedis.Error{}} on failure.

By default, the option no_reply? is set to true.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set(:cars, car, no_reply?: false)
{:ok, :set}
...> FluxRedis.HashManager.set(:cars, car, no_reply?: false)
{:ok, :not_changed}
Link to this function

set!(hash, data, opts \\ [no_reply?: true])

View Source (since 0.0.2)
set!(atom(), map(), keyword()) :: :ok | :set | :not_changed

Add or update a field in a hash.

Similar to set/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set!(:cars, car, no_reply?: false)
:set
...> FluxRedis.HashManager.set!(:cars, car, no_reply?: false)
:not_changed
Link to this function

set_many(hash, data_list, opts \\ [no_reply?: true])

View Source (since 0.0.2)
set_many(atom(), [map()], keyword()) ::
  :ok | {:ok, integer()} | {:error, FluxRedis.Error.t()}

Add or update one or more fields in a hash.

Returns :ok in case of successful request and no_reply? is set to true.

Returns {:ok, amount_set} in case of success and no_reply? is set to false.

Returns {:error, %FluxRedis.Error{}} on failure.

By default, the option no_reply? is set to true.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> civic = %{brand: "Honda", model: "Civic", year: 2019}
...> city = %{brand: "Honda", model: "City", year: 2018}
...> alias FluxRedis.HashManager
...> HashManager.set_many(:cars, [civic, city], no_reply?: false)
{:ok, 2}
...> ram = %{brand: "Dodge", model: "Ram", year: 2020}
...> HashManager.set_many(:cars, [city, ram], no_reply?: false)
{:ok, 1}
Link to this function

set_many!(hash, data_list, opts \\ [no_reply?: true])

View Source (since 0.0.2)
set_many!(atom(), [map()], keyword()) :: :ok | integer()

Add or update one or more fields in a hash.

Similar to set_many/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> civic = %{brand: "Honda", model: "Civic", year: 2019}
...> city = %{brand: "Honda", model: "City", year: 2018}
...> alias FluxRedis.HashManager
...> HashManager.set_many!(:cars, [civic, city], no_reply?: false)
...> ram = %{brand: "Dodge", model: "Ram", year: 2020}
...> HashManager.set_many!(:cars, [city, ram], no_reply?: false)
1
Link to this function

set_new(hash, data, opts \\ [no_reply?: true])

View Source (since 0.0.2)
set_new(atom(), map(), keyword()) ::
  :ok | {:ok, :set | :not_changed} | {:error, FluxRedis.Error.t()}

Add a field in a hash.

This function does not update a field, use set/3 to update a field.

Returns :ok in case of successful request and no_reply? is set to true.

Returns {:ok, :set} or {:ok, :not_changed} in case of success and no_reply? is set to false.

Returns {:error, %FluxRedis.Error{}} on failure.

By default, the option no_reply? is set to true.

Parameters

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set_new(:cars, car, no_reply?: false)
{:ok, :set}
...> FluxRedis.HashManager.set_new(:cars, car, no_reply?: false)
{:ok, :not_changed}
Link to this function

set_new!(hash, data, opts \\ [no_reply?: true])

View Source (since 0.0.2)
set_new!(atom(), map(), keyword()) :: :ok | :set | :not_changed

Add a field in a hash.

Similar to set_new/3, but raises FluxRedis.Error.t/0 on failure.

Examples

iex> hash_storages = [cars: [:brand, :model]]
...> Application.put_env(:flux_redis, :hash_storages, hash_storages)
...> car = %{brand: "Honda", model: "Civic", year: 2019}
...> FluxRedis.HashManager.set_new!(:cars, car, no_reply?: false)
:set
...> FluxRedis.HashManager.set_new!(:cars, car, no_reply?: false)
:not_changed