title: “NOSQL::SET_TTL Function” author: “Pierre Laplante laplante@plcb.ca” date: “2024-06-19” version: “1.0.0” section: “1g” category: “GOWEB/NOSQL”
nosql::set_ttl stores a key-value pair in the database with an associated time-to-live (TTL).
nosql::set_ttl(key, data, duration [, db: database, error: id])
nosql::setTtl(key, data, duration [, db: database, error: id])
The nosql::set_ttl
function stores a key-value pair in the database with an associated time-to-live (TTL).
The entry will automatically expire after the specified duration.
A duration string is a sequence of decimal numbers, optionally signed, each with an optional fraction and a unit suffix.
Examples: "300ms"
, "-1.5h"
, "2h45m"
.
Valid time units are:
ns
(nanoseconds)us
or µs
(microseconds)ms
(milliseconds)s
(seconds)m
(minutes)h
(hours)If the database parameter is not specified, an in-memory database will be used.
{{
res := nosql::open("/tmp/xx");
nosql::set(res, "kint", 1);
"kint="; nosql::get(res, "kint"); "\n";
nosql::set(res, "kfloat", 3.1415);
"kfloat="; nosql::get(res, "kfloat"); "\n";
nosql::set(res, "kstring", "String");
"kstring="; nosql::get(res, "kstring"); "\n";
nosql::set(res, "kbool", true);
"kbool="; nosql::get(res, "kbool"); "\n";
id := "mystrid";
nosql::set(res, "kid", id);
"kid="; nosql::get(res, "kid"); "\n";
a := [1, 3.1515, "string", true];
nosql::set(res, "karray", a);
"karray="; nosql::get(res, "karray"); "\n";
nosql::set(res, "kmap", {"x" : 2, "y" : 3.1414});
"KMAP="; nosql::get(res, "kmap"); "\n";
"--- DUMP ---\n";
x := nosql::dump(res);
"x="; x; "\n";
"--- DUMP ---\n";
nosql::get(res, "xywhdue", error : err);
"errget = "; err; "\n";
nosql::delete(res, "xywhdue", error : err);
"errdelete = "; err; "\n";
nosql::exist(res, "xywhdue", error : err);
"errexist = "; err; "\n";
"keys = "; nosql::keys(res); "\n";
nosql::close(res);
}}
Output:
kint=1
kfloat=3.1415
kstring=String
kbool=true
kid=mystrid
karray=[1,3.1515,"string",true]
KMAP={"x":2,"y":3.1414}
--- DUMP ---
x={"karray":[1,3.1515,"string",true],"kbool":true,"kfloat":3.1415,"kid":"mystrid","kint":1,"kmap":{"x":2,"y":3.1414},"kstring":"String"}
--- DUMP ---
errget = nosql::get : problem getting key 'xywhdue' : Key not found
errdelete =
falseerrexist =
keys = ["karray","kbool","kfloat","kid","kint","kmap","kstring"]
res={{
dbdir := "/tmp/ttl";
remove(recursive:true, dbdir);
db := nosql::open(dbdir);
nosql::set_ttl(db, "mykey", "myvalue", "1s");
"key="; nosql::get(db, "mykey"); "\n";
flush();
sleep("2s");
nosql::get(db, "mykey", error:err);
"err="; err;
}}
Output:
res=key=myvalue
err=nosql::get : problem getting key 'mykey' : Key not found
Pierre Laplante laplante@plcb.ca