gpt4 book ai didi

php - PHP 中的共享内存文件

转载 作者:可可西里 更新时间:2023-11-01 12:43:27 26 4
gpt4 key购买 nike

我使用 openssl_pkcs7_signopenssl_pkcs7_encrypt 创建加密数据。这些函数只接受文件名。我想将临时文件存储在共享内存中以提高性能。我知道在 Linux 中我可以 file_put_contents('/dev/shm/xxx', data),但在 Windows 中是不可能的。 PHP 中是否有可移植的方式来执行此操作? shmop_ 函数在这里有帮助吗?谢谢。

PS:或者有没有办法让这些函数接受数据字符串?

PS2:请不要建议从 PHP 调用 /usr/bin/openssl。它不是可移植的。

最佳答案

好的,所以我建议的方法是使用文件 stream wrapper .让我举一个简单的例子:

class staticStreamWrapper {
public $context;
protected static $data = array();

protected $path = '';
protected $pointer = 0;
protected $writable = false;

public function stream_close() {}

public function stream_eof() {
return $this->pointer >= strlen(static::$data[$this->path]);
}

public function stream_flush() {}

public function stream_open($path, $mode, $options, &$opened_path) {
switch ($mode[0]) {
case 'r':
if (!isset(static::$data[$path])) return false;
$this->path = $path;
$this->writable = isset($mode[1]) && $mode[1] == '+';
break;
case 'w':
static::$data[$path] = '';
$this->path = $path;
$this->writable = true;
break;
case 'a':
if (!isset(static::$data[$path])) static::$data[$path] = '';
$this->path = $path;
$this->writable = true;
$this->pointer = strlen(static::$data[$path]);
break;
case 'x':
if (isset(static::$data[$path])) return false;
$this->path = $path;
$this->writable = true;
break;
case 'c':
if (!isset(static::$data[$path])) static::$data[$path] = '';
$this->path = $path;
$this->writable = true;
break;
default:
return false;
}
$opened_path = $this->path;
return true;
}

public function stream_read($count) {
$bytes = min(strlen(static::$data[$this->path]) - $this->pointer, $count);
$data = substr(static::$data[$this->path], $this->pointer, $bytes);
$this->pointer += $bytes;
return $data;
}

public function stream_seek($offset, $whence = SEEK_SET) {
$len = strlen(static::$data[$this->path]);
switch ($whence) {
case SEEK_SET:
if ($offset <= $len) {
$this->pointer = $offset;
return true;
}
break;
case SEEK_CUR:
if ($this->pointer + $offset <= $len) {
$this->pointer += $offset;
return true;
}
break;
case SEEK_END:
if ($len + $offset <= $len) {
$this->pointer = $len + $offset;
return true;
}
break;
}
return false;
}

public function stream_stat() {
$size = strlen(static::$data[$this->path]);
$time = time();
return array(
0 => 0,
'dev' => 0,
1 => 0,
'ino' => 0,
2 => 0777,
'mode' => 0777,
3 => 1,
'nlink' => 1,
4 => 0,
'uid' => 0,
5 => 0,
'gid' => 0,
6 => '',
'rdev' => '',
7 => $size,
'size' => $size,
8 => $time,
'atime' => $time,
9 => $time,
'mtime' => $time,
10 => $time,
'ctime' => $time,
11 => -1,
'blksize' => -1,
12 => -1,
'blocks' => -1,
);
}

public function stream_tell() {
return $this->pointer;
}

public function stream_write($data) {
if (!$this->writable) return 0;
$size = strlen($data);
$len = strlen(static::$data[$this->path]);
if ($this->stream_eof()) {
static::$data[$this->path] .= $data;
} else {
static::$data[$this->path] = substr_replace(
static::$data[$this->path],
$data,
$this->pointer
);
}
$this->pointer += $size;
return $size;
}

public function unlink($path) {
if (isset(static::$data[$path])) {
unset(static::$data[$path]);
}
return true;
}

}

现在,您需要注册包装器:

stream_wrapper_register('static', 'staticStreamWrapper');

因此您现在可以将其视为一个文件,即使它从未真正离开 PHP(它存储为一个静态变量)!

file_put_contents('static://foo.txt', 'this is my data');
file_get_contents('static://foo.txt'); // "this is my data"
$f = fopen('static://foo.txt', 'r'); // should return a resource
// etc...

关于php - PHP 中的共享内存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5669631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com