Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Document#cache_key for ActionView::Helpers::CacheHelper#cache #149

Closed
wants to merge 3 commits into from
Closed

Add Document#cache_key for ActionView::Helpers::CacheHelper#cache #149

wants to merge 3 commits into from

Conversation

seivan
Copy link
Contributor

@seivan seivan commented Jun 29, 2015

This is usually called inside a cache() block on the view
<% cache(@Profile) do %>

Will append different values on the plural model name if
new_record? - will append /new
updated_at - will append /id-updated_at.to_s(:number)
No timestamps - will append /id

This is usually called inside a cache() block on the view

<% cache(@Profile) do %>

Will append different values on the plural model name if
new_record?   - will append /new
updated_at    - will append /id-updated_at.to_s(:number)
No timestamps - will append /id
@seivan
Copy link
Contributor Author

seivan commented Jun 29, 2015

I was wondering if the user would like to use other time fields as the cache key.

Possible solutions:

  • Pass an array with other Time fields and pick max()
  • Leave it as it is, and let the user override cache_key themselves with whatever they want.

@nviennot
Copy link
Collaborator

Hi Seivan :)

Few comments:

  1. Let's put that method in the PrimaryKey module.

  2. Let's use self.class.table_name instead of self.class.model_name.cache_key. Since the table name is configurable, two model with the same id with different table_names should not share the same cache key

  3. updated_at.to_i and updated_at.utc.to_iyield the same values, so you can remove .utc.

  4. The distinction with .new_record? doesn't seem useful as we always have an id, even before the record is created (unlike ActiveRecord).

  5. The convention for key separator is to use : in NoBrainer, as it seems to be the standard with redis.

So how about we use something like this:

  def cache_key
    if updated_at = _read_attribute(:updated_at)
      "#{self.class.table_name}:#{pk_value}-#{updated_at.to_i}"
    else
      "#{self.class.table_name}:#{pk_value}"
    end
  end

Thanks for the pull request :)

Nico

@seivan
Copy link
Contributor Author

seivan commented Jun 29, 2015

@nviennot Amazing feedback, I appreciate it. I'll update shortly!

@nviennot nviennot closed this in 10b2fc3 Jul 2, 2015
@nviennot
Copy link
Collaborator

nviennot commented Jul 2, 2015

I've ended up implementing the feature with little code, but I've maintained your authorship on the commit due to the initial work :)

Thanks,
Nico.

@seivan
Copy link
Contributor Author

seivan commented Jul 2, 2015

@nviennot No worries, but uhm I actually found a couple of things about your feedback that isn't valid. You're still interested, or we're done here?

@nviennot
Copy link
Collaborator

nviennot commented Jul 2, 2015

Yes please!! (I am interested)
Btw, if you look at the implementation, it's different from my initial feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants