7. 缓存系统Cache

龶龲龱drupal8龟龝龛龙龕龔龑龏龍龌龊龇龅龝龛龙龕龟龄龂齿齻齸齴龝龛龟龛龶齱齭齩齨齦齤龟齡齟齛齗齓齏齍齋齉齆齴齄齂齁鼿齨鼻鼺鼷龝龛鼶鼳龛龶龟鼰鼯齴鼫龇齦鼨鼤鼡鼯鼟鼝鼙鼕鼒鼎鼍鼉鼈齁齴鼄鼂黾齂黺黷黵黲黱黭黪龝龛鼨鼤黧黤齟齛鼙鼕鼎黣齴龝龛龟黡龇齦鼨鼤齴鼿齦鼨鼤黧黤龛黟龶黝黛黗黔黑黎黍黉黈黅黄黂麾齴麽鼄鼟龇麺齍齋麹麷齉齆齴龶齋麹麷麴麳麯龝龛麫麧麥鼯齴drupal8龟龝龛麢鼒麠黡麟麜鼿齨齦齤龟鼄麢鼒麛

Symfony鼶鼳齍麍龝龛麌麋齴drupal8麈麄麂鹾黉黈龌龝龛龙龕齴druapl8龟龝龛龙龕齂鹻黡龝龛鹷鹵鹳黍齴鼂鹰龝龛鹬黝龙龕麯鹩鹦鹥齴鹣鹟鹜黑鹙鹖鹦鹥鹒鹎鹌鹦鹥鹒鹉鹅鹦鹥鹁鹁齴鸿鸽鼈鼳鼰鼯龝龛龟鹦鹥鼂鸹黧黤龝龛齴鸵鸴龟鼂鸰鼺 鹦鹥鸬 鸪龛黟龝龛鹦鹥鸧鸣鸢鸠鹒鸞鼟鸜鸘麋鹁鸵鸴齂鼟鼝鹦鹥鸬鸗龛鸔齴龶鹦鹥鸬麯黤cache_鸇龔鸆龟鹦鹥鸃黂龛黟龟黡龝龛鹦鹥齴鸁鷿鼳龇齦鷽鷹cachetags龟鹦鹥鸃鼺鸪鸗龛龝龛龙龕龟鷪鷩鹦鹥齴鼫鷨鷥鷡鷝龝龛龟鷛鷙鷡鷘鷗龏鷓黧齴鷏鷎cachetags鸃齴鹟鼤鹻鷡鷘鷍龇齦龝龛鸃鼷齂鼯鷡鷝cachetags鸃齴鷡鷝cachetags鸃黺鷉鷆鷃鼳龝龛鸃鷀黍龟鹦鹥鶾鶺鸧鶸鶾鶺鼄鶵齴齂黺鷉鷆龙龕鶲鶮鸔齴鼫龙龕鶬鼝鷛鹩鶨鶦鷛黺鶣黈龝龛龙龕鶡鶞鶛黝鹦鹥齴麽鼄鶙鶖龝龛鶺鶓齴黧黤鷡鷘鷗龏龇龅麛

drupal8龟龝龛龙龕黧黤鼟鼝黝鶏鶌鶋龝龛鹦鹥齴齂鶸鶸黡鹦鹥鸬齴鹰黧黤鼨鶉鹎鹌鷿鶇齋齉龝龛鹁鹁齴龅黍龲龱鼂龟黉黈鶅鷝齡

龶鷝龊drupal8龝龛龙龕鷛鶄鶁鵿鼂黉黈龌齨鼙鵾鵻鹜齴龇黡鹟鵸龛鸗鵴鵰鵬鹦鹥鹒鸞黡鹟鵸鵩鵦龝龛鹦鹥黡鵢鶨鵟齿黧龝龛齸龂麛

鵛齩鹟鵸龛鸗鵴鵰鵬鹦鹥drupal8鼳齨齦鵗鵔齡 cache_bin cache.backend

bin黧黤鷝龊鸇龛黟鹦鹥龟鵅鵄鹒鹜黑鹒鹦鹥鵀鹁鹁齴鹣鹟龶鸵鸴黉黈麯鹦鹥鸬龟龇齦龝龛鸃鴼黡龇齦bin,鼿齦鸃鴼黡龇齦鵅鵄齴鷀黍黡龇鴷龇鴷龟鹦鹥麛backend鴼黡bin龟鴳鼻鴯齴鼂黡龇齦鴬鴫齴黅鴧鸗龛鹒鴥鴡鹒鵰鵬鹒鶾鶺bin鷀黍龟鹦鹥齴鸇鴞黪鵰龌backend鼿黪齦鴚鴖齴鴓鴒黡鴎鴊龟鴇鴅齴鼂鴄鴬齩龔鴊龝龛鼄鶵鸧龝龛鴂鷝黗黔黑鹒鴀鳼黑龝龛鹁鸔齴龶龙龕鷀黍鳸齦bin鴬鹵鳵龇齦backend齴鼿鶏鴬鹵鵛龙黧黤龶麳鳲鹎鹌鸘麋settings.php鷀黍麢鹌鸧龅黍鳰鸔齴鳭鹎鹌龟鳫鳧龅鳤鼺鸵鸴鹎鹌齴鹻鼡鼯鳡bin鳟鳝龝龛鳙鳗鴼鼉麂鷥鼈齁backend

鵛齩鹟鵸鵩鵦龝龛鹦鹥黡鵢鶨鵟齿黧龝龛齸龂鼳鳓齦鵗鵔齡 CacheMaxAge CacheTags CacheContexts
CacheMaxAge 齡鴂鸃龝龛鳅鼙鼳鶺鷛鶦
CacheTags 齡龝龛鳂鳀鴂鸃龝龛龟鹦鹥黡鴞黪
CacheContexts 齡龝龛黎龅鸘鴂鸃鲿龇鹦鹥龶齂鲿鳫鳧龅龟鲽鲼齴黎龅鸘鼙黝鸪麂鶄鲺鴬鴫鸧鲹齂黡麄鶇鸔齴鹣鹟鶄鲺龟鲷鲴鹒鹦鹥鲲鶋鹒鼺鲯鴂鷝鹒鼺鲯鹒ip鹒cookie鹦鹥鹁鹁齴鲿龇鹦鹥龶鼿鲫黎龅鸘龅黧黤鸃黈鲩齂鲿龟鲽鲼麛
龙龕鹙鹖龌龇齦黧龝龛齱齭鲨鲤鹟鼤龇齦鹦鹥黡黧龝龛龟齴黭黪黧黤鲡鼈黉黈鲝鲨鲤龟黧龝龛鲜鹦鹥齴鷀黍鹙鹖龌黤黎鳓齦鵗鵔龟鲘麛


龅黍鲖龇鲖鲔鸠鴂鲑黉黈齡


drupal8鲏鲎Cache龙龕龶 \core\lib\Drupal\Core\Cache 齴鼂齍麍鳅鲏鲎龟鲆鼉齴麍鲄鷗龝龛鲂鵾齏鼡鼯鸰鼺龝龛龟鲁鶌鸰鼺麛
鷃鼳龟backend鸹鼡鼯黉黈 CacheBackendInterface 鲨鲤齴鸁鲨鲤鷀黍鹙鹖龌 backend 黧鼺龟鱻鼺鶌鱷鸧龶drupal8鷀黍鷃鼳龟鱴鴚鸹鼳鱲鹙鱱鼷齴鲨鲤黤Interface鼨鱯齴齻鱭黤Trait鼨鱯齴鳙鳗鶌鱷黤Factory鼨鱯齴龇齦鸘麋龇齦鱨齴鸘麋鴚鵴鱨鴚龇鷆齴鷃黤鸿黧黤鱥鹥鸘麋鴚鼙鷆鱣鵦鸘麋龟鼺鱡齴黲黝鱲鹙鱱鼷鶄鲖鱟鱞鸘鱚鸔齴鱘齩鲨鲤龟鱕麋麢鼒黡鼙鱒鱕麋麢鼒龟鱐鱎鱌龇齴鼺鲯齂鼡鼯鱈鱆鲔鸠黉黈齴鱃鴬鲨鲤齍麍龟鶌鱷鸰鼺鷓黧麛

drupal8鸵鸴齍麍龌黤龅Backend齡

Apcu4Backend ApcuBackend DatabaseBackend MemoryBackend MemoryCounterBackend NullBackend PhpBackend

鸁鷿鼳齨齦齻鰹龟Backend齡

BackendChain 鳡黝齦Backend鼨鶉齄鸪鸰鼺齴鰷鰴龇齦Backend鰰

ChainedFastBackend 鸇龌鰮鰭齸鼉鳡龇齦鰫齉Backend齏龇齦龇鰪Backend鼨鶉齄鸪鸰鼺

黤黎 Backend 鸹鰧 Backend 鳙鳗黅鴧黉鰦鰣鸧 BackendChain 鴡鷿鸔齴鸵鸴鹙鹖龟 Backend 鳙鳗鼳齡

ApcuBackendFactory ChainedFastBackendFactory DatabaseBackendFactory MemoryBackendFactory NullBackendFactory PhpBackendFactory

鼿鲫鳙鳗鸹黉黈龌 CacheFactoryInterface 鲨鲤齴鲝鲨鲤鰠鰞鰝齴鶸鶸鹙鹖龌龇齦get鶌鱷鲨鰙bin鰗鹦齴鼂鰔鵬Backend鴬鴫齴鸵鸴龟bin齏Backend鼳龇鰒鴬鹵鵛龙齴鹟鵸麂鹙鹖鼿鶏鵛龙鴖鰑龶麳鳲鹎鹌鸘麋settings.php鷀黍鹙鹖鷓黧齴鲲鶋鹟龅齡

$settings['cache']['bins']['鸿鹙鹖龟bin']="龝龛鳙鳗黗黔龟黗黔ID";

鸵鸴龟鴬鹵鵛龙鹙鹖龶CacheFactory鱨龟get鶌鱷鷀黍齴齓鴂鲑鯼鯻鯹鵻鹜鱞齡

鼿鷀鲖龇鲖鸰鼺鳅黝龟DatabaseBackend齴鼂黡鸵鸴Backend齴黅鴧鹦鹥鸬龝龛鴳鼻齴龶DatabaseBackend鱨鷀黍鹙鹖龌鹦鹥鸬龝龛bin龟鹦鹥鼨鯷齴龍鲖龇鲖鸵鸴龟鹦鹥鸬龝龛鸃齴鼂龏鸹黤cache_鸇龔鸆齴鸃鯳鯯鹟龅齡

cid 齡龝龛id齴龇齦255黤鵻龟ascii鯳鯦鯢

data 齡龝龛龟鹦鹥鵻鹜

expire 齡龝龛齁鵟鷛鹩鯟齴鯜鯚鸇-1

created 齡龝龛鯔鯑龟鷛鹩

serialized 齡龝龛龟鹦鹥黡鵢鯍鶨鯊鯆鰣

tags 齡龝龛鳂鳀

checksum 齡龝龛鹦鹥鼳鶺齸鯃鮿鲘

public function get($bin) {  
    $cache_settings = $this->settings->get('cache');  
    if (isset($cache_settings['bins'][$bin])) {  
      $service_name = $cache_settings['bins'][$bin];  
    }  
    elseif (isset($cache_settings['default'])) {  
      $service_name = $cache_settings['default'];  
    }  
    elseif (isset($this->defaultBinBackends[$bin])) {  
      $service_name = $this->defaultBinBackends[$bin];  
    }  
    else {  
      $service_name = 'cache.backend.database';  
    }  
    return $this->container->get($service_name)->get($bin);  
  }  

龶龔黍龑龏鮽齁龌鵛齩鹟鵸鵩鵦龝龛鹦鹥黡鵢鶨鵟鼳鳓齦鵗鵔齴黭黪龙龕龶鼿齦鹦鹥鸃鼨鯷鷀黍鹟鵸鮻鮹鲩鸪鴖鰑鴬鹵鵛龙鹟龅齡

cid鴬鹵鳵黎龅鸘齱齭齴齂鲿龟黎龅鸘麌鶉鼈齁龟龝龛cid齂龇鮵齴鸰鼺麯龟cid齏龝龛bin鷀黍龛鸗龟cid齂龇鮵齴龛鸗龟cid黡鯍鶨鮲鮱鹁鶌鱷鮯鮫鼈齁龟龇齦255黤鵻龟ascii鯳鯦鯢齴鮯鮫鶨鮩鯼鯻鯹鵻鹜鱞齡

鸰鼺麯龟cid黡鰧 CacheContexts 龝龛黎龅鸘麌鶉鼈齁


expire鴬鹵鳵鷛鹩齱齭齴鮧鮤鶨鵟龟鷛鹩齴鼿齦鰞鰝齂鼰黝鳰麛


tags鴬鹵鳵鹦鹥鶏鱨齱齭齴龇鴷龝龛黧鼉鰧黝齦鳂鳀鴬鹵龟鹦鹥麌鰴齴鲄麯鮡鵸龇齦tag鴬鹵龟鹦鹥鶣鮝鮛鰣鸹黺鮙鰴鲝鴷龝龛鶾鶺齴鳸龇齦tag鴬鹵龟鹦鹥黾黧鼉鮖黝鴷龝龛鸰鼺齴黭黪鼫tag鴬鹵龟鹦鹥鶣鮝鮛鰣鷛龑龏鹟鵸齿鷛鼈鱈鼿鲫龝龛鴷齤鶨鵟龌鴖鰑鼿黡drupal8龝龛麢鼒龟龇齦鱐鮓齴鮏鮍鴼龶齩checksum鼳鶺齸鯃鮿鲘齴黎鸘鮽鶨龙龕麯龛龶龇齦龝龛鷪鷩鹦鹥鸃齴鸃鴚鮉黡cachetags齴鷀黍鼳齨齦鯳鯯齡鳂鳀齿鶾鶺鮇鹦齴鳸鼫龇齦鳂鳀鴬鹵龟鹦鹥鮃鮝鮂鰮鷥鼻齴黭黪鶾鶺鯳鯯鴼黺鮀龇齴鼄checksum龟鲘鴼黡鲝鴷龝龛鷃鼳鳂鳀鴬鹵龟鶾鶺鯳鯯鲘鱌齏齴麠鴼黡鮽鮡鴇龇齦tag鴬鹵龟鹦鹥鹻鼯鮃鮝鮂鰮鷥鼻齴黭黪鴼黺魽齄鸰鼺齁鼂龟龝龛鯃鮿鲘鮛鰣齴龙龕鴼黧黤鱥鹥鼿齦鸪鵩鵦龝龛黡鵢鶾鶺齴鼿齦鼒鼎鯃鮿鲘齏鵩鵦龟鳙鼻黡鰧DatabaseCacheTagsChecksum鱨鸪麈鰴龟齴鼂鼒鼎鲩鼫龔鯃鮿鲘魹齏龝龛bin鷀黍龟鯃鮿鲘鹣魶齴齂鲿鼷龝龛鶾鶺齴魳魳鹣鶾鶺龟鯃鮿鲘鼙齴鹟鼤龝龛龟鹦鹥鳭鳂鳀鼷鯃鮿鲘鯜魱鸇0麛


鹦鹥鸬bin鷀黍鳸齦鴷齤鴼黡龇齦龝龛齴drupal8鹰鰠魬鲎龟鸇龑龏鷹龌鼟龇麺龟鳙鼻齴龶龇齦龝龛鴷齤鷀黍鸗龛鰠鼙龟龇齦鹦鹥魫齴鹦鹥魫黤鹦麌龟鶌鶋鸗龛龶龝龛bin龟data鯳鯯齴鴬鼿齦鹦鹥魫龟鴳鼻drupal8齍麍龌 CacheCollector 鱨齴鼂黉黈 CacheCollectorInterface , DestructableInterface 鲨鲤齴 Destructable 鲨鲤鼺齩黗黔龶魧魤鷛鷹龇鲫魣鱯鳙鼻齴鱨魟齩魝鯷魛鹦麛


黤黎鴼黡鹦鹥鸬龝龛龟鹦鹥鼨鯷齿龛鵰鳫鳧齴龅黍鸪鲖龇鲖鹦鹥龟黧龝龛齸齡


鼡鼯龝龛龟鹦鹥鴬鴫鮖魘魗龌黧龝龛鲜鹦鹥鴬鴫齴黭黪鼂鴼鲔魕龌黧龝龛齸齴黧龝龛鲜鹦鹥鴬鴫黉黈龌黧龝龛齱齭鲨鲤齡 CacheableDependencyInterface 齴鲝鲨鲤鷀黍鶸鶸鹙鹖龌鳓齦get鶌鱷齴魒魏鴬鹵齩黎龅鸘鹒鳂鳀鹒鶨鵟鷛鹩齴鼳龌鼿鲫鶌鱷鴼黧黤鱈鱆鼡鼯龝龛龟鹦鹥鴬鴫鹟鵸龝龛齴 CacheMaxAge CacheTags CacheContexts 麠鮉鱌鸇黧龝龛魌齸齴 CacheTags CacheContexts 鸹黡鯳鯦鯢鸃鮤麛龶鮡鵸麴麳龙龕麯黧龝龛鹦鹥黂鼳鼿鳓鶌黍龟齱齭麛
\core\lib\Drupal\Core\Cache\Context 麯鹙鹖龌麧鼺龟黎龅鸘齱齭麛

protected function normalizeCid($cid) {  
   // Nothing to do if the ID is a US ASCII string of 255 characters or less.  
   $cid_is_ascii = mb_check_encoding($cid, 'ASCII');  
   if (strlen($cid) <= 255 && $cid_is_ascii) {  
     return $cid;  
   }  
   // Return a string that uses as much as possible of the original cache ID  
   // with the hash appended.  
   $hash = Crypt::hashBase64($cid);  
   if (!$cid_is_ascii) {  
     return $hash;  
   }  
   return substr($cid, 0, 255 - strlen($hash)) . $hash;  
 }  

黤黎鴼黡drupal8龝龛龙龕龟鲏鲎齴龅黍龲龱齨齦鹵鼺鲏鲎龝龛鲆鼉龟鲂鵾齴鷗龏黡 Page Cache Dynamic Page Cache 齴鼂龏鸹黡龙龕鸵鸴齍麍龟鲂鵾齴魊齂黧鵛魇齴魆齩 core\modules 齴龶龙龕魄鷝鴎黛-鹉鹅齡鲂鵾鯆鸃麯鼉魀齁麛


Page Cache鲂鵾鸇鬾鴚鼺鲯齍麍鹳黍龝龛齴龶龑龔黍鶣龟鬽 HttpKernel鬵鬱 鬰鷀黍鼳齍齁齴鼂黉黈龌 HttpKernelInterface 鲨鲤齴魌齩http麯鹩麋齴龝龛龟鹦鹥魆齩鹦鹥鸬鸃 cache_render 鷀黍齴鸰鼺URI齏鶄鲺鲲鶋鼻鸇cid齴鼿齦鲂鵾龟bin鴼黡render齴Backend黡DatabaseBackend麛


Dynamic Page Cache鲂鵾鸇鮡鴇鼺鲯齍麍鷥鲼龟龝龛鹳黍齴鹦鹥鸗龛齩鹦鹥鸬 cache_dynamic_page_cache 鸃齴鼂鬨鶨鸰鼺鬥麋鬢鬟鬜鸜龶龶鷥鲼鹷鹵鮃鮝鷛龝龛鹦鹥齴鵛齩鬥麋鬢鬟鬛鶄鵟鬙鴎鬗鬔鬓龙鯆鬒鬎麛


鹟鼤鸿鲖齁鼿鷀齴鸿鹵鲝鶵鯍鼙鷆龌龊龌drupal8龟龝龛鶬鼻鬜鸜齴鬍鬌鴼黡鬈鬆鬃鵟齴鬂鼙髿齟髾鰫髻齴髸鼎髵龶齟龔鲩龌鼿髲鬒鬎齴鼳齂鬍鲁鶌髱髮髭鲴麛

本书共83小节:

评论 (写第一个评论)