Index: openacs-4/packages/acs-tcl/tcl/memoize-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/memoize-procs.tcl,v diff -u -r1.16 -r1.17 --- openacs-4/packages/acs-tcl/tcl/memoize-procs.tcl 1 Dec 2017 18:21:25 -0000 1.16 +++ openacs-4/packages/acs-tcl/tcl/memoize-procs.tcl 19 Jan 2018 19:47:13 -0000 1.17 @@ -35,81 +35,81 @@ ad_proc -public util_memoize {script {max_age ""}} { - If script has been executed before, return the value it - returned last time, unless it was more than max_age seconds ago. - -

Otherwise, evaluate script and cache and return the - result. - -

Note: script is not evaluated with uplevel. - - @param script A Tcl script whose value should be memoized. May be - best to pass this as a list, e.g. [list someproc $arg1 $arg2]. - - @param max_age The maximum age in seconds for the cached value of - script. If the cached value is older than max_age - seconds, script will be re-executed. - - @return The possibly-cached value returned by script. + If script has been executed before, return the value it + returned last time, unless it was more than max_age seconds ago. + +

Otherwise, evaluate script and cache and return the + result. + +

Note: script is not evaluated with uplevel. + + @param script A Tcl script whose value should be memoized. May be + best to pass this as a list, e.g. [list someproc $arg1 $arg2]. + + @param max_age The maximum age in seconds for the cached value of + script. If the cached value is older than max_age + seconds, script will be re-executed. + + @return The possibly-cached value returned by script. } { if {$max_age ne ""} { set max_age "-expires $max_age" } ns_cache_eval {*}$max_age -- util_memoize $script {*}$script } - + # In case, the definition of the function has cached something, # drop this as well. catch {ns_cache_flush util_memoize} ad_proc -public util_memoize_seed {script value {max_age ""}} { - Pretend util_memoize was called with script and - it returned value. Cache value, replacing any - previous cache entry for script. - -

If clustering is enabled, this command flushes script's - value from the caches on all servers in the cluster before storing - the new value. The new value is only stored in the local cache. - - @param script A Tcl script that presumably would return - value. - - @param value The value to cache for script. - - @param max_age Not used. + Pretend util_memoize was called with script and + it returned value. Cache value, replacing any + previous cache entry for script. + +

If clustering is enabled, this command flushes script's + value from the caches on all servers in the cluster before storing + the new value. The new value is only stored in the local cache. + + @param script A Tcl script that presumably would return + value. + + @param value The value to cache for script. + + @param max_age Not used. } { ns_cache_eval -force util_memoize $script [list set _ $value] - } + } ad_proc -public util_memoize_cached_p {script {max_age ""}} { - Check whether script's value has been cached, and whether it - was cached no more than max_age seconds ago. - - @param script A Tcl script. + Check whether script's value has been cached, and whether it + was cached no more than max_age seconds ago. - @param max_age Maximum age of cached value in seconds. - - @return Boolean value. + @param script A Tcl script. + + @param max_age Maximum age of cached value in seconds. + + @return Boolean value. } { - if {$max_age ne ""} { - ns_log Warning "util_memoize_cached_p: ignore max_age $max_age for $script" - } + if {$max_age ne ""} { + ns_log Warning "util_memoize_cached_p: ignore max_age $max_age for $script" + } return [expr {[ns_cache_keys util_memoize $script] ne ""}] } ad_proc -public util_memoize_flush_pattern { - -log:boolean - pattern + -log:boolean + pattern } { - Loop through all cached scripts, flushing all that match the - pattern that was passed in. - - @param pattern Match pattern (glob pattern like in 'string match $pattern ...'). - @param log Whether to log keys checked and flushed (useful for debugging). - + Loop through all cached scripts, flushing all that match the + pattern that was passed in. + + @param pattern Match pattern (glob pattern like in 'string match $pattern ...'). + @param log Whether to log keys checked and flushed (useful for debugging). + } { set nr_flushed [ns_cache_flush -glob util_memoize $pattern] if {$log_p} { @@ -119,153 +119,153 @@ } else { # - # "Classical" implementation of util_memoize for AOLServer + # "Classical" implementation of util_memoize for AOLServer # with script-level expire handling # ad_proc -public util_memoize {script {max_age ""}} { - If script has been executed before, return the value it - returned last time, unless it was more than max_age seconds ago. + If script has been executed before, return the value it + returned last time, unless it was more than max_age seconds ago. -

Otherwise, evaluate script and cache and return the - result. +

Otherwise, evaluate script and cache and return the + result. -

Note: script is not evaluated with uplevel. +

Note: script is not evaluated with uplevel. - @param script A Tcl script whose value should be memoized. May be - best to pass this as a list, e.g. [list someproc $arg1 $arg2]. + @param script A Tcl script whose value should be memoized. May be + best to pass this as a list, e.g. [list someproc $arg1 $arg2]. - @param max_age The maximum age in seconds for the cached value of - script. If the cached value is older than max_age - seconds, script will be re-executed. + @param max_age The maximum age in seconds for the cached value of + script. If the cached value is older than max_age + seconds, script will be re-executed. - @return The possibly-cached value returned by script. + @return The possibly-cached value returned by script. } { - if {$max_age ne "" && $max_age < 0} { - error "max_age must not be negative" - } + if {$max_age ne "" && $max_age < 0} { + error "max_age must not be negative" + } - set current_time [ns_time] + set current_time [ns_time] - set cached_p [ns_cache get util_memoize $script pair] + set cached_p [ns_cache get util_memoize $script pair] - if {$cached_p && $max_age ne "" } { - set cache_time [lindex $pair 0] - if {$current_time - $cache_time > $max_age} { - ns_cache flush util_memoize $script - set cached_p 0 - } - } + if {$cached_p && $max_age ne "" } { + set cache_time [lindex $pair 0] + if {$current_time - $cache_time > $max_age} { + ns_cache flush util_memoize $script + set cached_p 0 + } + } - if {!$cached_p} { - set pair [ns_cache eval util_memoize $script { - list $current_time [eval $script] - }] - } + if {!$cached_p} { + set pair [ns_cache eval util_memoize $script { + list $current_time [eval $script] + }] + } - return [lindex $pair 1] + return [lindex $pair 1] } ad_proc -public util_memoize_seed {script value {max_age ""}} { - Pretend util_memoize was called with script and - it returned value. Cache value, replacing any - previous cache entry for script. + Pretend util_memoize was called with script and + it returned value. Cache value, replacing any + previous cache entry for script. -

If clustering is enabled, this command flushes script's - value from the caches on all servers in the cluster before storing - the new value. The new value is only stored in the local cache. +

If clustering is enabled, this command flushes script's + value from the caches on all servers in the cluster before storing + the new value. The new value is only stored in the local cache. - @param script A Tcl script that presumably would return - value. + @param script A Tcl script that presumably would return + value. - @param value The value to cache for script. + @param value The value to cache for script. - @param max_age Not used. + @param max_age Not used. } { - util_memoize_flush $script + util_memoize_flush $script - ns_cache set util_memoize $script [list [ns_time] $value] + ns_cache set util_memoize $script [list [ns_time] $value] } ad_proc -public util_memoize_cached_p {script {max_age ""}} { - Check whether script's value has been cached, and whether it - was cached no more than max_age seconds ago. + Check whether script's value has been cached, and whether it + was cached no more than max_age seconds ago. - @param script A Tcl script. + @param script A Tcl script. - @param max_age Maximum age of cached value in seconds. + @param max_age Maximum age of cached value in seconds. - @return Boolean value. + @return Boolean value. } { - if {![ns_cache get util_memoize $script pair]} { - return 0 - } + if {![ns_cache get util_memoize $script pair]} { + return 0 + } - if {$max_age eq ""} { - return 1 - } else { - set cache_time [lindex $pair 0] - return [expr {[ns_time] - $cache_time <= $max_age}] - } + if {$max_age eq ""} { + return 1 + } else { + set cache_time [lindex $pair 0] + return [expr {[ns_time] - $cache_time <= $max_age}] + } } ad_proc -public util_memoize_flush_pattern { - -log:boolean - pattern + -log:boolean + pattern } { - Loop through all cached scripts, flushing all that match the - pattern that was passed in. + Loop through all cached scripts, flushing all that match the + pattern that was passed in. - @param pattern Match pattern (glob pattern like in 'string match $pattern'). - @param log Whether to log keys checked and flushed (useful for debugging). + @param pattern Match pattern (glob pattern like in 'string match $pattern'). + @param log Whether to log keys checked and flushed (useful for debugging). } { - foreach name [ns_cache names util_memoize $pattern] { - if {$log_p} { - ns_log Debug "util_memoize_flush_pattern: flushing $name" - } - util_memoize_flush $name - } + foreach name [ns_cache names util_memoize $pattern] { + if {$log_p} { + ns_log Debug "util_memoize_flush_pattern: flushing $name" + } + util_memoize_flush $name + } } } ad_proc -public util_memoize_initialized_p {} { Return 1 if the util_memoize cache has been initialized and is ready to be used and 0 otherwise. - + } - if { [catch {ns_cache set util_memoize __util_memoize_installed_p 1} error] } { # This definition of util_memoize_initialized_p is for loading during bootstrap. - + proc util_memoize_initialized_p {} { - # - # If the cache is not yet created (or some other error is - # raised) the util_memoize cache is not available. - # - if {[catch {ns_cache set util_memoize __util_memoize_installed_p 1} error]} { - return 0 - } - # - # When he call above has succes, the cache is initialized, we - # can rewrite the function in an always succeeding one and - # return success as well. - # - proc ::util_memoize_initialized_p {} { - return 1 - } - return 1 + # + # If the cache is not yet created (or some other error is + # raised) the util_memoize cache is not available. + # + if {[catch {ns_cache set util_memoize __util_memoize_installed_p 1} error]} { + return 0 + } + # + # When he call above has succes, the cache is initialized, we + # can rewrite the function in an always succeeding one and + # return success as well. + # + proc ::util_memoize_initialized_p {} { + return 1 + } + return 1 } } else { proc util_memoize_initialized_p {} { - # - # This definition of util_memoize_initialized_p is just for - # reloading, since at that time the cache is always - # initialized. - # - return 1 + # + # This definition of util_memoize_initialized_p is just for + # reloading, since at that time the cache is always + # initialized. + # + return 1 } } @@ -274,7 +274,7 @@ Forget any cached value for script. You probably want to use util_memoize_flush to flush the caches on all servers in the cluster, in case clustering is enabled. - + @param script The Tcl script whose cached value should be flushed. } { ns_cache flush util_memoize $script @@ -289,24 +289,24 @@ It is recommended to use util_memoize_flush_pattern whenever possible, since glob-match is in most cases sufficient and much - better performancewise. the glob match can be better supported by + better performance-wise. the glob match can be better supported by the built-in set of the server. - + @see util_memoize_flush_pattern - + @param expr The regular expression to match. @param log Whether to log keys checked and flushed (useful for debugging). } { foreach name [ns_cache names util_memoize] { - if {$log_p} { - ns_log Debug "util_memoize_flush_regexp: checking $name for $expr" - } - if { [regexp $expr $name] } { - if {$log_p} { - ns_log Debug "util_memoize_flush_regexp: flushing $name" - } - util_memoize_flush $name - } + if {$log_p} { + ns_log Debug "util_memoize_flush_regexp: checking $name for $expr" + } + if { [regexp $expr $name] } { + if {$log_p} { + ns_log Debug "util_memoize_flush_regexp: flushing $name" + } + util_memoize_flush $name + } } }