Class: OAI::Provider::ActiveRecordCachingWrapper

Inherits:
ActiveRecordWrapper show all
Defined in:
lib/oai/provider/model/activerecord_caching_wrapper.rb

Overview

This class wraps an ActiveRecord model and delegates all of the record selection/retrieval to the AR model. It accepts options for specifying the update timestamp field, a timeout, and a limit. The limit option is used for doing pagination with resumption tokens. The timeout is used to expire old tokens from the cache. Default timeout is 12 hours.

The difference between ActiveRecordWrapper and this class is how the pagination is accomplished. ActiveRecordWrapper encodes all the information in the token. That approach should work 99% of the time. If you have an extremely active respository you may want to consider the caching wrapper. The caching wrapper takes the entire result set from a request and caches it in another database table, well tables actually. So the result returned to the client will always be internally consistent.

Instance Attribute Summary collapse

Attributes inherited from ActiveRecordWrapper

#identifier_field

Attributes inherited from Model

#identifier_field

Instance Method Summary collapse

Methods inherited from ActiveRecordWrapper

#deleted?, #earliest, #latest, #method_missing, #respond_to?, #sets

Methods inherited from Model

#about, #deleted?, #earliest, #latest, #sets

Constructor Details

#initialize(model, options = {}) ⇒ ActiveRecordCachingWrapper

Returns a new instance of ActiveRecordCachingWrapper.



51
52
53
54
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 51

def initialize(model, options={})
  @expire = options.delete(:timeout) || 12.hours
  super(model, options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OAI::Provider::ActiveRecordWrapper

Instance Attribute Details

#expireObject (readonly)

Returns the value of attribute expire.



49
50
51
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 49

def expire
  @expire
end

#modelObject (readonly)

Returns the value of attribute model.



49
50
51
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 49

def model
  @model
end

#timestamp_fieldObject (readonly)

Returns the value of attribute timestamp_field.



49
50
51
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 49

def timestamp_field
  @timestamp_field
end

Instance Method Details

#find(selector, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 56

def find(selector, options={})
  sweep_cache
  return next_set(options[:resumption_token]) if options[:resumption_token]

  conditions = sql_conditions(options)

  if :all == selector
    total = model.where(conditions).count
    if @limit && total > @limit
      select_partial(
        ResumptionToken.new(options.merge({:last => 0})))
    else
      model.where(conditions)
    end
  else
    model.where(conditions).find(selector)
  end
end