Fortinet white logo
Fortinet white logo

Global Key-Value Table

Global Key-Value Table

Fortiweb LUA supports a feature (Key-Value Table) that supports different transactions within same server policy to share global data. Transactions of different server policies cannot visit other’s global data.

SVRPOLICY:shared_table_create(table)

The `share_table_create` function is used to create a shared table if one with the specified name does not already exist. Each operation on a shared table must specify a `table_name`. The optional parameters include `entry_size` and `memory_limit`. If a table with the given `table_name` already exists, the function will return true. Typically, the first thread that calls this function will create the table. Returns Boolean true if the table is successfully created or has already been created. Otherwise, it returns false.

Syntax
SVRPOLICY:shared_table_create(table)
Arguments
Name

Description

table

A LUA table including:

  • table_name

    Required, length 1-127 characters

  • entry_count

    Optional, default 4096, 1-FWB_MAX_WEB_CACHE_HASH_SIZE

  • memory_limit

    Optional, default 1024*1024*2(2MB), 1-1024*1024*8(8MB)

Events

Applicable in the RUIT_INIT event.

Example

local table_name = "test_table"

when RULE_INIT {
local table_1 = {
    table_name = table_name,
    entry_count = 3,
    memory_limit = 1024 * 1024
}
local success = SVRPOLICY:shared_table_create(table_1)
debug("create table result %s", success)
}

SVRPOLICY:shared_table_destroy(table_name)

The `shared_table_destroy` function is used to destroy the specified shared table and all the entries associated. Returns Boolean true if the destroy action is successful. Otherwise, it returns false.

Syntax
SVRPOLICY:shared_table_destroy(table_name)
Arguments
Name

Description

table_name

String type, length 1-127 characters

Events

Applicable in the RULE_EXIT event.

Example
local table_name = "test_table"

when RULE_EXIT {
local success = SVRPOLICY:shared_table_destroy(table_name)
debug("destroy table result %s", success)
}

SVRPOLICY:shared_table_insert(table_name, key, value, expire_seconds)

The `shared_table_insert` function is used to insert a pair of <key, value> as the entry into the shared table. Returns true if successful, otherwise return false.

Syntax
SVRPOLICY:shared_table_insert(table_name, key, value, expire_seconds)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

value

Required, can be a lua string, number or table.

expire_seconds

Optional, integer type, 10-86400.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local user = "Alice"
local user_info = {
    id = 456,
    name = "Alice Tian",
    roles = {"admin", "editor"}
}
local success = SVRPOLICY:shared_table_insert(table_name, user, user_info, 1200)
debug("insert key-value result %s", success)
}

SVRPOLICY:shared_table_lookup(table_name, key)

The `shared_table_lookup` function is used to look up whether a key exists in the shared table. Return the stored value if successful, otherwise, return nil.

Syntax
SVRPOLICY:shared_table_lookup(table_name, key)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local key = "Alice"
local value = SVRPOLICY:shared_table_lookup(table_name, key)
if value then
    debug("Found key %s value = %s \n", key, value)
else
    debug("cannot find target node")
end
}

SVRPOLICY:shared_table_delete(table_name, key)

The `shared_table_delete` function is used to delete an entry specified by a key from the shared table. If the key does not exist or the table with `table_name` does not exist, the function will do nothing. Returns True if successful, otherwise, returns False.

Syntax
SVRPOLICY:shared_table_delete(table_name, key)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local key = "Alice"
local success = SVRPOLICY:shared_table_delete(table_name, key)
if success then
    debug("key = %s successfully deleted from table %s\n", key, table_name)
else
    debug("Failed to delete key = %s", key)
end
}

SVRPOLICY:shared_table_dump(table_name, index, count)

The `shared_table_dump` function is used to print the current contents of the shared table for debugging purposes. Returns a pair table, which can be traversed with for [k, v]. All keys and values will be converted into strings. Returns NIL if there is any error or the table is empty.

Syntax
SVRPOLICY:shared_table_dump(table_name, index, count)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

index

Optional. Default value: 1.

Range: (1 - current entry count of the table).

count

Optional. Default value: current entry count of the table.

Range: (1-current entry count of the table).

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local dump_result = SVRPOLICY:shared_table_dump(table_name)
if dump_result then
    for k,v in pairs(dump_result) do
        debug("====>> Key = %s Value %s\n", k, v)
    end
else
    debug("Failed to dump of table %s", table_name)
end
}

SVRPOLICY:shared_table_entry_count(table_name)

The `shared_table_count` function is used to retrieve the current count of entries in a shared table. Return the entries count, or returns -1 if the table does not exist.

Syntax
SVRPOLICY:shared_table_entry_count(table_name)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local count_result = SVRPOLICY:shared_table_entry_count(table_name)
if count_result then
        debug(" there are %d entry \n", count_result)
else
    debug("Failed to count or no such table found")
end
}

Global Key-Value Table

Global Key-Value Table

Fortiweb LUA supports a feature (Key-Value Table) that supports different transactions within same server policy to share global data. Transactions of different server policies cannot visit other’s global data.

SVRPOLICY:shared_table_create(table)

The `share_table_create` function is used to create a shared table if one with the specified name does not already exist. Each operation on a shared table must specify a `table_name`. The optional parameters include `entry_size` and `memory_limit`. If a table with the given `table_name` already exists, the function will return true. Typically, the first thread that calls this function will create the table. Returns Boolean true if the table is successfully created or has already been created. Otherwise, it returns false.

Syntax
SVRPOLICY:shared_table_create(table)
Arguments
Name

Description

table

A LUA table including:

  • table_name

    Required, length 1-127 characters

  • entry_count

    Optional, default 4096, 1-FWB_MAX_WEB_CACHE_HASH_SIZE

  • memory_limit

    Optional, default 1024*1024*2(2MB), 1-1024*1024*8(8MB)

Events

Applicable in the RUIT_INIT event.

Example

local table_name = "test_table"

when RULE_INIT {
local table_1 = {
    table_name = table_name,
    entry_count = 3,
    memory_limit = 1024 * 1024
}
local success = SVRPOLICY:shared_table_create(table_1)
debug("create table result %s", success)
}

SVRPOLICY:shared_table_destroy(table_name)

The `shared_table_destroy` function is used to destroy the specified shared table and all the entries associated. Returns Boolean true if the destroy action is successful. Otherwise, it returns false.

Syntax
SVRPOLICY:shared_table_destroy(table_name)
Arguments
Name

Description

table_name

String type, length 1-127 characters

Events

Applicable in the RULE_EXIT event.

Example
local table_name = "test_table"

when RULE_EXIT {
local success = SVRPOLICY:shared_table_destroy(table_name)
debug("destroy table result %s", success)
}

SVRPOLICY:shared_table_insert(table_name, key, value, expire_seconds)

The `shared_table_insert` function is used to insert a pair of <key, value> as the entry into the shared table. Returns true if successful, otherwise return false.

Syntax
SVRPOLICY:shared_table_insert(table_name, key, value, expire_seconds)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

value

Required, can be a lua string, number or table.

expire_seconds

Optional, integer type, 10-86400.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local user = "Alice"
local user_info = {
    id = 456,
    name = "Alice Tian",
    roles = {"admin", "editor"}
}
local success = SVRPOLICY:shared_table_insert(table_name, user, user_info, 1200)
debug("insert key-value result %s", success)
}

SVRPOLICY:shared_table_lookup(table_name, key)

The `shared_table_lookup` function is used to look up whether a key exists in the shared table. Return the stored value if successful, otherwise, return nil.

Syntax
SVRPOLICY:shared_table_lookup(table_name, key)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local key = "Alice"
local value = SVRPOLICY:shared_table_lookup(table_name, key)
if value then
    debug("Found key %s value = %s \n", key, value)
else
    debug("cannot find target node")
end
}

SVRPOLICY:shared_table_delete(table_name, key)

The `shared_table_delete` function is used to delete an entry specified by a key from the shared table. If the key does not exist or the table with `table_name` does not exist, the function will do nothing. Returns True if successful, otherwise, returns False.

Syntax
SVRPOLICY:shared_table_delete(table_name, key)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

key

Required, maximum length 255 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local key = "Alice"
local success = SVRPOLICY:shared_table_delete(table_name, key)
if success then
    debug("key = %s successfully deleted from table %s\n", key, table_name)
else
    debug("Failed to delete key = %s", key)
end
}

SVRPOLICY:shared_table_dump(table_name, index, count)

The `shared_table_dump` function is used to print the current contents of the shared table for debugging purposes. Returns a pair table, which can be traversed with for [k, v]. All keys and values will be converted into strings. Returns NIL if there is any error or the table is empty.

Syntax
SVRPOLICY:shared_table_dump(table_name, index, count)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

index

Optional. Default value: 1.

Range: (1 - current entry count of the table).

count

Optional. Default value: current entry count of the table.

Range: (1-current entry count of the table).

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local dump_result = SVRPOLICY:shared_table_dump(table_name)
if dump_result then
    for k,v in pairs(dump_result) do
        debug("====>> Key = %s Value %s\n", k, v)
    end
else
    debug("Failed to dump of table %s", table_name)
end
}

SVRPOLICY:shared_table_entry_count(table_name)

The `shared_table_count` function is used to retrieve the current count of entries in a shared table. Return the entries count, or returns -1 if the table does not exist.

Syntax
SVRPOLICY:shared_table_entry_count(table_name)
Arguments
Name

Description

table_name

String type, length 1-127 characters.

Events

Applicable in all events.

Example
local table_name = "test_table"

when HTTP_REQUEST {
local count_result = SVRPOLICY:shared_table_entry_count(table_name)
if count_result then
        debug(" there are %d entry \n", count_result)
else
    debug("Failed to count or no such table found")
end
}