The Keystore is a built-in object that provides key-value storage that can be persisted to the ESP32 flash memory. It is a singleton object whose only instance can be obtained by called getkeystore().

The values stored in the keystore using setValue(key, value) remains available across sessions as long as the Microcontroller Brick is powered. Once power is removed, the stored values are gone. For true persistance, the keystore must be saved to flash memory using save(), then later reloaded using the load() function.

API

void load(); // Load keystore from flash memory
void save(); // Save keystore to flash memory
int count(); // Return number of key-value pairs in keystore
bool exists(const char* key); // Return true if key exists in keystore
String getValue(const char* key); // Get value associated with given key. Return empty string if key does not exist.
void setValue(const char* key, const char* value); // Store key-value in keystore.
String getKeyByIndex(int index); // Get key at given index
String getValueByIndex(int index); // Get value at given index
void remove(const char* key); // Remove value for given key
void clear(); // Remove all entries in keystore

Example

var ks = getkeystore();
ks.setValue('a', 'Apple');
ks.setValue('b', 'Boy');
ks.setValue('c', 'Cat');
print('b => ' + ks.getValue('b'));
for (var i=0; i<ks.count(); i++) {
  print(ks.getKeyByIndex(i) + " => " + ks.getValueByIndex(i));
}

Implementation notes

The content of the keystore is saved to the file /rbx/.keystore on the ESP32's flash storage.