Skip to content

Commit

Permalink
Add a cache for label to class mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Mar 1, 2015
1 parent b641db0 commit 6ec01c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lib/neo4j/active_node/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Labels

WRAPPED_MODELS = []

MODELS_FOR_LABELS_CACHE = ActiveSupport::Cache::MemoryStore.new

included do |model|
def self.inherited(model)
add_wrapped_class(model)
Expand Down Expand Up @@ -55,13 +57,15 @@ def self._wrapped_classes
end

def self.model_for_labels(labels)
models = WRAPPED_MODELS.select do |model|
(model.mapped_label_names - labels).size == 0
end
MODELS_FOR_LABELS_CACHE.fetch(labels.to_set.hash) do
models = WRAPPED_MODELS.select do |model|
(model.mapped_label_names - labels).size == 0
end

models.sort_by do |model|
(model.mapped_label_names & labels).size
end.last
models.sort_by do |model|
(model.mapped_label_names & labels).size
end.last
end
end

protected
Expand Down
3 changes: 1 addition & 2 deletions lib/neo4j/active_node/node_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def wrapper
def class_to_wrap
load_classes_from_labels

named_class ||
::Neo4j::ActiveNode::Labels.model_for_labels(labels)
named_class || ::Neo4j::ActiveNode::Labels.model_for_labels(labels)
end

private
Expand Down

0 comments on commit 6ec01c6

Please sign in to comment.