NaviServer - programmable web server

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

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

## Name

ns_memoize - Evaluate a Tcl script and cache the result

## Table Of Contents

## Synopsis

- ns_memoize ?-timeout t? ?-expires t? ?--? script ?args ...?
- ns_memoize_flush ?pattern?
- ns_memoize_stats

## Description

A simple wrapper around ns_cache_eval which uses the script to be evaluated as the key to the cached result of evaluating the script. Use this when evaluating the script could take some time.

If the script is not pure, i.e. if it depends on something other than it's arguments to compute the result, then you will need to supply an expirey time or explicitly flush the result when the dependency changes.

## COMMANDS

ns_memoize ?-timeout t? ?-expires t? ?--? script ?args ...?

  Evaluate script and args and return the result. The result is cached so that future evaluations of the same script and args will return the cached result without first evaluating the script.

  The script result remains valid until the supplied expire time passes, or forever if not specified. The value for -expires can be expressed either as an absolute time in the future (large values are interpreted as seconds since the Unix epoch), or as an offset from the current time.

  If two threads execute the same script and args, one will wait for the other to compute the result and store it in the cache. The -timeout option specifies how long to wait.

ns_memoize_flush ?pattern?

  Flush results from the memoize cache. If a pattern is not given, all results are discarded. If a pattern is given it is treated as a globbing pattern and matched against each script for which there is a cached result in turn, and those that match are discarded.

  Note: this may be an expensive operation. First the cache lock is accquired, then all entries in the cache are iterated over in turn matching the script against the globbing pattern. During this period all access to the memoize cache will be blocked.

ns_memoize_stats

  Return a list of statistics about the effectiveness of the memoize cache. Use this information to help you tune the cache size. See ns_cache_stats for details of the format of this list.

## EXAMPLES

For the next three quarters of an hour, the answer to the "meaning of life" will take no time at all to decide:

    set answer [ns_memoize -expires 2520 deep_thought "meaning of life"]

Flush the cache of all thoughts which are deep:

    ns_memoize_flush deep_*

## See Also

ns_cache

## Keywords

cache