Key-Value Store
All Less deployments come with a built in key-value store.
Import the Key-Value Store
- Node.js
- Python
Import kvs
from @chuva.io/less
.
const { kvs } = require('@chuva.io/less');
Import kvs
from less
.
from less import kvs
Set Value for Key
- Node.js
- Python
The key should be a string
. The value can be any primitive (number, object, etc.).
const { kvs } = require('@chuva.io/less');
await kvs.set('MY_KEY', 'This is my value.');
from less import kvs
kvs.set('MY_KEY', 'This is my value.')
Get Value for Key
Get a value for a key.
- Node.js
- Python
const { kvs } = require('@chuva.io/less');
const my_value = await kvs.get('MY_KEY');
from less import kvs
my_value = kvs.get('MY_KEY')
Delete Value and Key
Delete a key and its value.
- Node.js
- Python
const { kvs } = require('@chuva.io/less');
await kvs.delete('MY_KEY');
from less import kvs
kvs.delete('MY_KEY')
Subscribe to KVS changes
The Key-Value Store (KVS) allows you to subscribe to a stream of real-time updates by leveraging Less's Topics/Subscribers (Pub/Sub) feature.
Read the Less Topics/Subscribers (Pub/Sub) documentation to learn more.
Subscribe to create
events
Create the kvs_created
Topic.
mkdir -p less/topics/kvs_created/log
- Node.js
- Python
touch less/topics/kvs_created/log/index.js
exports.process = async ({ key, new_value }) => {
console.log(`New item created with key: '${key}' and value: '${new_value}'.`);
}
touch less/topics/kvs_created/log/__init__.py
def process(data):
key = data['key']
new_value = data['new_value']
print(f"A new item is being created with the key '{key}' and value {new_value}")
Here is an example kvs_created
event payload:
{
"key": "artists",
"new_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}
Subscribe to update
events
Create the kvs_updated
Topic.
mkdir -p less/topics/kvs_updated/log
- Node.js
- Python
touch less/topics/kvs_updated/log/index.js
exports.process = async ({ key, old_value, new_value }) => {
console.log(`Item with with key: '${key}' updated.`);
console.log(`Old value: ${old_value}.`);
console.log(`New value: ${new_value}.`);
}
touch less/topics/kvs_updated/log/__init__.py
def process(data):
key = data['key']
new_value = data['new_value']
print(f"The value for the key '{key}' was updated.\nNew value: {new_value}\nOld value: {old_value}")
Here is an example kvs_updated
event payload:
{
"key": "artists",
"new_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
},
{
"first_name": "Mayra",
"last_name": "Andrade",
"dob": "02/13/1985"
}
],
"old_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}
Subscribe delete
events
Create the kvs_deleted
Topic.
mkdir -p less/topics/kvs_deleted/log
- Node.js
- Python
touch less/topics/kvs_deleted/log/index.js
exports.process = async ({ key, old_value }) => {
console.log(`Item with with key: '${key}' deleted.`);
console.log(`Old value: ${old_value}.`);
}
touch less/topics/kvs_deleted/log/__init__.py
def process(data):
key = data['key']
new_value = data['new_value']
print(f"The key '{key}' was deleted.")
Here is an example kvs_deleted
event payload:
{
"key": "artists",
"old_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}
Read the Less Topics/Subscribers (Pub/Sub) documentation to learn more.
Time to live
KVS offers a valuable feature called Time to Live (TTL
), which allows you to set an expiration time for an item, leading to its automatic deletion after the specified period. To use this feature, you can add the TTL
parameter as a third argument when creating a new item. While the first two parameters are required, the TTL
parameter is optional and should be an integer representing the time in seconds until the item expires.
- Node.js
- Python
const { kvs } = require('@chuva.io/less');
exports.process = async () => {
// This is two hours represented in seconds
const ttl = 2 * 60 * 60;
// The follow item should be expired within 2 hours after its creation
await kvs.set('UserName', 'Cesaria', ttl);
}
from less import kvs
def process():
# This is two hours represented in seconds
ttl = 2 * 60 * 60
# The follow item should be expired within 2 hours after its creation
kvs.set('UserName', 'Cesaria', ttl)