Metrics
0
Watch 101 Star 351 Fork 88

swoole / frameworkPHPLGPL-3.0

Sign up for free
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
基于Swoole扩展开发的PHP web server&应用服务器, 内置框架。提供统一注册树,数据库操作,模板,Cache,日志,队列,上传管理,用户管理等丰富的功能特性。 spread retract

Clone or download
Storage.php 1.82 KB
Copy Edit Web IDE Raw Blame History
<?php
namespace Swoole\Memory;
use Swoole\Exception\Syscall;
use Swoole\Tool;
class Storage
{
static $shmDir = '/dev/shm';
static $separator = ':';
protected $baseDir;
protected $mode;
function __construct($subdir = 'swoole', $mode = 0777)
{
$this->baseDir = self::$shmDir . '/' . $subdir;
$this->mode = $mode;
if (!is_dir($this->baseDir))
{
Syscall::mkdir($this->baseDir, $this->mode, true);
}
}
protected function getFile($key, $createDir = false)
{
$file = $this->baseDir . '/' . str_replace(self::$separator, '/', trim($key, self::$separator));
$dir = dirname($file);
if ($createDir and !is_dir($dir))
{
Syscall::mkdir($dir, $this->mode, true);
}
return $file;
}
function get($key)
{
$file = $this->getFile($key);
if (!is_file($file))
{
return false;
}
$res = Tool::readFile($file);
if ($res)
{
return unserialize($res);
}
else
{
return false;
}
}
function set($key, $value)
{
$file = $this->getFile($key, true);
if (file_put_contents($file, serialize($value), LOCK_EX) === false)
{
return false;
}
else
{
return true;
}
}
function exists($key)
{
return is_file($this->getFile($key));
}
function scan($prefix)
{
$dir = $this->baseDir . '/' . str_replace(self::$separator, '/', trim($prefix, self::$separator));
if (!is_dir($dir))
{
return false;
}
return Tool::scandir($dir);
}
function del($key)
{
$file = $this->getFile($key);
return unlink($file);
}
}

Comment ( 1 )

You need to Sign in for post a comment

Help Search