Laravel memcached Cache-driven configuration and application method analysis _php Example

Source: Internet
Author: User
Tags apc arrays data structures php framework php script redis port number smarty template

This article illustrates the configuration and application method of Laravel memcached cache driver. Share to everyone for your reference, specific as follows:

The memcached cache configuration can be configured to use in any PHP environment to enhance Web performance. For large Web sites (data, large access), caching system is a prerequisite component, which is to reduce the database load, improve page access speed, improve system performance. Laravel, as a full-featured and powerful PHP framework, naturally provides support for caching systems. The benefits of the current LARAVLE-supported cache drivers, including files, arrays, databases, APCs, memcached, and Redis, and providing a unified access interface for these drivers are obvious: we can switch the cache driver at any time according to business requirements without making any changes to the business logic code.

The Laravel cache-driven configuration is located in Config/cache.php, the first configuration item default in the configuration array to specify the default cache driver:

' Default ' => env (' cache_driver ', ' file '),

Here the default cache driver is the file cache. The second configuration item stores is a play that 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 ' =>,
      ],],
  '
    redis ' => [' Driver ' => ' Redis ',
    ' connection ' => ' default ',
  ],


APC is the APC cache, and APC is an extension of PHP with the goal of providing a free, open source, robust framework for caching and Optimizing PHP intermediate code (opcode). For more information, refer to the official PHP documentation: http://php.net/manual/zh/ The BOOK.APC.PHP,APC cache, like Memcached, is a memory-based caching system with comparable performance, but limited to stand-alone caching and distributed, while the actual production environment often does not stop a single Web server, so larger sites are relatively more likely to use memcached.

Array cache drivers (arrays) are often used only for testing purposes, and the benefits are not persisted, only in the lifetime of a PHP script execution.

File cache drivers (files) are often used only for local development testing, because file caching is stored in files, read from the hard disk, and performance is not as natural as a memory based caching system such as APC or memcached and Redis.

The database cache driver stores cached data in a database, and you need to create a new table in the database to hold the cached item, which can be defined as follows:

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

Caching is the data stored in the database to the cache system, although the database slow to some extent improve the performance of the system, but for large systems is not the best choice.

Memcached Cache driver is based on memcached, before you need to install memcached in the system, of course, if you are using the Homestead virtual machine, has been installed for you, and automatically boot, We can use the following instructions to view its status and boot port:

Ps-ef | grep memcached

As we mentioned earlier, Memcached is a distributed caching system based on memory, which is widely used in the actual production environment.

Redis is a cache system in recent years, compared to memcached key-value pairs to support more data structures, including strings, hashes, lists, sets and ordered sets, and so on, is called the data structure of the server, Redis is also based on memory, 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 Queuing, and so on. In short, the function is very powerful. About Redis We'll talk about it separately, so we'll take memcached as a caching driver for example, and systematically talk about how to use caching in Laravel.

From the configuration file, driver in the memcached configuration entry specifies that the cache-driven type used is memcached. Servers is used to install Memcached server, host on behalf of the host name, port on behalf of the memcached listening port number, the default is 11211,weight delegate weight, because many times we have configured a number of memcached servers, The weight represents the priority of the access.

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

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

' Default ' => env (' cache_driver ', ' memcached '),

In addition, the config/cache.php configuration file has the last configuration item prefix, which is used to configure the prefix of the cache key, and for the memory based caching system, the cache entry may be used by multiple applications on the same host, so it is necessary to prefix the distinction. Here we use Laravelacademy as a prefix:

' prefix ' => ' laravelacademy ',

More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with the PHP program design based on Laravel framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.