In my web application I have a function that is executed more than 100 times in a minute, and I'm trying to find a better performing approach.
public static function removeDuplicateFeeds($id, $feeds, $todo)
{
$actionsHistory = ActionsHistory::whereAccountId($id)->whereActionName($todo)->get();
if (!empty($actionsHistory)) {
foreach ($actionsHistory as $history) {
foreach ($feeds as $key => $feed) {
if ($history->action_name == $feed['pk']) {
unset($feeds[$key]);
}
}
}
}
}
I want to remove all the elements from $feeds
that are in $actionsHistory
as well.
UPDATE:
in this test code first index of $feeds
array as "pk":"7853740779"
is stored on my database and after remove duplicate this item should be removed, but i have all of $feeds
items into $filteredFeeds
too
$userAccountSource = InstagramAccount::with('user', 'schedule')->get();
$feeds = [
json_decode('{"pk":"7853740779","username":"teachkidss","full_name":"..."}'),
json_decode('{"pk":"7853740709","username":"teachkidss","full_name":"..."}'),
json_decode('{"pk":"7853740009","username":"teachkidss","full_name":"..."}')
];
$filteredFeeds = AnalyzeInstagramPageController::removeDuplicateFeeds($userAccountSource[0]->id, $feeds, 'like');
public function removeDuplicateFeeds($id, $feeds, $todo)
{
$feeds = collect($feeds); // If $feeds is not already a collection
$actionsHistory = ActionsHistory::whereAccountId($id)
->whereActionName($todo)
->whereIn('action_name', $feeds->pluck('pk')) // retrieves only duplicate records
->select('action_name') // reducing the select improves performance
->get(); // Should return a eloquent collection instance
if (!empty($actionsHistory)) {
return $feeds->whereNotIn('pk', $actionsHistory->pluck('action_name'));
}
return $feeds;
}