class PersonSweeper < ActionController::Caching::Sweeper
  observe Person

  def expire_group_members(record)
    record.groups.all.each do |group|
      expire_fragment(:controller => 'groups', :action => 'show', :id => group.id, :fragment => 'members')
    end
  end

  def expire_stream_items(record)
    record.stream_items.all.each do |stream_item|
      stream_item.expire_caches
    end
  end

  def after_save(record)
    expire_group_members(record)
    expire_stream_items(record)
  end

  def after_destroy(record)
    expire_group_members(record)
    expire_stream_items(record)
  end

end

class PersonSweeper < ActionController::Caching::Sweeper
  observe Person

  def expire_group_members(record)
    record.groups.all.each do |group|
      expire_fragment(:controller => 'groups', :action => 'show', :id => group.id, :fragment => 'members')
    end
  end

  def expire_stream_items(record)
    record.stream_items.all.each do |stream_item|
      #stream_item.expire_caches # very inefficient; let's just expire them all instead
      ActionController::Base.cache_store.delete_matched(%r{groups/\d+\?fragment=stream_items})
      ActionController::Base.cache_store.delete_matched(%r{stream\?for=\d+&fragment=stream_items})
    end
  end

  def after_save(record)
    expire_group_members(record)
    expire_stream_items(record)
  end

  def after_destroy(record)
    expire_group_members(record)
    expire_stream_items(record)
  end

end

Expire stream item caches more quickly.
Expire stream item caches more quickly.

This expires all stream item fragments rather than targeted ones, but
gets the user on with their task more quickly. Expiring individual
stream item caches was taking too long.
