Cache only part of a page in PHP

Is it possible to cache only a specific part of a page in PHP or the output of a specific section of code in a PHP script? It seems that when I try to cache a specific page, it caches the entire page that it does not want, I want some of the content on my page to be updated every time the page is loaded, and others (for example, a drop-down list with data from the database) need to be updated only every hour or so.

+6
source share
4 answers

Zend_cache

I would use the Zend Frameworks Zend_Cache library for this.

You can simply use this component without using the entire infrastructure.

Go to the Zend Framework download page and grab the latest version.

After you have uploaded the main files, you will need to include Zend_Cache in your project. Zend_Cache docs.

Have you decided how you want to cache your data? Are you using a file system? Or are you memcache? Once you know what you are going to use, you need to use the specific Zend_Cache backend.

Zend_Cache Backend / Zend_Cache Frontends

  • You need to use the backend (how do you cache what you want to cache in the storage) and
  • You need to use the interface (how you really want to cache, for example, use the buffer or the results of the cache function, etc.).

Database Documentation: Zend_Cache Backends Front-end Documentation: Zend_Cache Fonts

So, you would do something like this ...

 <?php // configure caching backend strategy $backend = new Zend_Cache_Backend_Memcached( array( 'servers' => array( array( 'host' => '127.0.0.1', 'port' => '11211' ) ), 'compression' => true ) ); // configure caching frontend strategy $frontend = new Zend_Cache_Frontend_Output( array( 'caching' => true, 'cache_id_prefix' => 'myApp', 'write_control' => true, 'automatic_serialization' => true, 'ignore_user_abort' => true ) ); // build a caching object $cache = Zend_Cache::factory( $frontend, $backend ); 

This will create a cache that uses the Zend_Cache_Frontend_Output caching mechanisms.

To use the Zend_Cache_Frontend_Output you want, it will be simple . Instead of a kernel you would use output . The parameters you pass are identical. Then to use it you:

Zend_Cache_Frontend_Output - usage

 // if it is a cache miss, output buffering is triggered if (!($cache->start('mypage'))) { // output everything as usual echo 'Hello world! '; echo 'This is cached ('.time().') '; $cache->end(); // output buffering ends } echo 'This is never cached ('.time().').'; 

Useful blog: http://perevodik.net/en/posts/14/

Sorry, this question took longer than expected, and many answers were written.

+3
source

If you are talking about caching by the browser (and any proxy servers with which it can interact), then no. Caching is performed only on full HTTP resources (i.e. based on each URI).

In your own application, you can cache data, so you do not need (for example) to delete the database for each request. Memcached is a popular way to do this.

+5
source

You can use your own caching with ob_start (), ob_end_flush () and similar functions. Collect the desired result, upload it to a file or database and read it later if the conditions are the same. I usually build the md5 sum of state and restore it later.

+1
source

It depends on which caching and browsing technologies you use. In general, yes, you can do something like this:

 // if it is a cache miss, output buffering is triggered if (!($cache->start('mypage'))) { // output everything as usual echo 'Hello world! '; echo 'This is cached ('.time().') '; $cache->end(); // output buffering ends } echo 'This is never cached ('.time().').'; 

taken from Zend_Cache documentation .

Otherwise, in your example, you can always make a function that returns a drop-down list and implements the caching mechanism inside this function. Thus, your page does not even know about caching.

+1
source

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


All Articles