-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from kodedphp/shmop
- Loading branch information
Showing
19 changed files
with
249 additions
and
85 deletions.
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
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
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
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
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,164 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Koded package. | ||
* | ||
* (c) Mihail Binev <[email protected]> | ||
* | ||
* Please view the LICENSE distributed with this source code | ||
* for the full copyright and license information. | ||
* | ||
*/ | ||
|
||
namespace Koded\Caching\Client; | ||
|
||
use Exception; | ||
use Koded\Caching\{Cache, CacheException}; | ||
use function Koded\Caching\verify_key; | ||
|
||
/** | ||
* @property ShmopClient client | ||
* | ||
*/ | ||
final class ShmopClient implements Cache | ||
{ | ||
use ClientTrait, MultiplesTrait; | ||
|
||
/** @var string */ | ||
private $dir; | ||
|
||
public function __construct(string $dir, ?int $ttl) | ||
{ | ||
$this->dir = $dir; | ||
$this->ttl = $ttl; | ||
$this->setDirectory($dir); | ||
} | ||
|
||
|
||
public function get($key, $default = null) | ||
{ | ||
if (false === $this->has($key, $filename)) { | ||
return $default; | ||
} | ||
|
||
try { | ||
$resource = shmop_open(fileinode($filename), 'a', 0, 0); | ||
return unserialize(shmop_read($resource, 0, shmop_size($resource))); | ||
} finally { | ||
shmop_close($resource); | ||
} | ||
} | ||
|
||
|
||
public function set($key, $value, $ttl = null) | ||
{ | ||
verify_key($key); | ||
|
||
if (1 > $expiration = $this->timestampWithGlobalTtl($ttl, Cache::DATE_FAR_FAR_AWAY)) { | ||
// The item is considered expired and must be deleted | ||
return $this->delete($key); | ||
} | ||
|
||
$value = serialize($value); | ||
$size = strlen($value); | ||
$filename = $this->filename($key, true); | ||
|
||
try { | ||
$resource = shmop_open(fileinode($filename), 'n', 0666, $size); | ||
} catch (Exception $e) { | ||
$resource = shmop_open(fileinode($filename), 'w', 0666, $size); | ||
} | ||
|
||
try { | ||
return shmop_write($resource, $value, 0) === $size | ||
&& false !== file_put_contents($filename . '-ttl', $expiration); | ||
|
||
} finally { | ||
shmop_close($resource); | ||
} | ||
} | ||
|
||
|
||
public function delete($key) | ||
{ | ||
if (false === $this->has($key, $filename)) { | ||
return true; | ||
} | ||
|
||
return $this->expire($filename); | ||
} | ||
|
||
|
||
public function clear() | ||
{ | ||
foreach ((glob($this->dir . 'shmop-*.cache*') ?: []) as $filename) { | ||
$this->expire($filename); | ||
} | ||
return true; | ||
} | ||
|
||
|
||
public function has($key, &$filename = '') | ||
{ | ||
verify_key($key); | ||
$filename = $this->filename($key, false); | ||
$expiration = (int)(@file_get_contents($filename . '-ttl') ?: 0); | ||
|
||
if ($expiration <= time()) { | ||
$this->expire($filename); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
private function filename(string $key, bool $create): string | ||
{ | ||
$filename = $this->dir . 'shmop-' . sha1($key) . '.cache'; | ||
|
||
if ($create) { | ||
touch($filename); | ||
touch($filename . '-ttl'); | ||
chmod($filename, 0666); | ||
chmod($filename . '-ttl', 0666); | ||
} | ||
|
||
return $filename; | ||
} | ||
|
||
/** | ||
* Prepares the cache directory. | ||
* | ||
* @param string $directory | ||
* | ||
* @throws CacheException | ||
*/ | ||
private function setDirectory(string $directory): void | ||
{ | ||
// Overrule shell misconfiguration or the web server | ||
umask(umask() | 0002); | ||
$dir = $directory ?: sys_get_temp_dir(); | ||
$dir = rtrim($dir, '/') . '/'; | ||
|
||
if (false === is_dir($dir) && false === mkdir($dir, 0775, true)) { | ||
throw CacheException::forCreatingDirectory($dir); | ||
} | ||
|
||
$this->dir = $dir; | ||
} | ||
|
||
private function expire(string $filename): bool | ||
{ | ||
if (false === $resource = @shmop_open(fileinode($filename), 'w', 0, 0)) { | ||
return false; | ||
} | ||
|
||
try { | ||
unlink($filename . '-ttl'); | ||
return shmop_delete($resource); | ||
} finally { | ||
shmop_close($resource); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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
Oops, something went wrong.