Skip to content

Commit

Permalink
Provide doctrine cache based storage
Browse files Browse the repository at this point in the history
  • Loading branch information
davedevelopment committed Jul 3, 2013
1 parent 300a270 commit ac380c2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
}],

"require": {
"php": ">=5.3.1",
"php": ">=5.3.1"
},

"suggest": {
"doctrine/cache": "~1.0"
},

Expand Down
76 changes: 76 additions & 0 deletions src/Stiphle/Storage/DoctrineCache.php
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;
}

}



0 comments on commit ac380c2

Please sign in to comment.