Skip to content

Commit

Permalink
Aggregate all expire results
Browse files Browse the repository at this point in the history
  • Loading branch information
driv3r committed Nov 17, 2022
1 parent f8cf1e3 commit 6b3cdba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 6 additions & 2 deletions lib/identity_cache/cached/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ def fetch(db_key)
end

def expire(record)
all_deleted = true

unless record.send(:was_new_record?)
old_key = old_cache_key(record)
IdentityCache.cache.delete(old_key)
all_deleted = IdentityCache.cache.delete(old_key)
end
unless record.destroyed?
new_key = new_cache_key(record)
if new_key != old_key
IdentityCache.cache.delete(new_key)
all_deleted = IdentityCache.cache.delete(new_key) && all_deleted
end
end

all_deleted
end

def cache_key(index_key)
Expand Down
5 changes: 3 additions & 2 deletions lib/identity_cache/parent_model_expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def lazy_hooks
def expire_parent_caches
parents_to_expire = Set.new
add_parents_to_cache_expiry_set(parents_to_expire)
parents_to_expire.each do |parent|
parent.expire_primary_index if parent.class.primary_cache_index_enabled
parents_to_expire.select! { |parent| parent.class.primary_cache_index_enabled }
parents_to_expire.reduce(true) do |all_expired, parent|
parent.expire_primary_index && all_expired
end
end

Expand Down
12 changes: 8 additions & 4 deletions lib/identity_cache/query_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,17 @@ def cache_fetch_includes
def _run_commit_callbacks
if destroyed? || transaction_changed_attributes.present?
expire_cache
expire_parent_caches
end
super
end

# Invalidate the cache data associated with the record. Returns `true` on success,
# `false` otherwise.
def expire_cache
expire_attribute_indexes.all?
expired_parent_caches = expire_parent_caches
expired_attribute_indexes = expire_attribute_indexes

expired_parent_caches && expired_attribute_indexes
end

# @api private
Expand All @@ -185,9 +187,11 @@ def was_new_record? # :nodoc:

private

# Even if we have problems with some attributes, carry over the results and expire
# all possible attributes without array allocation.
def expire_attribute_indexes # :nodoc:
cache_indexes.map do |cached_attribute|
cached_attribute.expire(self)
cache_indexes.reduce(true) do |all_expired, cached_attribute|
cached_attribute.expire(self) && all_expired
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/identity_cache/with_primary_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module WithPrimaryIndex
include WithoutPrimaryIndex

def expire_cache
expire_primary_index
super
expired_primary_index = expire_primary_index

super && expired_primary_index
end

# @api private
Expand Down

0 comments on commit 6b3cdba

Please sign in to comment.