Class: OAI::Provider::ActiveRecordCachingWrapper

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

Overview

OAI::Provider::ActiveRecordCachingWrapper

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

Instance Method Summary collapse

Methods inherited from ActiveRecordWrapper

#deleted?, #earliest, #last_id, #latest, #sets

Methods inherited from Model

#deleted?, #earliest, #last_id, #latest, #sets

Constructor Details

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

Returns a new instance of ActiveRecordCachingWrapper.



53
54
55
56
# File 'lib/oai/provider/model/activerecord_caching_wrapper.rb', line 53

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

Instance Attribute Details

#expireObject (readonly)

Returns the value of attribute expire.



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

def expire
  @expire
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#timestamp_fieldObject (readonly)

Returns the value of attribute timestamp_field.



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

def timestamp_field
  @timestamp_field
end

Instance Method Details

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



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

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.count(:id, :conditions => conditions)
    if @limit && total > @limit
      select_partial(
        ResumptionToken.new(last_id(conditions), options.merge({:last => 0}), nil, total))
    else
      model.find(:all, :conditions => conditions)
    end
  else
    model.find(selector, :conditions => conditions)
  end
end