Skip to content

Commit

Permalink
fixed: slow query in counter check
Browse files Browse the repository at this point in the history
Moved logic to session and no longer check database for viewed entities
  • Loading branch information
jeabakker committed Sep 17, 2018
1 parent be68a83 commit 07c481c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions views/default/entity_view_counter/extends/counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@

// check if we didn't already view this entity
// views are locked by session id
$session_id = session_id();
$session = elgg_get_session();

$existing_annotations = elgg_get_entities_from_annotations([
'guid' => $entity->guid,
'annotation_name' => ENTITY_VIEW_COUNTER_ANNOTATION_NAME,
'annotation_value' => $session_id,
'count' => true,
]);
$viewed_guids = $session->get('entity_view_counter', []);
if (!is_array($viewed_guids)) {
$viewed_guids = [];
}

if ($existing_annotations) {
if (in_array($entity->guid, $viewed_guids)) {
return;
}

// write to session, for speed
$viewed_guids[] = $entity->guid;
$session->set('entity_view_counter', $viewed_guids);

$session_id = $session->getId();

// log the user who is viewing
// if no logged in user, log by entity
$owner_guid = elgg_get_logged_in_user_guid() ?: $entity->guid;
Expand Down

0 comments on commit 07c481c

Please sign in to comment.