Keybase is a key counting database with expiring keys and optional persistence.
Keybase is designed to work out of the box with minimal configuration. To use keybase,
call the Open()
function:
kb, err := keybase.Open(keybase.WithStorage("/tmp/keybase.db"), keybase.WithTTL(time.Minute))
This will initialize a database at /tmp/keybase.db
with a key timeout of one minute. Once
keybase is open, it is ready to store and maintain keys. Each key is assigned to a namespace
and can be inserted using the Put
function:
_ = kb.Put(context.Background(), "namespace", "key")
Once the key is stored, various functions can be used to query key and namespace information,
such as GetKeys()
, which will return a slice of strings representing all keys in a given
namespace:
active := true
unique := true
keys, err := kb.GetKeys(context.Background(), "namespace", active, unique)
By setting active
and unique
to true
, the slice will include each active key once. Otherwise,
the string make contain multiple copies of the same key, as well as stale keys, if it has been
submitted multiple times and queried within the TTL duration. Over time, as the keys become stale,
they can be removed using the PruneEntries
function:
_ = kb.PruneEntries(context.Background())
This will remove the stale keys and reduce the amount of storage required by memory or
filesystem. When the keybase is no longer needed, it needs to be disconnected using the
Close()
function:
kb.Close()