A simple ETS-based key-value store with the ability to watch changes.
The package can be installed by adding rkv to your list of dependencies in mix.exs:
defdepsdo[{:rkv,"~> 0.1.0"}]endAdd Rkv to your supervision tree:
defmoduleMyApp.ApplicationdouseApplicationdefstart(_type,_args)dochildren=[{Rkv,bucket: :my_app_cache}]opts=[strategy: :one_for_one,name: MyApp.Supervisor]Supervisor.start_link(children,opts)endendOr 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.
# Put a value:ok=Rkv.put(:my_bucket,"key","value")# Get a value"value"=Rkv.get(:my_bucket,"key")# Get a missing valuenil=Rkv.get(:my_bucket,"missing")# Get with default"default"=Rkv.get(:my_bucket,"missing","default")# Delete a value:ok=Rkv.delete(:my_bucket,"key")You can subscribe to changes on a specific key or the entire bucket.
# Watch a specific keyRkv.watch_key(:my_bucket,"config")Rkv.put(:my_bucket,"config",%{debug: true})receivedo{:updated,:my_bucket,"config"}->IO.puts("Config updated!")endRkv.delete(:my_bucket,"config")receivedo{:deleted,:my_bucket,"config"}->IO.puts("Config deleted!")end# Watch all keysRkv.watch_all(:my_bucket)Rkv.put(:my_bucket,"other_key",123)receivedo{:updated,:my_bucket,"other_key"}->IO.puts("Something changed!")endUse Rkv.unwatch_key/2 and Rkv.unwatch_all/1 to stop watching.