Skip to content

Commit e369537

Browse files
committed
Added touch() and incWithTouch().
1 parent 975765e commit e369537

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ protected function getExpiry()
5555
// or it could be some logic based on value(s) in $this->item
5656
}
5757
```
58+
*NB!* Expiry is not updated on inc() call. It's default Memcached
59+
behaviour. If you need to update expiry use `touch()`, p.ex.:
60+
```php
61+
$counter->inc();
62+
$counter->touch();
63+
// or
64+
$counter->incWithTouch();
65+
```
66+
Second option is more convenient but you loose control over `touch()`
67+
success/fail.
5868

5969

6070

src/AbstractCounter.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function value()
6767
*
6868
* @param int $step
6969
*
70-
* @return int value after increase
70+
* @return int|bool value after increase or false on fail
7171
*/
7272
public function inc($step = 1)
7373
{
@@ -79,6 +79,38 @@ public function inc($step = 1)
7979
);
8080
}
8181

82+
/**
83+
* Update expriration time
84+
*
85+
* @link http://php.net/manual/en/memcached.touch.php
86+
* @return bool true on success
87+
*/
88+
public function touch()
89+
{
90+
return $this->client->touch(
91+
$this->getKey(),
92+
$this->getExpiry()
93+
);
94+
}
95+
96+
/**
97+
* Increase counter and update expiry at the same time.
98+
*
99+
* @param int $step
100+
* @see inc()
101+
*
102+
* @return bool|int new value or false if increase failed
103+
*/
104+
public function incWithTouch($step = 1)
105+
{
106+
$new = $this->inc($step);
107+
if (false !== $new) {
108+
$this->touch();
109+
}
110+
111+
return $new;
112+
}
113+
82114
/**
83115
* Initial counter value
84116
* Could be redefined in descendants

0 commit comments

Comments
 (0)