Skip to content

Instantly share code, notes, and snippets.

@bogn
Last active January 4, 2016 09:09
Show Gist options
  • Save bogn/8600648 to your computer and use it in GitHub Desktop.
Save bogn/8600648 to your computer and use it in GitHub Desktop.
Naming is hard
module ItemCaching
extend ActiveSupport::Concern
def self.cache_all_items_grouped_by_group
Rails.cache.fetch(:items_grouped_by_group) do
Item.all.group_by {|i| i.group_id }
end
end
def self.belonging_to_group_from_cache(group_id)
Rails.cache.read(:items_grouped_by_group)[group_id] || []
end
end
@bogn
Copy link
Author

bogn commented Jan 24, 2014

COMMENT IS FOR 1b897ab384
This is a concern that gets mixed into Item. So a call would be

Item.belonging_to_group_from_cache(group.id)

Completely unsatisfied with the method names. The mentioned method returns multiple items but doesn't indicate that by its name for example.

@bogn
Copy link
Author

bogn commented Jan 24, 2014

Item.cached_for_group(group.id) is a good suggestion.

@bogn
Copy link
Author

bogn commented Jan 24, 2014

cached_for_group it is.

@bogn
Copy link
Author

bogn commented Jan 24, 2014

Made the model concern more general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment