@@ -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