Skip to content

Commit

Permalink
fix bug: expiration time reset every incerments
Browse files Browse the repository at this point in the history
  • Loading branch information
BSN4 committed Sep 28, 2017
1 parent 29efa2c commit f35a48e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"require": {
"illuminate/support": "~5.1",
"predis/predis": "^1.1",
"php" : "~5.6|~7.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/BareqServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function boot()
{
$this->publishes([
__DIR__ . '/config/bareq.php' => config_path('bareq.php'),
]);
], 'config');
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public function factory()
public function visits()
{
if ($this->keys->id) {
$this->forceDecrement($this->count());
Redis::zrem($this->keys->visits, $this->keys->id);

foreach ($this->periods as $period) {
Redis::zrem($this->keys->period($period), $this->keys->id);
}

$this->ips();
} else {
Redis::del($this->keys->visits);
Redis::del($this->keys->visits . '_total');
Expand Down
10 changes: 8 additions & 2 deletions src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,14 @@ public function increment($inc = 1, $force = false, $periods = true)
if($periods) {
foreach ($this->periods as $period => $days) {
$periodKey = $this->keys->period($period);
Redis::zincrby($periodKey, $inc, $this->keys->id);
Redis::expire($periodKey, $days * 24 * 60 * 60);

if ( ! Redis::exists($periodKey)) {
Redis::zincrby($periodKey, $inc, $this->keys->id);
Redis::expire($periodKey, $days * 24 * 60 * 60);
} else {
Redis::zincrby($periodKey, $inc, $this->keys->id);
}

Redis::incrby($periodKey . '_total', $inc);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/config/bareq.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
'year' => 365,
],

/*
|--------------------------------------------------------------------------
| Redis prefix
|--------------------------------------------------------------------------
*/
'redis_keys_prefix' => 'bareq',

/*
|--------------------------------------------------------------------------
| Remember ip for x seconds of time
Expand All @@ -36,6 +43,5 @@
*/
'always_fresh' => false,

'redis_keys_prefix' => 'bareq',
];

16 changes: 16 additions & 0 deletions tests/VisitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ function make($class, $times = null, $attributes = [])
return factory($class, $times)->make($attributes);
}

/** @test */
public function dont_reset_time_expiration_every_increment()
{
$post = $this->create('App\Post');

visits($post)->forceIncrement();

$timenow = visits($post)->timeLeft('day')->diffInSeconds();

visits($post)->forceIncrement();

$timethen = visits($post)->timeLeft('day')->diffInSeconds();

$this->assertNotEquals($timenow, $timethen);
}

/**
* @test
*/
Expand Down

0 comments on commit f35a48e

Please sign in to comment.