Configuration Storage¶
Configuration Storage is FIRERPA’s built-in key-value store that has persistence. Even if FIRERPA or the device restarts, you can still read these variables the next time you start. This Storage allows you to persistently store information in the device for different client processes to read, to share device configurations such as login accounts or other independent information. You can even store encrypted configurations in the machine.
Attention
The built-in Storage has a total capacity of 128MB. Please do not use it to store large amounts of data. It does not support listing container names or key names existing in the storage. You must know the container name and key name completely to read values from the container, otherwise, you will never be able to read them.
Getting Storage¶
You can get a Storage object through the following code, so you can perform subsequent operations.
storage = d.stub("Storage")
Clearing Storage¶
You can clear all information in the Storage, including containers, through the following code. You can simply understand it as - formatting Storage.
storage.clear()
Getting a Container¶
You can get a key-value storage container object through the following code. Storage is storage, and a container is a bucket under the storage. Your subsequent read and write operations are all performed on this bucket.
container = storage.use("container_name")
Getting an Encrypted Container¶
If you also need to securely store values, for example, when the device will be used by others, but you don’t want the stored configuration to be read by others, you can use an encrypted container. It is different from simply getting a container; an encrypted container will encrypt and store the key values you set. Others need a password or other verification to correctly read them.
Our library comes with a FernetCryptor for encryption and decryption. You can also implement your own unique encryption algorithm according to FernetCryptor. You just need to implement the encrypt and decrypt methods.
from lamda.client import FernetCryptor
container = storage.use("container_name", cryptor=FernetCryptor,
key="this_is_password")
Removing a Container¶
You can clear all key values stored in the container named container_name through the following code. This operation is also equivalent to deleting the bucket.
storage.remove("container_name")
Writing Key Values to a Container¶
You can set the value of key_name to “value” through the following example call. The value supports any variable that can be serialized by msgpack.
container.set("key_name", [1, 2, 3])
container.set("key_name", {"john": "due"})
container.set("key_name", b"value")
container.set("key_name", "value")
Reading Key Values from a Container¶
You can get the value of key_name through the following method. If it doesn’t exist, it returns None.
container.get("key_name")
Getting Time-to-Live¶
You can get the time-to-live of key_name through the following call. -2 means the key does not exist, -1 means never expires, other positive integers are the remaining seconds for the key to live.
container.ttl("key_name")
Setting Time-to-Live¶
You can set the time-to-live for key values through the following call. For example, the following call method will automatically delete this key value 10 seconds after setting it.
container.setex("key_name", "value", 10)
Or, if you have previously set this key and now want to set a time-to-live for it, you can use it like this. After 60 seconds, key_name will be automatically deleted.
container.expire("key_name", 60)
Conditional Writing¶
You can call it like below, so the key value will only be set when key_name does not exist. If it already exists, no action will be taken.
container.setnx("key_name", "value")
Checking if Exists¶
You can call it like below to check if key_name exists in the container.
container.exists("key_name")
Deleting Key Values from a Container¶
You can call it like below to delete key_name and its value from the container.
container.delete("key_name")