Laravel: use memcache instead of file system

Whenever I load a page, I see that Laravel is reading a large amount of data from the / storage folder.

Generally speaking, dynamically reading and writing to our file system is a bottleneck. We use the Google App Engine, and our storage is located in Google’s cloud storage, which means that one write or read is equal to the API request "remote". Google Cloud Storage is fast, but I feel it is slow when Laravel makes up to 10-20 Cloud Storage calls per request.

Is it possible to store data in Memcache, and not in the / storage directory? I believe that this will give our systems much better performance.

NB. Both sessions and the cache use Memcache, but compiled views and meta are stored on the file system.

+4
source share
1 answer

To save compiled views to Memcache , you need to replace the repository that uses the Blade Compiler .

First of all, you need a new storage class that extends Illuminate \ Filesystem \ Filesystem . Listed below are the methods used by BladeCompiler : you will need to use Memcache.

  • exist
  • Lastmodified
  • receive
  • to place

The project of this class is below, you can make it more complex:

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;
  }
}

Second, add memcache configuration to 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);
});

.

, , , .

+4

Source: https://habr.com/ru/post/1598468/


All Articles