Home >php教程 >PHP开发 >Laravel Memcached cache driver configuration and application method analysis

Laravel Memcached cache driver configuration and application method analysis

高洛峰
高洛峰Original
2016-12-28 16:32:391271browse

The example in this article describes the configuration and application method of Laravel Memcached cache driver. Share it with everyone for your reference, the details are as follows:

Memcached cache configuration can be configured and used in any PHP environment to improve WEB performance. For large websites (with a lot of data and a lot of visits), the caching system is an essential component. It makes great contributions to reducing database load, increasing page access speed, and improving system performance. As a fully functional and powerful PHP framework, Laravel naturally provides support for the caching system. Laravle currently supports cache drivers including files, arrays, databases, APC, Memcached and Redis, and provides a unified access interface for these drivers. The benefits of this are obvious: we can switch cache drivers at any time according to business needs without having to worry about the business. Make any changes to the logic code.

Laravel cache driver configuration is located in config/cache.php. The first configuration item default in the configuration array is used to specify the default cache driver:

'default' => env('CACHE_DRIVER', 'file'),

The default cache driver here is file cache. The second configuration item, stores, is the highlight and is used to configure the 6 cache drivers supported by Laravel:

'stores' => [
  'apc' => [
    'driver' => 'apc',
  ],
  'array' => [
    'driver' => 'array',
  ],
  'database' => [
    'driver' => 'database',
    'table' => 'cache',
    'connection' => null,
  ],
  'file' => [
    'driver' => 'file',
    'path' => storage_path('framework/cache'),
  ],
  'memcached' => [
    'driver' => 'memcached',
    'servers' => [
      [
        'host' => '127.0.0.1',
        'port' => 11211,
        'weight' => 100,
      ],
    ],
  ],
  'redis' => [
    'driver' => 'redis',
    'connection' => 'default',
  ],
],

where apc represents APC cache, APC is an extension of PHP, and its goal is to cache and optimize PHP intermediate code ( opcode) provides a free, open source, and robust framework. For more details, please refer to the official PHP documentation: http://php.net/manual/zh/book.apc.php. APC cache, like Memcached, is a memory-based caching system with comparable performance, but the limitation is that it can only be used Single-machine caching does not support distribution, and the actual production environment often does not have more than one web server, so relatively speaking, large websites choose to use Memcached more.

Array cache driver (array) is often only used for testing. The advantage is that it is not persistent and will only be valid within the life cycle of a PHP script execution.

The file cache driver (file) is often only used for local development and testing, because the file cache stores the cache in the file and reads it from the hard disk when reading. The performance is naturally not as good as that of memory-based caching systems such as APC or Memcached. And Redis.

The database cache driver (database) stores cached data in the database. Before using it, you need to create a new table in the database to store cache items. The table structure can be defined as follows:

Schema::create('cache', function($table) {
  $table->string('key')->unique();
  $table->text('value');
  $table->integer('expiration');
});

Cache originally reads data from the database and stores it in the cache system. Although database caching improves system performance to a certain extent, it is naturally not the best choice for large systems.

The memcached cache driver is based on Memcached. You need to install Memcached in the system before using it. Of course, if you are using the Homestead virtual machine, it has already been installed for you and will start automatically at boot. We can use the following instructions Check its status and startup port:

ps -ef | grep memcached

As we mentioned earlier, Memcached is a memory-based distributed cache system that is widely used in actual production environments.

Redis is a caching system that has only appeared in recent years. Compared with Memcached's key-value pairs, it supports more data structures, including strings, hashes, lists, sets, and ordered sets. Because of this, also known as a data structure server, Redis is also memory-based, but can be persisted to the hard disk. In addition to being a caching system, it can also be used as a NoSQL database, message queue, etc. All in all, it's very powerful. We will talk about Redis separately later, so for caching instances, we will take Memcached as the cache driver as an example, and systematically talk about how to use caching in Laravel.

Starting from the configuration file, the driver in the memcached configuration item specifies the cache driver type used as memcached. servers represents the server where Memcached is installed, host represents the host name, port represents the port number that Memcached listens on, the default is 11211, and weight represents the weight, because many times we configure multiple Memcached servers, and the weight represents the priority of access.

As a test, we will use the default configuration here.

Before we start, we need to change the default cache driver to memcached:

'default' => env('CACHE_DRIVER', 'memcached'),

In addition, there is the last configuration item prefix in the config/cache.php configuration file, which is used to configure the prefix of the cache key. , For memory-based caching systems, cache items may be used by multiple applications on the same host, so it is necessary to add a prefix to show the difference. Here we use laravelacademy as the prefix:

'prefix' => 'laravelacademy',

I hope this article will be helpful to everyone’s PHP programming based on the Laravel framework.

For more articles related to the configuration and application method analysis of Laravel Memcached cache driver, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn