DataScript: avi.vs.table_lookup

DataScript

Function avi.vs.table_lookup( [table_name,] key [, lifetime_exten] )
Description The table API is used to store and retrieve custom data. The avi.vs.table_lookup API looks up a key and return the corresponding value. This key-value store is unique per Virtual Service and is mirrored across all Service Engines hosting the VS.
Events HTTP_REQ
HTTP_RESP
Parameter If the optional table_name is not specified, the key is looked up in the default table for the VS. When it is specified, a custom table will be searched.

The key is used to look up the value.

The optional lifetime_exten flag must be a positive integer, indicating the length of time, in seconds, that should be added to the current lifetime parameter for the key. When not specified, the default value of 300 seconds is used. This means that by default, looking up the value of a key will extend the lifetime of a key by an additional 300 seconds. To lookup the key without impacting the expiration, set the lifetime_exten flag to 0.

See Application for more details.

Returns Returns the value field of the key. If no value exists or the key does not exist, returns nil.

The remaining lifetime, a non-negative integer representing the remaining lifetime of the entry in seconds.

HTTP Request Example This use case can also be performed via the native App-Cookie persistence mode.


if avi.http.get_cookie("OAM_JSESSIONID") then
    if avi.vs.table_lookup(avi.http.get_cookie("OAM_JSESSIONID")) then
        avi.pool.select(avi.vs.table_lookup(avi.http.get_cookie("JSESSIONID")))
     end                                                                        
 end
HTTP Response Example

if avi.http.get_cookie("JSESSIONID") ~= "" then
   avi.vs.table_insert(avi.http.get_cookie("JSESSIONID"), avi.pool.server_ip(), 3600)                                                                        
  end

Application

When three parameters are passed to this function, there are two possibilities for its usage:

  • avi.vs.table_lookup(key, lifetime_exten)
  • avi.vs.table_lookup(table_name, key)

Users disambiguate between the two scenarios by the typings of the arguments as follows:

  • If both arguments to the function are strings, then it is avi.vs.table_lookup(table_name, key).
  • If the 2nd argument is a positive integer, then it is avi.vs.table_lookup(key, lifetime_exten).

In order to avoid misconfigurations shown in the below sample DataScript, it is important to understand this disambiguation:

  • See the below command, when avi.vs.table_insert(key, value, lifetime) is used.
    
      inserted = avi.vs.table_insert("foo", "bar", 1)
        
      if inserted then
        avi.vs.log("Insertion SUCCEEDED")
      else
        avi.vs.log("Insertion FAILED")
      end
      
  • See the below command, when avi.vs.table_lookup(table_name, key) is used.
    
      if avi.vs.table_lookup("foo", "bar") == nil then
        avi.vs.log("Element not found")
      end
      

The DataScript inserts key foo in the global table, but lookup is done for the key bar in table foo. Hence, the table lookup fails to find the entry.