php - Laravel:使用 Memcache 而不是文件系统

标签 php google-app-engine laravel laravel-4 google-cloud-storage

每当我加载页面时,我都可以看到 Laravel 从/storage 文件夹中读取大量数据。

一般来说,动态读写我们的文件系统是一个瓶颈。我们正在使用 Google App Engine,我们的存储在 Google Cloud Storage 中,这意味着一次写入或读取等同于一次“远程”API 请求。 Google Cloud Storage 很快,但我觉得它很慢,因为 Laravel 每个请求最多调用 10-20 次 Cloud Storage。

是否可以将数据存储在 Memcache 而不是/storage 目录中?我相信这会给我们的系统带来更好的性能。

注意。 Session 和 Cache 都使用 Memcache,但编译后的 View 和元数据存储在文件系统中。

最佳答案

为了在 Memcache 中存储编译 View ,您需要替换 Blade 编译器 使用的存储。

首先,您需要一个扩展 Illuminate\Filesystem\Filesystem 的新存储类。 BladeCompiler 使用的方法如下所列 - 您需要让它们使用 Memcache。

  • 存在
  • 上次修改时间
  • 得到

这个类的草稿如下,你可能想让它更复杂:

class MemcacheStorage extends Illuminate\Filesystem\Filesystem {
  protected $memcached;

  public function __construct() {
    $this->memcached = new Memcached();
    $this->memcached->addServer(Config::get('view.memcached_host'), Config::get('view.memcached_port');
  }

  public function exists($key) {
    return !empty($this->get($key));
  }

  public function get($key) {
    $value = $this->memcached->get($key);
    return $value ? $value['content'] : null;
  }

  public function put($key, $value) {
    return $this->memcached->set($key, ['content' => $value, 'modified' => time()]);
  }

  public function lastModified($key) {
    $value = $this->memcached->get($key);
    return $value ? $value['modified'] : null;
  }
}

第二件事是在您的 config/view.php 中添加内存缓存配置:

'memcached_host' => 'localhost',
'memcached_port' => 11211

您需要做的最后一件事是覆盖您的一个服务提供商中的 blade.compiler 服务,以便它使用您全新的 memcached 存储:

$app->singleton('blade.compiler', function ($app) {
    $cache = $app['config']['view.compiled'];

    $storage = $app->make(MemcacheStorage::class);

    return new BladeCompiler($storage, $cache);
});

这应该可以解决问题。

如果您发现一些拼写错误或错误,请告诉我,我没有机会运行它。

关于php - Laravel:使用 Memcache 而不是文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486719/

相关文章:

php - 如何摆脱 GET 请求的 "You must configure the check path to be handled by the firewall"错误?

php - 为什么这个 PHP 函数没有传递所有变量数据?

java - Google App Engine - 接收特定版本的电子邮件

laravel - 如何更改 Laravel 中的响应?

php - 如何验证键名中带有点的数组? (拉拉维尔)

PHP:向 ios 发送推送通知

python - Google App Engine : ImportError: No module named _md5 的 Go 教程

google-app-engine - 在 GAME 中查看 memcached 项目

javascript - Laravel - 在使用 jQuery AJAX 提交表单之前检查用户名是否已存在

php - 使用未定义常量 S - 假定为 "S"