Skip to content

Commit 22cb245

Browse files
james-johnston-thumbtackbarryvdh
authored andcommitted
Allow setting Memcached item expiration (php-debugbar#334)
Some users may wish to prevent indefinite accumulation of logged data, especially if the Memcached cluster is shared with other production data.
1 parent a186961 commit 22cb245

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/DebugBar/Storage/MemcachedStorage.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ class MemcachedStorage implements StorageInterface
2121

2222
protected $keyNamespace;
2323

24+
protected $expiration;
25+
2426
/**
2527
* @param Memcached $memcached
28+
* @param string $keyNamespace Namespace for Memcached key names (to avoid conflict with other Memcached users).
29+
* @param int $expiration Expiration for Memcached entries (see Expiration Times in Memcached documentation).
2630
*/
27-
public function __construct(Memcached $memcached, $keyNamespace = 'phpdebugbar')
31+
public function __construct(Memcached $memcached, $keyNamespace = 'phpdebugbar', $expiration = 0)
2832
{
2933
$this->memcached = $memcached;
3034
$this->keyNamespace = $keyNamespace;
35+
$this->expiration = $expiration;
3136
}
3237

3338
/**
@@ -36,9 +41,12 @@ public function __construct(Memcached $memcached, $keyNamespace = 'phpdebugbar')
3641
public function save($id, $data)
3742
{
3843
$key = $this->createKey($id);
39-
$this->memcached->set($key, $data);
44+
$this->memcached->set($key, $data, $this->expiration);
4045
if (!$this->memcached->append($this->keyNamespace, "|$key")) {
41-
$this->memcached->set($this->keyNamespace, $key);
46+
$this->memcached->set($this->keyNamespace, $key, $this->expiration);
47+
} else if ($this->expiration) {
48+
// append doesn't support updating expiration, so do it here:
49+
$this->memcached->touch($this->keyNamespace, $this->expiration);
4250
}
4351
}
4452

0 commit comments

Comments
 (0)