Forum OpenACS Development: Re: connection-global cache: namespace problem?

Posted by Ola Hansson on
I studied ad_*_client_property and it looks like it could be useful. I had kind of overlooked it, so thanks for mentioning it.

Here is the current version of the "cache exchange" proc in case you want to steal it or comment on it. Serious testing needs to be performed before I trust it, though.

namespace eval curriculum {}

ad_proc -public ::curriculum::conn {
} {
    set flag [lindex $args 0]
    if { [string index $flag 0] != "-" } {
        set var $flag
        set flag "-get"
    } else {
        set var [lindex $args 1]

    set module MyModule

    switch -- $flag {
        -set {
            # Set cache key $var of module $module to some value and return it
            set value [lindex $args 2]
            ad_set_client_property -persistent f $module $var $value
            return $value
        -flush {
            # Flush the cache for key $var in module $module
            util_memoize_flush [list sec_lookup_property \
                                    [ad_conn session_id] $module $var]
        -nocache {
            # Call ourself with the -flush flag to flush the cache
            ::curriculum::conn -flush $var

            # Call ourself with the -get flag and return fresh data
            ::curriculum::conn -get $var
        -get {
            # Get value of key $var in module $module - from query or cache
            switch -- $var {
                name -
                shoesize -
                weight -
                whatever {
                    set value [ad_get_client_property \
                                      -cache_only t -default "" $module $var]

                    if { ![empty_string_p $value] } {
                        return $value

                    set info "Result from query"

                    return [::curriculum::conn -set $var $info]
                default {
                    error "Unknown variable $var"
        default {
            error "::curriculum::conn: unknown flag $flag"