![]() Cache contexts = (request) context dependenciesĬache contexts are analogous to HTTP's Vary header. What are the available cache contexts A quick trick to find the list of cache contexts you could use is to search the files. Globally, there are two ways to write a changelog:1 2 3 4 5 6 7 8 9 10 11 12 13 14. Why?Ĭache contexts provide a declarative way to create context-dependent variations of something that needs to be cached. I would be good if drupal by default come with a rule in. Some expensive-to-calculate data depends on the active theme: different results for different themes.By making it declarative, code that creates caches becomes easier to read, and the same logic doesn't need to be repeated in every place where the same context variations are necessary. When creating a render array that shows a personalized message, the render array varies per user.Then you'd vary by the theme cache context. Generally: when some expensive-to-calculate information varies by some environment context: vary by a cache context.Ī cache context is a string that refers to one of the available cache context services (see below).Ĭache contexts are passed around in sets (order doesn't matter) of strings, so they are typehinted to string.Then you'd vary (the render array) by the user cache context. To make dealing with cacheability metadata (cache tags, cache contexts and max-age) easier, Drupal 8 has CacheableDependencyInterface. They're sets because a single cache item can depend on (vary by) many cache contexts. Typically, cache contexts are derived from the request context (i.e., from the Request) object. Most of the environment for a web application is derived from the request context. After all, HTTP responses are generated in large part depending on the properties of the HTTP requests that triggered them.īut, this doesn't mean cache contexts have to originate from the request - they could also depend on deployed code, e.g., a deployment_id cache context. Second, cache contexts are hierarchical in nature. The clearest example: when varying something per user, it's pointless to also vary that per permissions (i.e., the set of permissions that a user has), because per-user is already more granular. A user has a set of permissions, so per-user caching implies per-permissions caching. Now for the most interesting aspect: if one part of the page is varied per user and another per permissions, then Drupal needs to be smart enough to make the combination of the two: only vary per user. ![]() a plurally named cache context indicates a parameter may be specified to use: append a colon, then specify the desired parameter (when no parameter is specified, all possible parameters are captured, e.g., all query arguments)ĭrupal core ships with the following hierarchy of cache contexts: cookies.That is where Drupal can exploit the hierarchy information to not create unnecessary variations. Protocol_version // Available in 8.9.x or higher. Note: To use the _front cache context in prior branches/releases, see the change record.Įverywhere cache contexts are used, that entire hierarchy is listed, which has 3 benefits: is_front // Available in 8.3.x or higher. no ambiguity: it's clear what parent cache context is based on wherever it is used.I have seen a few posts on setting cache context and cache tags but those don't seem to work.Comparing (and folding) cache contexts becomes simpler: if both a.b.c and a.b are present, it's obvious that a.b encompasses a.b.c, and thus it's clear why the a.b.c can be omitted, why it can be "folded" into the parent. When I switch the page back and forth between languages the title language does not change.Ī little digging and I see that Views cache context is based on Interface language but the Title is content so it needs to be based on Content language. The view is set to use Tag based caching. This block is then placed using Layout Builder on the Full Content view mode for this node type. This view has an id from url contextual filter and a single filter to filter on Content language set for the page (by URL prefix). Login with User 100 -> The block output is correct. It seems the cache tag is only set once for users with the same role or permissions. I have a very simple (block) view to list a node's title. Cache contexts need to be something Drupal can work out before it gets to the level where it's rendering content and etc so that's why you can have a context on route (cheap to inspect because Apache just gives it to you) but not node (expensive to vary on because to work out the node Drupal needs to run SQL) - because hopefully Drupal can just. The block output varies correctly by the logged in user but the user specific cache tag is not set correctly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |