NaviServer - programmable web server

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

# ns_sls(n) 4.99.8 naviserver "NaviServer Built-In Commands"

## Name

ns_sls - Socket local storage for connections

## Table Of Contents

## Synopsis

- ns_sls set key value
- ns_sls get key ?default?
- ns_sls unset key
- ns_sls array

## Description

The underlying TCP socket used by a NaviServer connection may be kept open and reused for further connections, e.g. during HTTP keep-alive . The ns_sls commands associate data with the socket, which will be available to future connections on that socket. The data is freed automatically when the socket closes.

The data is saved as a string, both for thread safety and to allow easy sharing with C code using the Ns_SlsGetKeyed and Ns_SlsSetKeyed API.

Per-socket data is often used by socket drivers and protocol drivers. It may be tempting to save HTTP session data here, but beware: intermediate HTTP proxies may send multiple, unrelated requests using a single TCP socket. You may be looking for Ns_Cls -- connection local storage.

All the ns_sls commands require an active connection.

## COMMANDS

ns_sls set key value

  Set the string value of an sls key. Any old value will be freed. The new value will be freed automatically when the socket is closed.

ns_sls get key ?default?

  Get the current string value of an sls key. If the key does not exist and no default was given an error is raised, otherwise the default is returned.

ns_sls unset key

  Free the data associated with the given key. A key which doesn't exist is ignored.

ns_sls array

  Return a list of sls keys and values in array-get format.

## EXAMPLES

Log the number of connections handled by a single TCP socket:

        set n [ns_sls get connections 0]
        incr n
        ns_sls set connections $n
        if {$n > 1} {
            ns_log notice connections: $n
        }

Log all the keys and data stored for the socket of the currently active connection:

(NB: Does not include C module data set via the Ns_SlsSet API)

        foreach {k v} [ns_sls array] {
            ns_log notice sls: $k: $v
        }

## See Also

Ns_Cls, Ns_Sls

## Keywords

data, ns_sls, sls, storage