Skip to content
Browse files

Added ShortTermMemory (cache) mixin

  • Loading branch information...
1 parent 4dc8791 commit f3540212ad9377e8f91eb8aa3b053481177a055f @kmees kmees committed Jan 22, 2013
View
2 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/iron_control.rb
@@ -2,12 +2,14 @@
require 'iron_sharepoint/mixins/type_registration'
require 'iron_sharepoint/mixins/control_view'
require 'iron_sharepoint/mixins/parent_attributes'
+require 'iron_sharepoint/mixins/short_term_memory'
module IronSharePoint
class IronControl
include Mixins::Logging
include Mixins::ControlView
include Mixins::ParentAttributes
+ extend Mixins::ShortTermMemory
def self.inherited child
child.send :include, Mixins::TypeRegistration
View
143 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/mixins/short_term_memory.rb
@@ -0,0 +1,143 @@
+module IronSharePoint::Mixins
+ module ShortTermMemory
+ include IronSharePoint::Mixins::Logging
+
+ DEFAULT_CACHE_SETTINGS = {:expiration_time => 900, :anonymous_only => true}
+
+ def cache_settings settings = nil
+ if settings.nil?
+ @cache_settings || DEFAULT_CACHE_SETTINGS
+ else
+ @cache_settings = cache_settings.merge settings
+ end
+ end
+
+ # def remember_all
+ # prime_cache ".*"
+ # end
+
+ def forget_all
+ flush_cache ".*"
+ end
+
+ # def prime_cache(*syms)
+ # syms.each do |sym|
+ # methods.each do |m|
+ # if m.to_s =~ /^_unmemoized_(#{sym})/
+ # if method(m).arity == 0
+ # __send__($1)
+ # else
+ # ivar = IronSharePoint::Mixins::ShortTermMemory.memoized_ivar_for($1)
+ # instance_variable_set(ivar, {})
+ # end
+ # end
+ # end
+ # end
+ # end
+
+ def flush_cache(*syms)
+ syms.each do |sym|
+ (methods + private_methods + protected_methods).each do |m|
+ if m.to_s =~ /^_unremembered_(#{sym.to_s.gsub(/\?\Z/, '\?')})/
+ cache_key = cache_key_for $1
+ invalidate cache_key
+ end
+ end
+ end
+ end
+
+ def remember(*symbols)
+ symbols.each do |symbol|
+ original_method = :"_unremembered_#{symbol}"
+ cache_key = cache_key_for symbol
+
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
+ if method_defined?(:#{original_method})
+ raise "Already remembered #{symbol}"
+ end
+ alias #{original_method} #{symbol}
+
+ if instance_method(:#{symbol}).arity == 0
+ def #{symbol}(reload = false)
+ result = self.class.fetch '#{cache_key}'
+ if reload || result.nil?
+ result = #{original_method}
+ self.class.store '#{cache_key}', result
+ end
+ return result
+ end
+ else
+ def #{symbol}(*args)
+ args_length = method(:#{original_method}).arity
+ if args.length == args_length + 1 &&
+ (args.last == true || args.last == :reload)
+ reload = args.pop
+ end
+
+ result = self.class.fetch '#{cache_key}', args
+ if reload || result.nil?
+ result = #{original_method}(*args)
+ self.class.store '#{cache_key}', result, args
+ end
+ return result
+ end
+ end
+ EOS
+ end
+ end
+
+ def store key, data, args = []
+ if http_cache
+ hash = http_cache[key] || {}
+ hash[args] = data
+ http_cache.insert key, hash, nil,
+ System::DateTime.utc_now.add_seconds(cache_settings[:expiration_time]),
+ System::Web::Caching::Cache.no_sliding_expiration
+ logger.debug "Remembered #{key} in HttpCache"
+ elsif http_context
+ hash = http_context.items[key] || {}
+ hash[args] = data
+ http_context.items[key] = hash
+ logger.debug "Remembered #{key} in HttpContext"
+ end
+ end
+
+ def fetch key, args = []
+ if http_cache
+ (http_cache[key] || {})[args]
+ elsif http_context
+ (http_context.items[key] || {})[args]
+ end
+ end
+
+ def invalidate key
+ if http_cache
+ http_cache.remove key
+ logger.debug "Invalidated #{key} in HttpCache"
+ end
+ end
+
+ protected
+
+ def must_cache?
+ !cache_settings.fetch(:anonymous_only, true) || SPContext.current.web.current_user.nil?
+ end
+
+ def cache_key_for symbol
+ variation = IronSharePoint::Variation.current
+ class_name = self.is_a?(Class) ? self.name : self.class.name
+
+ "#{variation}_#{class_name}##{symbol}"
+ end
+
+ def http_cache
+ if http_context && must_cache?
+ http_context.cache
+ end
+ end
+
+ def http_context
+ System::Web::HttpContext.current
+ end
+ end
+end

0 comments on commit f354021

Please sign in to comment.
Something went wrong with that request. Please try again.