Skip to content

Commit 519f6c2

Browse files
authored
Merge pull request #18 from WyriHaximus/atomic-set-expire
Set expire atomicly
2 parents 081f2b7 + 3271605 commit 519f6c2

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/Redis.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ public function set($key, $value, $ttl = null)
6868
});
6969
}
7070

71-
return $this->client->set($this->prefix . $key, $value)->then(function () use ($key, $ttl) {
72-
return $this->client->expire($this->prefix . $key, $this->ttl > 0 ? $this->ttl : $ttl);
73-
})->then(function () {
71+
return $this->client->psetex(
72+
$this->prefix . $key,
73+
($this->ttl > 0 ? $this->ttl : $ttl) * 1000,
74+
$value
75+
)->then(function () {
7476
return resolve(true);
7577
}, function () {
7678
return resolve(false);

tests/ClientStub.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function set(): PromiseInterface
3939
return resolve();
4040
}
4141

42-
public function expire(): PromiseInterface
42+
public function psetex(): PromiseInterface
4343
{
4444
return resolve();
4545
}
@@ -48,4 +48,9 @@ public function del(): PromiseInterface
4848
{
4949
return resolve();
5050
}
51+
52+
public function isBusy()
53+
{
54+
return true;
55+
}
5156
}

tests/RedisTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function testSetGlobalTtl(): void
6262
$key = 'key';
6363
$value = 'value';
6464
$ttl = 123;
65-
$this->client->set($prefix . $key, $value)->shouldBeCalled()->willReturn(new FulfilledPromise());
66-
$this->client->expire($prefix . $key, $ttl)->shouldBeCalled()->willReturn(new FulfilledPromise());
65+
$this->client->psetex($prefix . $key, $ttl * 1000, $value)->shouldBeCalled()->willReturn(new FulfilledPromise());
6766
(new Redis($this->client->reveal(), $prefix, $ttl))->set($key, $value);
6867
}
6968

@@ -73,8 +72,7 @@ public function testSetTtl(): void
7372
$key = 'key';
7473
$value = 'value';
7574
$ttl = 123;
76-
$this->client->set($prefix . $key, $value)->shouldBeCalled()->willReturn(new FulfilledPromise());
77-
$this->client->expire($prefix . $key, $ttl)->shouldBeCalled()->willReturn(new FulfilledPromise());
75+
$this->client->psetex($prefix . $key, $ttl * 1000, $value)->shouldBeCalled()->willReturn(new FulfilledPromise());
7876
(new Redis($this->client->reveal(), $prefix))->set($key, $value, $ttl);
7977
}
8078

0 commit comments

Comments
 (0)