1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
<?php
namespace bandwidthThrottle\tokenBucket\storage;
use bandwidthThrottle\tokenBucket\storage\scope\GlobalScope;
use malkusch\lock\mutex\MemcacheMutex;
final class MemcacheStorage implements Storage, GlobalScope
{
private $memcache;
private $key;
private $mutex;
const PREFIX = "TokenBucket_";
public function __construct($name, \Memcache $memcache)
{
trigger_error("MemcacheStorage has been deprecated in favour of MemcachedStorage.", E_USER_DEPRECATED);
$this->memcache = $memcache;
$this->key = self::PREFIX . $name;
$this->mutex = new MemcacheMutex($name, $memcache);
}
public function bootstrap($microtime)
{
$this->setMicrotime($microtime);
}
public function isBootstrapped()
{
return $this->memcache->get($this->key) !== false;
}
public function remove()
{
if (!$this->memcache->delete($this->key)) {
throw new StorageException("Could not remove microtime.");
}
}
public function setMicrotime($microtime)
{
if (!$this->memcache->set($this->key, $microtime, 0, 0)) {
throw new StorageException("Could not set microtime.");
}
}
public function getMicrotime()
{
$microtime = $this->memcache->get($this->key);
if ($microtime === false) {
throw new StorageException("The key '$this->key' was not found.");
}
return (double) $microtime;
}
public function getMutex()
{
return $this->mutex;
}
public function letMicrotimeUnchanged()
{
}
}