NestGen2.Storage (nest_gen2 v0.6.6)

Copy Markdown View Source

Settings that survive restarts and power cuts, for apps.

NestGen2.Storage.put(:screen, %{idle_timeout_ms: 30_000})
NestGen2.Storage.get(:screen, %{idle_timeout_ms: :infinity})

Each app gets its own folder in flash that nothing else touches, <install>/data/<app>, and each key is one file in it, holding an Erlang term as readable text (cat shows it) with a checksum. Writes are made safe against the power going mid-write: the new value goes to a temporary file that is flushed to flash, the previous value is kept as a backup, and the temporary file is then renamed into place. A value that doesn't check out when read falls back to the backup, then to the default; it never raises.

Meant for settings and small state that change now and then, not for data written every second: the Nest's flash is slow and wears out.

For anything more (logs, a cache, a database such as CubDB), dir/1 is the app's own folder to use as it likes.

Keys are atoms or strings of lowercase letters, digits and underscores. Values are any term that can be written out and read back as text: maps, lists, tuples, strings, numbers, atoms; not pids, references or functions. The calling process's application is used unless app: is given.

Summary

Functions

The app's own folder in flash (created if missing).

The value stored under key, or default.

The keys stored, as strings.

Stores value under key.

Types

key()

@type key() :: atom() | String.t()

Functions

delete(key, opts \\ [])

@spec delete(key(), keyword()) :: :ok

Removes key.

dir(opts \\ [])

@spec dir(keyword()) :: Path.t()

The app's own folder in flash (created if missing).

get(key, default \\ nil, opts \\ [])

@spec get(key(), term(), keyword()) :: term()

The value stored under key, or default.

keys(opts \\ [])

@spec keys(keyword()) :: [String.t()]

The keys stored, as strings.

put(key, value, opts \\ [])

@spec put(key(), term(), keyword()) :: :ok | {:error, :not_storable | File.posix()}

Stores value under key.