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

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants
@seivan
Contributor

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

seivan added some commits Jun 29, 2015

Add Document#cache_key for ActionView::Helpers::CacheHelper#cache
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

This comment has been minimized.

Show comment
Hide comment
@seivan

seivan Jun 29, 2015

Contributor

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.
Contributor

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

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jun 29, 2015

Owner

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

Owner

nviennot commented Jun 29, 2015

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

This comment has been minimized.

Show comment
Hide comment
@seivan

seivan Jun 29, 2015

Contributor

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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jul 2, 2015

Owner

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.

Owner

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

This comment has been minimized.

Show comment
Hide comment
@seivan

seivan Jul 2, 2015

Contributor

@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?

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jul 2, 2015

Owner

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

Owner

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