Skip to content

Commit 2c6196a

Browse files
authored
Update 8. Demonstrate the ability to manage the cache.md
1 parent 6843c70 commit 2c6196a

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

1. Magento Architecture and Customization Techniques/8. Demonstrate the ability to manage the cache.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,45 @@ How would you clean the cache? In which case would you refresh cache/flash cache
113113

114114
### Describe how to clear the cache programmatically.
115115

116-
What mechanisms are available for clearing all or part of the cache?
116+
To clear the cache programmatically you neeed to call next the methods:
117+
- [\Magento\Framework\App\CacheInterface::remove($identifier)](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/App/CacheInterface.php#L48) - remove cached data by identifier
118+
- [\Magento\Framework\App\CacheInterface::clean($tags = [])](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/App/CacheInterface.php#L56) - clean cached data by specific tag
119+
120+
##### What mechanisms are available for clearing all or part of the cache?
121+
122+
Dispatch a `clean_cache_by_tags` event with parameter of the object you want to clear from the cache.
123+
124+
Example: [\Magento\Framework\Model\AbstractModel](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Model/AbstractModel.php#L817) (afterSave, afterDelete methods)
125+
126+
```php
127+
<?php
128+
public function afterSave()
129+
{
130+
$this->cleanModelCache();
131+
$this->_eventManager->dispatch('model_save_after', ['object' => $this]);
132+
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
133+
$this->_eventManager->dispatch($this->_eventPrefix . '_save_after', $this->_getEventData());
134+
$this->updateStoredData();
135+
return $this;
136+
}
137+
138+
public function cleanModelCache()
139+
{
140+
$tags = $this->getCacheTags();
141+
if ($tags !== false) {
142+
$this->_cacheManager->clean($tags); // \Magento\Framework\App\CacheInterface
143+
}
144+
return $this;
145+
}
146+
```
117147

148+
Default `clean_cache_by_tags` event observers are:
149+
- [Magento\PageCache\Observer\FlushCacheByTags](https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/PageCache/Observer/FlushCacheByTags.php#L57) - if Built-In caching is enabled
150+
- [Magento\CacheInvalidate\Observer\InvalidateVarnishObserver](https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/CacheInvalidate/Observer/InvalidateVarnishObserver.php#L50)- if Varnish caching is enabled
118151

119152
###### Links
120153
- [Magento DevDocs - Magento cache overview](https://devdocs.magento.com/guides/v2.2/frontend-dev-guide/cache_for_frontdevs.html)
121154
- [Magento DevDocs - Configure caching](https://devdocs.magento.com/guides/v2.2/config-guide/cache.html)
122155
- [Magento DevDocs - Partial caching](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/partial-caching.html)
123156
- [Magento DevDocs - Full Page caching](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching.html)
157+
- [Magento DevDocs - Private content](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html)

0 commit comments

Comments
 (0)