PHP 7 是否有兼容的内存缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37109004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any compatible memory cache for PHP 7?
提问by Frodik
I would like to use PHP 7. However there seems to be no option for key-value memory caching.
我想使用 PHP 7。但是似乎没有用于key-value memory caching 的选项。
XCache will not be available for PHP 7 for some time.
Is there an available alternative for PHP 7?
PHP 7 是否有可用的替代方案?
Or is there a way to use PHP7's Opcache to implement key-value memory caching?
或者有没有办法使用PHP7的Opcache来实现键值内存缓存?
回答by Matt Prelude
I'd suggest using Memcached, especiallyif you're concerned about performance.
我建议使用Memcached,特别是如果您担心性能。
Whilst you are correct that APC(u) is a lot faster than Memcache, you're not taking into the account that by the time you're worrying about these metrics, you will be running across multiple serversand APC(u) cannot be shared across nodes.
虽然您说 APC(u) 比 Memcache 快很多是正确的,但您并没有考虑到当您担心这些指标时,您将在多个服务器上运行而 APC(u) 不能跨节点共享。
You can use a single Memcache instance or cluster to serve as many application servers as you want. Scalability is a greater concern in modern application development than "how much performance can I squeeze out of one server?"
您可以使用单个 Memcache 实例或集群来为任意数量的应用服务器提供服务。在现代应用程序开发中,可扩展性比“我可以从一台服务器中挤出多少性能”更受关注。
Having said that, your alternative is APCu, which has all of the functionality that you're used to from APC. It is marked as stable with PHP7 but I wouldn't recommend this because of its single-node nature & inability to work correctly with fastcgi.
话虽如此,您的替代方案是APCu,它具有您从 APC 习惯使用的所有功能。它在 PHP7 中被标记为稳定,但我不推荐这样做,因为它的单节点性质和无法与 fastcgi 正常工作。
回答by Alister Bulman
APCUis literallyAPC without the code caching (they took the APC code, removed the byte-code cache and released it as APCU). It's a drop-in replacement. Exactly as APC's user-cache, it keeps the data in the same process as the PHP runtime, and so using the value is like much like retrieving an ordinary variable, hence the speed.
APCU是字面上APC无码缓存(他们把APC代码,删除字节码缓存,它释放APCU)。这是一个直接的替代品。就像 APC 的用户缓存一样,它将数据保存在与 PHP 运行时相同的进程中,因此使用该值就像检索普通变量一样,因此速度很快。
回答by El Yobo
Another approach (which I haven't tried, but sounds very interesting) is to take advantage of the opcache as a key value cache. This graphiqpost contains more details but no real code unfortunately (and note the comments from Kerry Schwab).
另一种方法(我没有尝试过,但听起来很有趣)是利用 opcache 作为键值缓存。这篇graphiq帖子包含更多细节,但不幸的是没有真正的代码(并注意 Kerry Schwab 的评论)。
The gist of it is to ensure that the opcache is enabled and has enough memory allocated for the data that you need to cache and then something along the lines of the following (lifted from the article, check it out in full). Cache expiration (aside from manual deletion) would need to be handled too, but wouldn't be hard to add (e.g. wrap your data in a containing object with an expiry time and check it in your cache_get
, deleting and ignoring the record if it's expired).
它的要点是确保启用 opcache 并为您需要缓存的数据分配足够的内存,然后按照以下内容(从文章中提取,完整查看)。缓存过期(除了手动删除)也需要处理,但添加起来并不难(例如,将您的数据包装在一个包含对象中并带有过期时间并在您cache_get
的 . )。
function cache_set($key, $val) {
$val = var_export($val, true);
// HHVM fails at __set_state, so just use object cast for now
$val = str_replace('stdClass::__set_state', '(object)', $val);
// Write to temp file first to ensure atomicity
$tmp = "/tmp/$key." . uniqid('', true) . '.tmp';
file_put_contents($tmp, '<?php $val = ' . $val . ';', LOCK_EX);
rename($tmp, "/tmp/$key");
}
function cache_get($key) {
@include "/tmp/$key";
return isset($val) ? $val : false;
}
Because of the opcache this functions as an in memory cache, but it avoids the overhead of serialization and deserialization. I guess that the cache_set should also call opcache_invalidate
when writing (and in the cache_delete
function which doesn't exist in their examples) but otherwise it seems sound for a cache which doesn't need to be shared between servers.
由于 opcache,它充当内存缓存,但它避免了序列化和反序列化的开销。我想 cache_set 也应该opcache_invalidate
在写入时调用(以及在cache_delete
它们的示例中不存在的函数中),但否则对于不需要在服务器之间共享的缓存来说似乎是合理的。
Edit: An example implementation with cache expiry (only accurate to a second, could use microtime(true)
if more accuracy is required) below. Minimal testing actually done, and I dropped the HHVM specific replacement, so YMMV. Suggestions for improvements welcome.
编辑:下面是缓存到期的示例实现(仅精确到一秒,microtime(true)
如果需要更高的准确性,可以使用)。实际完成了最少的测试,我放弃了 HHVM 特定的替代品,所以 YMMV。欢迎提出改进建议。
class Cache {
private $root;
private $compile;
private $ttl;
public function __construct($options = []) {
$this->options = array_merge(
array(
'root' => sys_get_temp_dir(),
'ttl' => false,
),
$options
);
$this->root = $this->options['root'];
$this->ttl = $this->options['ttl'];
}
public function set($key, $val, $ttl = null) {
$ttl = $ttl === null ? $this->ttl : $ttl;
$file = md5($key);
$val = var_export(array(
'expiry' => $ttl ? time() + $ttl : false,
'data' => $val,
), true);
// Write to temp file first to ensure atomicity
$tmp = $this->root . '/' . $file . '.' . uniqid('', true) . '.tmp';
file_put_contents($tmp, '<?php $val = ' . $val . ';', LOCK_EX);
$dest = $this->root . '/' . $file;
rename($tmp, $dest);
opcache_invalidate($dest);
}
public function get($key) {
@include $this->root . '/' . md5($key);
// Not found
if (!isset($val)) return null;
// Found and not expired
if (!$val['expiry'] || $val['expiry'] > time()) return $val['data'];
// Expired, clean up
$this->remove($key);
}
public function remove($key) {
$dest = $this->root . '/' . md5($key);
if (@unlink($dest)) {
// Invalidate cache if successfully written
opcache_invalidate($dest);
}
}
}
回答by DevWL
PHP 7 cache / accelerator list
PHP 7 缓存/加速器列表
Listo of dead/outdatedPHP accelerators: XCache, APC, memoize, ZendOpcache, chdb, hidef (they are not supporting PHP 7)
的LISTO死/过时的PHP加速器:了XCache,APC,memoize的,ZendOpcache,chdb,hidef(他们不支持PHP 7)
We can find a list of PHP Accelerators on PECL website, but as i menthion some of them are discontinued or not up to date.
我们可以在PECL 网站上找到 PHP 加速器列表,但正如我所提到的,其中一些已停产或不是最新的。
Currently developed(with support for PHP 7.3) are :
目前开发的(支持 PHP 7.3)有:
You will find all installation instruction in a downloaded tgz/zip file.
您将在下载的 tgz/zip 文件中找到所有安装说明。
APCu WINDOWS USERS: Download APCu and APCu_bc DLL files that matches your system specification x64 (64 bits) OR x86 (32 bits windows) select TS or UTS version and of course the right PHP version. Paste .DLL into your php/extdirectory You can determine thread mode by looking at your php directory. Find at DLL file name (for example: php7ts.dll). Notice the TS or UTS in file name.
APCu WINDOWS 用户:下载与您的系统规范相匹配的 APCu 和 APCu_bc DLL 文件 x64(64 位)或 x86(32 位窗口)选择 TS 或 UTS 版本,当然还有正确的 PHP 版本。将 .DLL 粘贴到您的php/ext目录中您可以通过查看您的php 目录来确定线程模式。在 DLL 文件名处查找(例如:php7ts.dll)。注意文件名中的 TS 或 UTS。
php -v
will show you your current PHP CLI installation version.
Just make sure your PHP version is same with the one you are using in your server. If not change update the windows environment path for PHP.
php -v
将显示您当前的 PHP CLI 安装版本。只需确保您的 PHP 版本与您在服务器中使用的版本相同。如果没有更改,请更新 PHP 的 Windows 环境路径。
If you have difficulties read this: How to install apcu in windowshttps://kapilpatel84.wordpress.com/2016/06/15/install-xdebug-apcu-on-windows-xampp-for-php7/
如果您有困难,请阅读:如何在 Windows 中安装 apcu https://kapilpatel84.wordpress.com/2016/06/15/install-xdebug-apcu-on-windows-xampp-for-php7/
FOR XAMPP:
对于 XAMPP:
1) Down load compatible APCu with following link http://pecl.php.net/package/apcu
1) 使用以下链接下载兼容的 APCu http://pecl.php.net/package/apcu
2) APCu need backword compatabily with APC so you have to download it with following link. http://pecl.php.net/package/apcu_bc
2)APCu需要与APC兼容的backword,所以你必须通过以下链接下载它。http://pecl.php.net/package/apcu_bc
3) Extact DDL file and move DDL file named : php_apc.dll and php_apcu.dll and copy that files to your PHP ext Directory. i.e C:\xampp\php\ext
3) 提取 DDL 文件并移动名为 php_apc.dll 和 php_apcu.dll 的 DDL 文件,并将这些文件复制到您的 PHP ext 目录。即 C:\xampp\php\ext
4) Open php.ini file and copy the following code at the bottom of the file
4)打开php.ini文件,在文件底部复制以下代码
[apcu]
extension="C:\xampp\php\ext\php_apcu.dll"
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
apc.serializer=php
extension="C:\xampp\php\ext\php_apc.dll"
When you unzip the file. Copy DLL file to your PHP extenstion folder np: .../php/ext. and configure php.ini (conf instruction is included in the INSTALL text file).
当您解压缩文件时。将 DLL 文件复制到您的 PHP 扩展文件夹 np: .../php/ext。并配置 php.ini(安装文本文件中包含 conf 指令)。
Symfony 4
Symfony 4
PS. If by any chance you using Symfony framework dont forget to anable APCu in config>packages>cache.yaml
附注。如果您使用 Symfony 框架,请不要忘记在config>packages>cache.yaml 中启用 APCu
framework:
cache:
app: cache.adapter.apcu
Use builtin server with:
使用内置服务器:
php bin/console server:run