Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also added Instrumentation and a log subscriber If you run ActiveFedora in Rails, it injects a middleware that enables the cache at the beginning of the request. Then if you ever ask for an object from LDP, it caches the response and returns the cached result. At the end of the request the cache is cleared. If you have a list of N objects and you iterate over each to get an associated object (e.g. collection), you'd have N loads. This prevents extra loads if the associated object has already been loaded.
- Loading branch information
Showing
16 changed files
with
263 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module ActiveFedora | ||
class CachingConnection < Ldp::Client | ||
def initialize(host) | ||
super | ||
@cache = {} | ||
@cache_enabled = false | ||
end | ||
|
||
def get(url, options = {}) | ||
if @cache_enabled | ||
cache_resource(url) { super } | ||
else | ||
super | ||
end | ||
end | ||
|
||
def post(*) | ||
clear_cache if @cache_enabled | ||
super | ||
end | ||
|
||
def put(*) | ||
clear_cache if @cache_enabled | ||
super | ||
end | ||
|
||
def patch(*) | ||
clear_cache if @cache_enabled | ||
super | ||
end | ||
|
||
# Enable the cache within the block. | ||
def cache | ||
old, @cache_enabled = @cache_enabled, true | ||
yield | ||
ensure | ||
@cache_enabled = old | ||
clear_cache unless @cache_enabled | ||
end | ||
|
||
def enable_cache! | ||
@cache_enabled = true | ||
end | ||
|
||
def disable_cache! | ||
@cache_enabled = false | ||
end | ||
|
||
# Disable the query cache within the block. | ||
def uncached | ||
old, @cache_enabled = @cache_enabled, false | ||
yield | ||
ensure | ||
@cache_enabled = old | ||
end | ||
|
||
def clear_cache | ||
@cache.clear | ||
end | ||
|
||
private | ||
|
||
def log(url) | ||
ActiveSupport::Notifications.instrument("ldp.active_fedora", | ||
id: url, name: "Load LDP", ldp_service: object_id) { yield } | ||
end | ||
|
||
def cache_resource(url, &block) | ||
result = | ||
if @cache.key?(url) | ||
ActiveSupport::Notifications.instrument("ldp.active_fedora", | ||
id: url, name: "CACHE", ldp_service: object_id) | ||
@cache[url] | ||
else | ||
@cache[url] = log(url) { yield } | ||
end | ||
result.dup | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module ActiveFedora | ||
# = Active Fedora Ldp Cache | ||
class LdpCache | ||
module ClassMethods | ||
# Enable the query cache within the block if Active Fedora is configured. | ||
# If it's not, it will execute the given block. | ||
def cache(&block) | ||
connection = ActiveFedora.fedora.connection | ||
connection.cache(&block) | ||
end | ||
|
||
# Disable the query cache within the block if Active Fedora is configured. | ||
# If it's not, it will execute the given block. | ||
def uncached(&block) | ||
ActiveFedora.fedora.connection.uncached(&block) | ||
end | ||
end | ||
|
||
def initialize(app) | ||
@app = app | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
end | ||
|
||
def call(env) | ||
This comment has been minimized.
Sorry, something went wrong.
awead
Contributor
|
||
ActiveFedora.fedora.connection.enable_cache! | ||
|
||
response = @app.call(env) | ||
response[2] = Rack::BodyProxy.new(response[2]) do | ||
reset_cache_settings | ||
end | ||
|
||
response | ||
rescue Exception => e | ||
reset_cache_settings | ||
raise e | ||
end | ||
|
||
private | ||
|
||
def reset_cache_settings | ||
ActiveFedora.fedora.connection.clear_cache | ||
ActiveFedora.fedora.connection.disable_cache! | ||
end | ||
|
||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module ActiveFedora | ||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
|
||
def initialize | ||
super | ||
@odd = false | ||
end | ||
|
||
def ldp(event) | ||
return unless logger.debug? | ||
|
||
payload = event.payload | ||
|
||
name = "#{payload[:name]} (#{event.duration.round(1)}ms)" | ||
id = payload[:id] || "[no id]" | ||
|
||
if odd? | ||
name = color(name, CYAN, true) | ||
id = color(id, nil, true) | ||
else | ||
name = color(name, MAGENTA, true) | ||
end | ||
|
||
debug " #{name} #{id} Service: #{payload[:ldp_service]}" | ||
end | ||
|
||
def odd? | ||
@odd = !@odd | ||
end | ||
|
||
def logger | ||
ActiveFedora::Base.logger | ||
end | ||
end | ||
end | ||
|
||
ActiveFedora::LogSubscriber.attach_to :active_fedora | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
|
||
describe "Caching" do | ||
before do | ||
class TestClass < ActiveFedora::Base | ||
property :title, predicate: ::RDF::DC.title | ||
end | ||
end | ||
|
||
after { Object.send(:remove_const, :TestClass) } | ||
|
||
let!(:object) { TestClass.create(id: '123') } | ||
|
||
describe "#cache" do | ||
it "should find records in the cache" do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).once.and_call_original | ||
ActiveFedora::Base.cache do | ||
o1 = TestClass.find(object.id) | ||
o2 = TestClass.find(object.id) | ||
expect(o1.ldp_source.get.body.object_id).to eq o2.ldp_source.get.body.object_id | ||
end | ||
end | ||
|
||
it "should clear the cache at the end of the block" do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).twice.and_call_original | ||
ActiveFedora::Base.cache do | ||
TestClass.find(object.id) | ||
end | ||
ActiveFedora::Base.cache do | ||
TestClass.find(object.id) | ||
end | ||
end | ||
|
||
context "an update" do | ||
it "should flush the cache" do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).twice.and_call_original | ||
ActiveFedora::Base.cache do | ||
TestClass.find(object.id) | ||
object.title= ['foo'] | ||
object.save! | ||
TestClass.find(object.id) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "#uncached" do | ||
it "should not use the cache" do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).twice.and_call_original | ||
ActiveFedora::Base.cache do | ||
TestClass.find(object.id) | ||
ActiveFedora::Base.uncached do | ||
TestClass.find(object.id) | ||
end | ||
TestClass.find(object.id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
describe ActiveFedora::LdpResource do | ||
let(:obj) { ActiveFedora::Base.create! } | ||
let!(:r1) { ActiveFedora::LdpResource.new(ActiveFedora.fedora.connection, obj.uri) } | ||
let!(:r2) { ActiveFedora::LdpResource.new(ActiveFedora.fedora.connection, obj.uri) } | ||
|
||
it "should cache requests" do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).once.and_call_original | ||
ActiveFedora::Base.cache do | ||
r1.get | ||
r2.get | ||
end | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is this an attr_accessor from another class?