Class ::acs::Cache (public)
::nx::Class ::acs::Cache
Defined in packages/acs-tcl/tcl/acs-cache-procs.tcl
Base class for cache management
- Testcases:
- No testcase defined.
Source code: :property name :property parameter:required :property package_key:required :property maxentry:integer :property {timeout 5m} :property {default_size 100KB} :method cache_name {key} { # # More or less dummy function, which can be refined. The # base definition completely ignores "key". # return ${:name} } :method get_size {} { # # Determine the cache size depending on configuration # variables. # set specifiedSize [::parameter::get_from_package_key -package_key ${:package_key} -parameter "${:parameter}Size" -default ${:default_size}] if {[::nsf::is integer $specifiedSize]} { set size $specifiedSize } else { set size [ns_baseunit -size $specifiedSize] } return $size } :public method flush {{-partition_key} key} { # # Flush a single entry in the cache # if {![info exists partition_key]} { set partition_key $key } ::acs::clusterwide ns_cache flush [:cache_name $partition_key] $key } :public method eval {{-partition_key} {-expires} {-timeout} {-per_request:switch} key command} { # # Evaluate the command unless the result was already computed before and cached. # # @param expires Lifetime of the cache entry. # The entry will be purged automatically when the time is reached. The time is in seconds # unless a time unit is specified (e.g., 5m) # @param timeout Maximum time to wait for the command to complete. The time is in seconds # unless a time unit is specified (e.g., 2.5m) # @param partition_key Used for determining the cache # name in partitioned caches. The partition key is computed typically # automatically depending on the cache type. # @param per_request When set, cache the result per # request. So far, no attempt is made to flush # the result during the lifetime of the request. # @param key The cache key # @param command The command to be executed when the result is not yet cached. # if {![info exists partition_key]} { set partition_key $key } foreach optional_parameter {expires timeout} { if {[info exists $optional_parameter]} { set ${optional_parameter}_flag [list -$optional_parameter [set $optional_parameter]] } else { set ${optional_parameter}_flag "" } } set cache_name [:cache_name $partition_key] try { if {$per_request} { acs::per_request_cache eval -key ::acs-${cache_name}($key) { :uplevel [list ns_cache_eval {*}$expires_flag {*}$timeout_flag -- $cache_name $key $command] } } else { :uplevel [list ns_cache_eval {*}$expires_flag {*}$timeout_flag -- $cache_name $key $command] } } on break {r} { # # When the command ends with "break", it means: # "don't cache". We return in this case always a # 0. # #ns_log notice "====================== [self] $key -> break -> <$r>" return 0 } on ok {r} { return $r } } :public method set {-partition_key key value} { # # Set a single value in the cache. This code uses # ns_cache_eval to achieve this behavior, which is # typically an AOLserver idiom and should be avoided. # if {![info exists partition_key]} { set partition_key $key } :uplevel [list ns_cache_eval -force -- [:cache_name $partition_key] $key [list set _ $value]] } :public method flush_pattern {{-partition_key ""} pattern} { # # Flush in the cache a value based on a pattern # operation. Use this function rarely, since on large # caches (e.g. 100k entries or more) the glob # operation will cause long locks, which should be # avoided. The partitioned variants can help to reduce # the lock times. # return [::acs::clusterwide ns_cache_flush -glob [:cache_name $partition_key] $pattern] } :method cache_create {name size} { # # Create a cache. # ns_cache_create -timeout ${:timeout} {*}[expr {[info exists :maxentry] ? "-maxentry ${:maxentry}" : ""}] $name $size } :public method get {-partition_key key} { # # The "get" method retrieves data from the cache. It # should not be used for new applications due to likely # race conditions, but legacy applications use this. As # implementation, we use the AOLserver API emulation. # if {![info exists partition_key]} { set partition_key $key } return [ns_cache get [:cache_name $partition_key] $key] } :public method show_all {} { # # Log all cache keys to the system log. The primary usage # is for debugging. # ns_log notice "content of ${:name}: [ns_cache_keys ${:name}]" } :public method flush_cache {{-partition_key ""}} { # # Flush all entries in a cache. # ::acs::clusterwide ns_cache_flush [:cache_name $partition_key] #ns_log notice "flush_all -> ns_cache_flush [:cache_name $partition_key]" #ns_log notice "... content of ${:name}: [ns_cache_keys ${:name}]" } :public method flush_all {} { # # Flush all contents of all (partitioned) caches. In the # case of a base ::acs::Cache, it is identical to # "flush_cash". # :flush_cache } :public method init {} { # # If the name was not provided, use the object name as # default. # if {![info exists :name]} { set :name [namespace tail [current]] } :cache_create ${:name} [:get_size] }XQL Not present: Generic, PostgreSQL, Oracle