-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide doctrine cache based storage
- Loading branch information
1 parent
300a270
commit ac380c2
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,10 @@ | |
}], | ||
|
||
"require": { | ||
"php": ">=5.3.1", | ||
"php": ">=5.3.1" | ||
}, | ||
|
||
"suggest": { | ||
"doctrine/cache": "~1.0" | ||
}, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Stiphle\Storage; | ||
|
||
use Doctrine\Common\Cache\Cache; | ||
|
||
/** | ||
* This file is part of Stiphle | ||
* | ||
* Copyright (c) 2011 Dave Marshall <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
class DoctrineCache implements StorageInterface | ||
{ | ||
protected $lockWaitTimeout = 1000; | ||
protected $lockWaitInterval = 100; | ||
|
||
public function __construct(Cache $cache, $lockWaitTimeout = 1000, $lockWaitInterval = 100) | ||
{ | ||
$this->cache = $cache; | ||
$this->lockWaitTimeout = $lockWaitTimeout; | ||
$this->lockWaitInterval = $lockWaitInterval; | ||
} | ||
|
||
public function setLockWaitTimeout($milliseconds) | ||
{ | ||
$this->lockWaitTimeout = $milliseconds; | ||
return; | ||
} | ||
|
||
public function setSleep($microseconds) | ||
{ | ||
$this->sleep = $microseconds; | ||
return; | ||
} | ||
|
||
public function lock($key) | ||
{ | ||
$key = $key . "::LOCK"; | ||
$start = microtime(true); | ||
while ($this->cache->contains($key)) { | ||
$passed = (microtime(true) - $start) * 1000; | ||
if ($passed > $this->lockWaitTimeout) { | ||
throw new LockWaitTimeoutException(); | ||
} | ||
usleep($this->sleep); | ||
} | ||
$this->cache->save($key, true); | ||
|
||
return; | ||
} | ||
|
||
public function unlock($key) | ||
{ | ||
$key = $key . "::LOCK"; | ||
$this->cache->delete($key); | ||
} | ||
|
||
public function get($key) | ||
{ | ||
return $this->cache->fetch($key); | ||
} | ||
|
||
public function set($key, $value) | ||
{ | ||
$this->cache->save($key, $value); | ||
return; | ||
} | ||
|
||
} | ||
|
||
|
||
|