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 support for manual expiration for after direct DB manipulation #500

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

dylanahsmith
Copy link
Contributor

@dylanahsmith dylanahsmith commented Jun 14, 2021

cc @Larochelle & @Hectorhammett

I've started an alternative to #495 (and the existing expire_primary_key_cache_index class method) since I think we need a way to expire caches that doesn't make assumptions about what caches need to be expired. Otherwise, it is easy to miss caches that the developer using this manual expiration didn't think about (e.g. from embedded associations) or ones added in the future.

This PR adds cache_indexed_columns class method that can be used to get the columns that are needed for cache expiration (see its documentation for details). After that data is queried and put into the form of a hash, it can then be used with expire_cache_for_delete or expire_cache_for_update class methods. I've also added expire_cache_for_insert, which could just be used with the data to insert.

Note that I haven't yet tested or fully documented this code (e.g. we could use a brief mention of this in the REAMDE and link to a new docs/manual_expire.md file with details).


def check_for_unsupported_parent_expiration_entries
return unless parent_expiration_entries.any?
msg = "Unsupported manual expiration of record embedded in parent associations:\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hectorhammett will this be needed for your use case?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not cause any problems with our use.

@dylanahsmith dylanahsmith changed the base branch from master to skip-rubocop-version-with-bug June 14, 2021 13:35
Base automatically changed from skip-rubocop-version-with-bug to master June 14, 2021 20:44
Copy link

@Larochelle Larochelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏🏻

end

def expire_for_update(old_values_hash, changes)
expire_for_values(old_values_hash, changes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expire_for_values expects only one parameter, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, this should be expire_for_values(old_values_hash)


def test_expire_cache_for_insert
id = 1
AssociatedRecord.fetch_name_by_id(id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there no assertion for what value is returned by this? Also, why is this making an assertion for a hard code id?

Shouldn't this test be making the insertion after this using something like insert_all that skips callbacks? It looks like this is loading a record that already exists

id: id,
}

AssociatedRecord.expire_cache_for_delete(expire_hash_keys)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a corresponding deletion in this test, so this isn't really testing deletion.

require "test_helper"

module IdentityCache
class CacheManualExpireTest < IdentityCache::TestCase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test files should be organized around the code it tests as explained in #498

assert_equal(expected_result, AssociatedRecord.cache_indexed_columns)
end

def test_expire_cache_for_udate
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_expire_cache_for_udate
def test_expire_cache_for_update

end
end

def test_expire_cache_for_udate_raises_when_a_hash_is_missing_an_index_key
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_expire_cache_for_udate_raises_when_a_hash_is_missing_an_index_key
def test_expire_cache_for_update_raises_when_a_hash_is_missing_an_index_key

name: "bar",
}

error = assert_raises do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = assert_raises do
error = assert_raises(KeyError) do

Hector Mendoza Jacobo added 2 commits July 12, 2021 16:03
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.

3 participants