Skip to content

Latest commit

 

History

34 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rkv

CI Hex Docs

A simple ETS-based key-value store with the ability to watch changes.

Installation

The package can be installed by adding rkv to your list of dependencies in mix.exs:

def deps do
  [
    {:rkv, "~> 0.1.0"}
  ]
end

Usage

Starting a bucket

Add Rkv to your supervision tree:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      {Rkv, bucket: :my_app_cache}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Or start one directly:

{:ok, _pid} = Rkv.start_link(bucket: :my_bucket)

A bucket's data lives only as long as the process that owns it, so treat a bucket as a cache rather than a store.

Basic operations

# Put a value
:ok = Rkv.put(:my_bucket, "key", "value")

# Get a value
"value" = Rkv.get(:my_bucket, "key")

# Get a missing value
nil = Rkv.get(:my_bucket, "missing")

# Get with default
"default" = Rkv.get(:my_bucket, "missing", "default")

# Delete a value
:ok = Rkv.delete(:my_bucket, "key")

Watching changes

You can subscribe to changes on a specific key or the entire bucket.

# Watch a specific key
Rkv.watch_key(:my_bucket, "config")

Rkv.put(:my_bucket, "config", %{debug: true})

receive do
  {:updated, :my_bucket, "config"} -> IO.puts("Config updated!")
end

Rkv.delete(:my_bucket, "config")

receive do
  {:deleted, :my_bucket, "config"} -> IO.puts("Config deleted!")
end

# Watch all keys
Rkv.watch_all(:my_bucket)

Rkv.put(:my_bucket, "other_key", 123)

receive do
  {:updated, :my_bucket, "other_key"} -> IO.puts("Something changed!")
end

Use Rkv.unwatch_key/2 and Rkv.unwatch_all/1 to stop watching.

About

A simple ETS-based key-value storage with the ability to monitor changes

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages