# Example - Real Cache

- ## Unfortunately, a real cache is generally more complicated, requiring the ns_cond command as well:

proc cache.get {key} {

set lock [nsv_get cache lock]; set cond [nsv_get cache cond]

ns_mutex lock $lock

if ![nsv_exists cache $key] {

nsv_set cache $key “”

ns_mutex unlock $lock

set value [… fetch data for $key …]

ns_mutex lock $lock

nsv_set cache $key $value

ns_cond broadcast $cond

} else {

while {[set value [nsv_get cache $key]] == “”} {

ns_cond wait $cond $lock

}

}

ns_mutex unlock $lock

return $value

}

Previous slide | Next slide | Back to first slide | View graphic version