Skip to content

Commit ac6d411

Browse files
committed
add microseconds to key of online users
1 parent fea5a28 commit ac6d411

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: php
22

33
php:
44
- 7.1
5+
- 7.2
56

67
sudo: false
78

docs/tests.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ $config['databases']['redis']['hostname'] = 'redis';
107107
language: php
108108

109109
php:
110-
- 7.0
111110
- 7.1
111+
- 7.2
112112

113113
sudo: false
114114

@@ -186,9 +186,9 @@ PHPStorm, попробуйте её.
186186
version: '2'
187187
services:
188188
php:
189-
image: bscheshir/codeception:php7.1.9-fpm-yii2 #contain phpunit
189+
image: bscheshir/codeception:php7.2.1-fpm-alpine-yii2 #contain phpunit
190190
volumes:
191-
- ..:/project #src and tests shared to container
191+
- ..:/var/www/html #src and tests shared to container
192192
- ~/.composer/cache:/root/.composer/cache
193193
environment:
194194
TZ: Europe/Moscow
@@ -197,7 +197,7 @@ services:
197197
depends_on:
198198
- redis
199199
redis:
200-
image: redis:4.0.1-alpine
200+
image: redis:4.0.7-alpine
201201
restart: always
202202
ports:
203203
- "6379"
@@ -271,15 +271,15 @@ $ docker-compose -f ./tests/docker-compose.yml run --rm --entrypoint composer ph
271271

272272
Например, вывод может выглядить так:
273273
```sh
274-
docker-compose://[/home/dev/projects/yii2-redis-session/tests/docker-compose.yml]:php/php /repo/vendor/phpunit/phpunit/phpunit --configuration /project/phpunit.xml.dist --teamcity
275-
Testing started at 13:58 ...
276-
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
274+
Testing started at 13:10 ...
275+
docker-compose://[/home/dev/projects/yii2-redis-session/tests/docker-compose.yml]:php/php /repo/vendor/phpunit/phpunit/phpunit --configuration /var/www/html/phpunit.xml.dist --teamcity
276+
PHPUnit 6.5.1 by Sebastian Bergmann and contributors.
277277
278278
279279
280-
Time: 76 ms, Memory: 6.00MB
280+
Time: 108 ms, Memory: 8.00MB
281281
282-
OK (24 tests, 48 assertions)
282+
OK (26 tests, 52 assertions)
283283
284284
Process finished with exit code 0
285285
```

src/Session.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function writeSession($id, $data)
5454
if ($result && ($userIdentity = \Yii::$app->user->getIdentity(false) ?? false)) {
5555
$userIdentityId = $userIdentity->getId() ?? 0;
5656
$key = $this->keyUser($userIdentityId);
57-
$result = (bool) $this->redis->zadd($key, 'CH', time(), $id);
57+
$result = (bool) $this->redis->zadd($key, 'CH', $this->time(), $id);
5858
$result = $result && (bool) $this->redis->set($this->keySession($id), $userIdentityId);
5959
}
6060

@@ -105,8 +105,8 @@ public function removeExpired()
105105
if ($keys = $this->redis->keys($this->maskUser())) {
106106
foreach ($keys ?? [] as $key) {
107107
$this->redis->multi();
108-
$this->redis->zrangebyscore($key, '-inf', time() - $this->getTimeout());
109-
$this->redis->zremrangebyscore($key, '-inf', time() - $this->getTimeout());
108+
$this->redis->zrangebyscore($key, '-inf', $this->expiredTime());
109+
$this->redis->zremrangebyscore($key, '-inf', $this->expiredTime());
110110
$exec = $this->redis->exec();
111111
if ($sessionIds = $exec[0]) {
112112
$this->redis->del(...array_map(function ($sessionId){
@@ -168,6 +168,30 @@ public function getActiveSessions(): array
168168
return $sessions;
169169
}
170170

171+
/**
172+
* Get the timestamp of the start of the request with microsecond precision.
173+
*/
174+
public function time()
175+
{
176+
return $_SERVER["REQUEST_TIME_FLOAT"];
177+
}
178+
179+
/**
180+
* Get the timestamp of the expiry of session
181+
* @param bool $cached save first-time calculated value
182+
* @return float
183+
*/
184+
public function expiredTime($cached = true)
185+
{
186+
if ($cached) {
187+
return $this->expiredTimeCache ?: $this->expiredTimeCache = $this->time() - $this->getTimeout();
188+
}
189+
190+
return $this->time() - $this->getTimeout();
191+
}
192+
193+
protected $expiredTimeCache = null;
194+
171195
/**
172196
* Get internal key for store/read a sorted list of sessions
173197
* @param int $userIdentityId user identity id
@@ -235,10 +259,10 @@ protected function prefixSession(): string
235259
private function getSessionIdsByKey(string $key, bool $withScores = false): array
236260
{
237261
if ($withScores) {
238-
return $this->redis->zrevrangebyscore($key, time(), time() - $this->getTimeout(), 'WITHSCORES');
262+
return $this->redis->zrevrangebyscore($key, $this->time(), $this->expiredTime(), 'WITHSCORES');
239263
}
240264

241-
return $this->redis->zrevrangebyscore($key, time(), time() - $this->getTimeout());
265+
return $this->redis->zrevrangebyscore($key, $this->time(), $this->expiredTime());
242266
}
243267

244268
/**

tests/SessionTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public function sessionDataProvider()
7474
];
7575
}
7676

77+
public function testTime()
78+
{
79+
$this->mockWebApplication();
80+
$session = \Yii::$app->session;
81+
$this->assertEquals($_SERVER["REQUEST_TIME_FLOAT"], $session->time());
82+
}
83+
84+
public function testExpiredTime()
85+
{
86+
$this->mockWebApplication();
87+
$session = \Yii::$app->session;
88+
$this->assertEquals($_SERVER["REQUEST_TIME_FLOAT"] - $session->getTimeout(), $session->expiredTime(), 0.0000004);
89+
$expired = [$session->expiredTime(false), $session->expiredTime(false)];
90+
$this->assertEquals($expired[0], $expired[1], 0.0000004);
91+
}
92+
7793
/**
7894
* Test write session
7995
* @dataProvider sessionDataProvider
@@ -138,7 +154,7 @@ public function testGetOnlineUsers()
138154
/** @var \bscheshirwork\redis\Session $session */
139155
$session = \Yii::$app->session;
140156
$this->assertTrue($session->writeSession($sessionId, $sessionData));
141-
$time[$key] = time();
157+
$time[$key] = $_SERVER["REQUEST_TIME_FLOAT"];
142158
}
143159
}
144160

tests/docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
version: '2'
77
services:
88
php:
9-
image: bscheshir/codeception:php7.1.9-fpm-yii2 #contain phpunit
9+
image: bscheshir/codeception:php7.2.1-fpm-alpine-yii2 #contain phpunit
1010
volumes:
11-
- ..:/project #src and tests shared to container
11+
- ..:/var/www/html #src and tests shared to container
1212
- ~/.composer/cache:/root/.composer/cache
1313
environment:
1414
TZ: Europe/Moscow
@@ -17,7 +17,7 @@ services:
1717
depends_on:
1818
- redis
1919
redis:
20-
image: redis:4.0.1-alpine
20+
image: redis:4.0.7-alpine
2121
restart: always
2222
ports:
2323
- "6379"

0 commit comments

Comments
 (0)