Skip to content

Commit

Permalink
fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
abdumu committed Apr 4, 2020
1 parent 9bee98f commit 0d0d7ce
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ public function ip($ip)
/**
* list cache key
*/
public function cache($limit = '*', $isLow = false)
public function cache($limit = '*', $isLow = false, $constraints = [])
{
$key = $this->visits.'_lists';

if ($limit == '*') {
return "{$key}:*";
}

return "{$key}:".($isLow ? 'low' : 'top').$limit;
//it might not be that unique but it does the job since not many lists
//will be generated to one key.eloquent
$constraintsPart = count($constraints) ? ':'.substr(sha1(serialize($constraints)), 0, 7) : '';

return "{$key}:".($isLow ? 'low' : 'top').$constraintsPart.$limit;
}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/Traits/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ trait Lists
* Fetch all time trending subjects.
*
* @param int $limit
* @param bool $isLow
* @param array $constraints optional. filter models by attributes (where=[...])
* @return \Illuminate\Support\Collection|array
*/
public function top($limit = 5, $orderByAsc = false)
public function top($limit = 5, $orderByAsc = false, $constraints = [])
{
$cacheKey = $this->keys->cache($limit, $orderByAsc);
if(is_array($orderByAsc)) {
$constraints = $orderByAsc;
$orderByAsc = false;
}

$cacheKey = $this->keys->cache($limit, $orderByAsc, $constraints);

$cachedList = $this->cachedList($limit, $cacheKey);
$visitsIds = $this->getVisitsIds($limit, $this->keys->visits, $orderByAsc);

if($visitsIds === $cachedList->pluck($this->keys->primary)->toArray() && ! $this->fresh) {
return $cachedList;
}

return $this->freshList($cacheKey, $visitsIds);
return $this->freshList($cacheKey, $visitsIds, $constraints);
}


Expand Down Expand Up @@ -69,11 +75,12 @@ protected function getSortedList($name, $limit, $orderByAsc = false, $withValues
* Fetch lowest subjects.
*
* @param int $limit
* @param array $constraints optional
* @return \Illuminate\Support\Collection|array
*/
public function low($limit = 5)
public function low($limit = 5, $constraints = [])
{
return $this->top($limit, true);
return $this->top($limit, true, $constraints);
}


Expand All @@ -95,13 +102,16 @@ protected function getVisitsIds($limit, $visitsKey, $orderByAsc = false)
* @param $visitsIds
* @return mixed
*/
protected function freshList($cacheKey, $visitsIds)
protected function freshList($cacheKey, $visitsIds, $constraints = [])
{
if (count($visitsIds)) {

$this->connection->delete($cacheKey);

return ($this->subject)::whereIn($this->keys->primary, $visitsIds)
->when(count($constraints), function($query) use($constraints) {
return $query->where($constraints);
})
->get()
->sortBy(function ($subject) use ($visitsIds) {
return array_search($subject->{$this->keys->primary}, $visitsIds);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/VisitsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,19 @@ public function it_list_from_cache()

$this->assertNotEquals($fresh2->first(), $cached->first());
}

/**
* @test
*/
public function it_list_filtered_by_constraints()
{
$posts =[];

foreach (['naji', 'fadi', 'hanadi', 'maghi', 'lafi'] as $player) {
$posts[$player] = Post::create(['name' => $player])->fresh();
visits($posts[$player])->forceIncrement(rand(2, 109));
}

$this->assertNotEquals(visits('Awssat\Visits\Tests\Post')->top(5, ['name' => 'naji']), [$posts['naji']]);
}
}

0 comments on commit 0d0d7ce

Please sign in to comment.