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

Posted by Ola Hansson on
Thanks David,

I will try something like the following and see how it works (it seems to work fairly well so far) ...

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 my_array MyArray

    switch -- $flag {
        -set {
            # Set cache key $var to some value and return it
            return [nsv_set $my_array $var [lindex $args 2]]
        -flush {
            # Unset key $var - effectively flushes the cache
            nsv_unset $my_array $var
        -nocache {
            # Unset key $var - effectively flushes the cache
            nsv_unset $my_array $var

            # Call ourself with the -get flag and return fresh data
            ::curriculum::conn -get $var
        -get {
            # Get value of key $var - from query or cache
            if { [nsv_exists $my_array $var] } {
                return "[nsv_get $my_array $var] - CACHED"
            switch -- $var {
                name -
                shoesize -
		weight -
                whatever {
                    return "[nsv_set $my_array $var "$var from an expensive query, say"] - NOT CACHED"
                default {
                    error "Unknown variable $var"
        default {
            error "::curriculum::conn: unknown flag $flag"

Roberto: That's a couple of really good points, I think. We should stick to this advice unless we come up with something better still.

Getting rid of an unnessecary level of indentation is worth gold!

Don: I have checked out ns_cache two weeks ago and a thread-private cache does not work since there are ~5 threads in each connection, I believe. The Global ns_cache is accessible to all threads, which is not what I want (I guess).

I want to cache across interpreters but not _all_ interpreters - only the five or so per connection, no?

I hope there is a smooth way to make caches accessible on a per user basis.

I feel as though I've probably misunderstood some of the interpreter/thread/connection mess (nothing the community couldn't help straighten out, I'm sure) and it's too late in the evening now, anyway, so I'll have to look at this and eventual extra answers in the morning...