Skip to content

Commit 975765e

Browse files
committed
Added a way to define expiry inside counter.
1 parent f55baf2 commit 975765e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@ if ($counter->value() < 100) {
4040
}
4141
```
4242

43-
If you need use self expiring counter(flag?), you can set third
44-
parameter in the constructor:
43+
By default it's set to never expire. But if you need to use self
44+
expiring counter(flag?), you can set third parameter in the
45+
constructor:
4546
```php
4647
$counter = new MyCounter($user, $client, 3600); // hour, in this case
4748
```
48-
By default it's set to never expire.
49+
or you can define expiry logic/value inside counter by overriding
50+
`getExpiry()` method, p.ex.:
51+
```php
52+
protected function getExpiry()
53+
{
54+
return 3600; // also hour, but this way it's defined inside counter
55+
// or it could be some logic based on value(s) in $this->item
56+
}
57+
```
58+
4959

5060

5161
*Important note.* You will have to use binary protocol in memcached.

src/AbstractCounter.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function inc($step = 1)
7575
$this->getKey(),
7676
$step,
7777
$this->getInitialValue(),
78-
$this->expiry
78+
$this->getExpiry()
7979
);
8080
}
8181

@@ -90,6 +90,16 @@ protected function getInitialValue()
9090
return 0;
9191
}
9292

93+
/**
94+
* Get expiry value
95+
*
96+
* @return int
97+
*/
98+
protected function getExpiry()
99+
{
100+
return $this->expiry;
101+
}
102+
93103
/**
94104
* Method for building cache key based on $item value
95105
*

0 commit comments

Comments
 (0)