Storage Configuration¶
The Storage configuration is a built-in, persistent key-value store in FIRERPA. Even if FIRERPA or the device restarts, you can still read these variables on the next startup. This Storage allows you to persistently store information on the device for different client processes to read, which can be used to share device configurations such as login accounts or other independent information. You can even store encrypted configurations on the machine.
Attention
Get Storage¶
You can get a Storage object using the following code to perform subsequent operations.
storage = d.stub("Storage")
Clear Storage¶
You can use the following code to clear all information in the Storage, including all containers. You can think of this as formatting the Storage.
storage.clear()
Get Container¶
You can get a key-value storage container object using the following code. The Storage is the main store, and a container is like a bucket within it. All subsequent read and write operations are performed on this bucket.
container = storage.use("container_name")
Get Encrypted Container¶
If you need to store values securely, for example, when the device might be used by others and you don't want your stored configurations to be read by them, you can use an encrypted container. Unlike a simple container, an encrypted container will encrypt the key-value pairs you set. Others will need a password or other verification to read them correctly.
Our library comes with a built-in FernetCryptor for encryption and decryption. You can also implement your own unique encryption algorithm following the FernetCryptor pattern. You only need to implement the encrypt and decrypt methods.
from lamda.client import FernetCryptor
container = storage.use("container_name", cryptor=FernetCryptor,
key="this_is_password")
Delete Container¶
You can use the following code to clear all key-value pairs stored in the container named container_name. This operation is equivalent to deleting the bucket.
storage.remove("container_name")
Write Key-Value to Container¶
You can use the following example calls to set the value of key_name. The value can be any variable that is serializable 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")
Read Key-Value from Container¶
You can use the following method to get the value of key_name. If it does not exist, None is returned.
container.get("key_name")
Get Time to Live (TTL)¶
You can use the following call to get the time to live (TTL) of key_name. A return value of -2 means the key does not exist, -1 means it never expires, and any other positive integer represents the remaining lifetime of the key in seconds.
container.ttl("key_name")
Set Time to Live (TTL)¶
You can use the following call to set the time to live for a key-value pair. For example, the following call will set the key-value pair and cause it to be automatically deleted after 10 seconds.
container.setex("key_name", "value", 10)
Alternatively, if you have already set a key and now want to set its expiration time, you can use the following method. key_name will be automatically deleted after 60 seconds.
container.expire("key_name", 60)
Conditional Write¶
You can use the following call to set the key-value pair only if key_name does not already exist. If it exists, no action is taken.
container.setnx("key_name", "value")
Check for Existence¶
You can use the following call to check if key_name exists in the container.
container.exists("key_name")
Delete Key-Value from Container¶
You can use the following call to delete key_name and its value from the container.
container.delete("key_name")