Grav - Performance & Caching


In this chapter, we will understand the concepts of performance and caching in Grav.

Performance

The term performance refers to the system performance in such a way that whether it can handle higher load on system and modify the system to handle a higher load.

Consider the following points relating to the performance of Grav −

  • To have better performance of Grav, you can use PHP opcache and usercache. The opcache works well with PHP 5.4 and usercache works faster with PHP 5.5, PHP 5.6 and Zend opcache.

  • The SSD (Solid State Drive) which uses flash memory and has no moving parts. Sometimes cache will be stored in user cache or stored as files. So SSD drives can give better performance.

  • Virtual machines are the best way of hosting providers under the cloud computing technology. You can specify the resources without interacting with physical equipment. Native hosting is faster than virtual machine. Grav runs better on virtual machines, but for optimal performance you can make use of the native hosting option.

  • Grav has faster memory in which its cache uses heavy memory that provides better performance on your server. Compared to other platforms, it uses less amount of memory.

  • Grav uses shared hosting on the shared server to share the things. Sharing hosting is available in a low cost and sometimes it may lead to slow the things on the server.

  • Multi core processors are used for handling multiple tasks faster. The advanced processors are better than these processors which helps the user to accomplish the task.

Caching

In general, cache is a stored data in a cache memory. Cache mechanism makes Grav faster in which browser can get files from cache rather than the original server, saving time and additional network traffic.

Grav uses Doctrine Cache library which supports the following −

  • Auto (Default) − It uses default option automatically.

  • File − It specifies cache files present in the cache/ folder.

  • APC

  • XCache

  • Memcache

  • Redis

  • WinCache

By default, Grav uses the auto setting. It will try for APC, then for WinCache, XCache and lastly it uses File.

Caching Types

There are 5 types of caching −

  • YAML configuration caching into PHP.

  • Core Grav caching for page objects.

  • Twig caching of template files as PHP classes.

  • Image caching for media resources.

  • Asset caching of CSS and jQuery with pipelining.

The caching of YAML configuration stored in the /cache folder. The image caching stores its images in the /images folder. The configuration option for core Grav caching is stored in user/config/system.yml file as shown below −

cache:
   enabled: true
   check:
      method: file
   driver: auto
   prefix: 'g'
  • The enabled option enables the caching by setting it to true.

  • 方法选项检查页面(例如文件或文件夹)中的更新。

  • 驱动程序选项指定不同类型的缓存选项,例如 Auto、File、APC、XCache、Memcache、Redis 或 WinCache。

  • prefix选项定义缓存前缀字符串。

内存缓存选项

如果您使用memcached 服务器,则需要使用user/config/system.yml 文件中的memcache驱动程序添加一些额外的配置选项。这些选项可以在cache:组下定义。

cache:
...
...
   memcache:
      server:localhost
      port: 8080

Redis 选项

如果您使用redis,那么您需要使用user/config/system.yml 文件中的redis驱动程序添加一些额外的配置选项。这些选项可以在cache:组下定义。

cache:
...
...
   redis:
   	server:localhost
      port: 8080

Twig 模板通过在user/config/system.yml文件中使用 twig 驱动程序来使用自己的缓存机制。

twig:
   cache: true
   debug: true
   auto_reload: true
   autoescape: false

它有一些选项,例如 -

  • 缓存选项通过将其设置为 true 来启用树枝缓存。

  • debug选项启用 twig 调试。

  • auto_reload选项用于通过将其设置为 true 来重新加载更改。

  • autoescape选项用于自动转义树枝变量。

缓存和事件

启用缓存后可以使用事件。可以为除OnPageContentRawOnPageProcessedOnPageContentProcessedOnTwigPageVariablesOnFolderProcessed事件之外的所有事件启用此功能。这些事件可以在所有页面和文件夹上使用,并且只能在处理事件时运行。页面缓存后,这些事件无法运行。