DWScript provides mechanisms for sharing state across concurrent requests and users in a thread-safe manner.
Global variables are shared across the entire server, while private variables are scoped to the specific unit or script context.
| Function | Parameters | Description |
|---|---|---|
ReadGlobalVar |
name |
Reads a global variable. Returns Unassigned if not found. |
ReadGlobalVarDef |
name, default |
Reads with a default fallback value. |
TryReadGlobalVar |
name, var v |
Returns True if the variable exists and reads it into v. |
WriteGlobalVar |
name, val [, expire] |
Writes a global variable with optional expiration in seconds. |
IncrementGlobalVar |
name [, step, expire] |
Atomically increments a numeric variable with optional expiration in seconds. |
CompareExchangeGlobalVar |
name, val, comp |
Atomic compare-and-swap operation. |
DeleteGlobalVar |
name |
Removes a global variable. |
CleanupGlobalVars |
filter |
Removes global variables matching the mask (default *). |
GlobalVarsNames |
filter |
Returns array of String with matching names. |
SaveGlobalVarsToString |
- | Serializes all global variables without expiration to a string. |
LoadGlobalVarsFromString |
s |
Deserializes global variables from a string. |
Private variables are similar to global variables but are scoped to the Unit (Module) in which they are called. They are persistent across requests but cannot be accessed from the main script or other units directly.
| Function | Parameters | Description |
|---|---|---|
ReadPrivateVar |
name [, default] |
Reads a private variable with an optional default. |
WritePrivateVar |
name, val [, expire] |
Writes a private variable with optional expiration in seconds. |
IncrementPrivateVar |
name [, step, expire] |
Atomically increments a private variable. |
CompareExchangePrivateVar |
name, val, comp |
Atomic compare-and-swap for private variables. |
CleanupPrivateVars |
filter |
Removes private variables matching the mask (default *). |
PrivateVarsNames |
filter |
Returns array of String with matching names. |
Thread-safe message queues for inter-request communication.
| Function | Parameters | Description |
|---|---|---|
GlobalQueuePush |
name, val |
Adds item to the end. Returns new length. |
GlobalQueueInsert |
name, val |
Adds item to the front. |
GlobalQueuePull |
name, var v |
Removes and returns first item (FIFO). Returns True if success. |
GlobalQueuePop |
name, var v |
Removes and returns last item (LIFO). |
GlobalQueuePeek |
name, var v |
Gets last item without removing. |
GlobalQueueFirst |
name, var v |
Gets first item without removing. |
GlobalQueueLength |
name |
Current number of items in the queue. |
GlobalQueueClear |
name |
Removes all items from the queue. |
GlobalQueueSnapshot |
name |
Returns a copy of the entire queue as an array. |
var count : Integer;
// Atomic increment
WriteGlobalVar('Requests', 0); // Reset for documentation demo
count := IncrementGlobalVar('Requests');
PrintLn('Request #' + IntToStr(count)); Request #1