Drupal Cache Form




Yes, the form cache was disabled. Otherwise the hook would have fired the first time. But this means that the validation handler validates a version of the form other than what was on the actual page seen by the user. This seems like a bug. Cache form Drupal change expire time. Ask Question Asked 6 years, 10 months ago. Active 3 years, 7 months ago. Viewed 1k times 2.


Jimmy Huang


Drupal

Founder, since 2006

NETivism

Founder, since 2009
jimmy at netivism.com.tw




User can see the lastest post

客戶說他的網頁不會更新



Display blank page randomly
隨機出現白畫面



My css effect has gone
CSS的效果不見了



Form can't submit.
本來正常的表單,卻再也送不出去了



Everyone saw broken page but not on my computer.
別人看網頁壞掉,但我看起來是好的


Drupal Cache Form Template


Drupal

Why my counter not increase?
為什麼我的文章點閱沒增加?



Only a word comes to mind...





Logstalgia







Cache_set


Peopo.org

  • over 100,000 nodes (not many)
  • over 800,000 node-category relation
  • over 300 query in page (when no cache)
  • over 140 enabled drupal modules
  • heavy query (join large table)

31 ms for render page

to anonymous user

0ms for 304 not modified

376 ms for render page

to logged in user





Plugin

Cache control in http header

  • Only for anonymous visitor
  • Tell browser take control of cache
  • Tell browser this page will expire after 21600s
  • Drupal already done in default:

read: drupal_serve_page_from_cache

Etag / Expires / Last Modified
in HTML header

  • Only for anonymous visitor
  • Etag:
    • Add unique id to delivered page
  • Last-Modified:
    • Used to detect if page modified since last visit.
  • Expires:
    • Setting by 'max-age' of heder cache-control

read: drupal_serve_page_from_cache

Static file cache in browser

  • Do not write CSS / Javascript in page
  • drupal_add_css in ALL PAGE
  • durpal_add_js should consider too




Nginx

  • Combination of proxy / static http file server
    • We won't hit Dynamic if we don't need
    • Extreme low in memory comsumption
    • Build-in cache mechanism
    • Build-in flood control mechanism
  • Asynchronous event-driven handle request
    • Eat ton of request in 1 process
    • More effective in multi-processor env

microcache in Nginx

  • Flood Protection / DOS protection
  • Short lifetime ( 1s - 5s )
  • Can used for logged user (use carefully)
  • Never hit PHP when continue hitting
read: microcache config on drupal-with-nginx

Boost.module of Drupal

  • Only for anonymous visitor
  • Static HTML page generation when first hit on PHP
  • Use rewrite rule (in Nginx or Apache) to redirect to HTML
  • Expired by
    • Cron
    • Crawler ( drupal 6 only)
    • Node update ( drupal 6 only )

Serve static html from nginx

10x faster than Apache prefork

nginx: 100,000 requests in 60s
apache: 10,000 requests in 60s







What Drupal does from fresh hit?

Warm up PHP in every request
  1. Load system wide variables
  2. Load menu to check user have permission to access page
  3. Load localized data to prepare translation strings
  4. Load modules to prepare work
  5. Render forms when needed
  6. Gethering content in Blocks of page
  7. Deliver page rendered by template / theme system
  8. Finally we got an page! So slow...
Run SQL Query in every needs

OP Code Cache for PHP

Before: 104MB per page
Memory used at: devel_boot()=5.43 MB, devel_shutdown()=104.15 MB
Memory used at: devel_boot()=2.34 MB, devel_shutdown()=49.58 MB
OPcode cache in php-apc cache status

Drupal Cache Stack

almost cache everything

  1. Page ( only for anonymous user )
  2. Variables
  3. Session cache ( 3 party module )
  4. Localized string
  5. Module info/registry
  6. Template engine registry
  7. Filtered content
  8. Forms
  9. Aggregated content (Block/Views/Panels)
  10. Aggregated resource (CSS/JS/Image)


Core - Page Cache / Block Cache / CSS and JS cache


Cache

Views Cache (in period or ...)


Panels Cache (per page or ...)

Block Cache (set by module code)


Recommended Modules for cache

  • Memcache API and Integration or Memcache Storage
    • save cache into memcached (instead hitting database)
  • Views Content Cache
    • cache views based on content save
      more effective because not time period
  • Panel Page Cache or Panel Hash Cache
    • cache panels based on url / user / content type
    • setting cache clear rules in simple clicks (with rules)
    • Claw specific pages you need.

Cache Strategy

You should look decent to different strategy for each block
  • cache in Time Period
  • cache per User
  • cache per Page (URL)
  • cache per User per Page
  • cache per Role per Page
  • others
    • cache per content type
    • cache per node
    • cache per role
    • ...

Cache Clear Strategy

  • Expired after time period
  • Expired when new version exists
    • clear node cache when node update
    • clear views / panels cache when node update
    • clear user profile page when user update
    • clear term page when term update
  • Expired in specific URL parameter





more on later




Drupal is a Query Monster

  • Join , Join, Join, Join .......
  • Eat 300 query a page easily
    • Save config / variables in DB
    • Save session in DB
    • Save cache in DB
    • Save localized string in DB
    • Save search index in DB
  • Un-optimized query from views
  • Everything combined with node

Hot news page

Query Cache Performance

Before query cache: 2301.12ms

Drupal Cache Form Pdf

After query cache: 2.49ms


'Watch' your views

Calculate Hit Rate

High hit rate means memory saving.



Server / Service Combination

  • PHP-APC
    • Almost 100% hit rate
    • Watch out - some version ignore .module file
    • Do not use in multiple virtualhost environment
  • MySQL Query Cache
    • 32MB memory get huge performance enhance
    • Remember calculate hit rate
  • Memcached
    • client-server architect
    • use even behind balancer
      • Session should save here, but D7 still problem

Anonymous User

  • microcache of nginx
    • 1-3s cache time is enough
    • ajax needed for Drupal node page counting
    • Best practice at perusio/ drupal-with-nginx
  • Page Cache to memcached
    • drupal core mechnism but save cache to memory
    • Trouble-less, very stable, but still hitting PHP
    • recommend set cache max lifetime
  • Cache Warmer of drupal
    • simple drush command to crawl specific page in cron

Logged in User

  • Panels + Panel page cache
    • Cache whole panel based on url
    • Hack module to support per role per page
    • Fix some personal string in ajax
    • do not cache form
  • Use views cache carefully
    • can't run without this
    • headache on Contextual / Exposed filter cache
    • finally, we can add 'update - then - clear cache' rule
    • got to have debug skill at fetch 'cache id'







Basic Question

  1. Do the cache enabled?
  2. What I'm seeing, cached or not?
  3. When the cache expired or refresh?
  4. Is that correct when different user see this page?
    user name or profile misplace)
  5. Is that correct when different role see this page?

    (permission reason)

  6. Do the form worked correctly?
  7. you can't cache form (security reason)

Tools and Hint


  1. Page cache can verify by HTTP header
  2. PHP-APC havewatch script
  3. Memcached have script, too
  4. Use devel module to see page generate time
  5. Profiling: XHProf
  6. on large site, never 'Clear All Cache'
    this will save you life)
  7. no cached page when anonymous have session
    (added at drupal 7)





May the cache be with you.

  1. 4.6.x includes/bootstrap.inc cache_clear_all()
  2. 4.7.x includes/bootstrap.inc cache_clear_all()
  3. 5.x includes/cache.inc cache_clear_all()
  4. 6.x includes/cache.inc cache_clear_all()
  5. 6.x includes/cache-install.inc cache_clear_all()

Expires data from the cache.

If called with the arguments $cid and $bin set to NULL or omitted, thenexpirable entries will be cleared from the cache_page and cache_block bins,and the $wildcard argument is ignored.

Parameters

$cid:If set, the cache ID or an array of cache IDs. Otherwise, all cache entriesthat can expire are deleted. The $wildcard argument will be ignored if setto NULL.

$bin:If set, the cache bin to delete from. Mandatory argument if $cid is set.

$wildcard:If TRUE, the $cid argument must contain a string value and cache IDsstarting with $cid are deleted in addition to the exact cache ID specifiedby $cid. If $wildcard is TRUE and $cid is '*', the entire cache is emptied.

aggregator_aggregator_parsein modules/aggregator/aggregator.parser.inc
Implements hook_aggregator_parse().
BlockCacheTestCase::testCachePerRolein modules/block/block.test
Test DRUPAL_CACHE_PER_ROLE.
block_add_block_form_submitin modules/block/block.admin.inc
Form submission handler for block_add_block_form().
block_admin_configure_submitin modules/block/block.admin.inc
Form submission handler for block_admin_configure().
block_admin_display_form_submitin modules/block/block.admin.inc
Form submission handler for block_admin_display_form().
menu_cache_clearin includes/menu.inc
Clears the cached data for a single named menu.
_menu_clear_page_cachein includes/menu.inc
Clears the page and block caches at most twice per page load.

File

includes/cache.inc, line 163
Functions and interfaces for cache handling.

Code

Comments

You may be looking for this...

MichaelCole commented

drupal_flush_all_caches:

drupal_flush_all_caches() calls cache_clear_all()

Elijah Lynn commented

In addition it is worth noting that drupal_flush_all_caches() calls cache_clear_all().

Not sure what happened to link above but this one currently works https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_...

$bin cache table

jyee commented

The $bin is the name of the particular database cache table. So, for example:
cache_clear_all('field:node:' . $node->nid, 'cache_field');

Speeding up cache

AlexBorsody commented

I built a custom module that allows the user to upload an image and then clears the cache to display the image on the page. the process takes a very long time, as the image must be uploaded then the cache cleared. right now I am using cache_clear_all but is there a way to only clear the cache for images?

If you'd like to clear an

geerlingguy commented

If you'd like to clear an entire cache bin for database caches, regardless of the 'expire' value for a cached object, you need to do something like:

Providing an '*' for the first parameter tells the database cache interface's clear() method to truncate the particular cache bin.

Drupal cache_form table

Drupal 8

bwaindwain commented

Use the new cache API https://www.drupal.org/node/1272696





Comments are closed.