The DX Files: Static caching in Drupal
This is part four of my series, The DX Files: Improving Drupal Developer Experience. I started this series with fairly simplistic suggestions. They proved not very popular and some of them I agree were of questionable benefit due to PHP’s nature. I was pleased to discover, however, that they nevertheless had quite an impact on raising the visibility of “Developer Experience” within the Drupal community. I am therefore ready to move on to some of the more complex DX issues in Drupal.
The most important DX change Drupal needs to make is switching from a form-driven model to an API-driven model. There are many parts to such a change. Today’s topic: static caching.
A simple example
Much static caching in Drupal core and contrib is based on the assumption of a “normal” page request in which only a fairly controlled set of operations can occur during a single execution. Consider this fairly typical pattern (from D7 HEAD as of today):
<?php
function taxonomy_get_term_data($tid, $reset = FALSE) {
static $terms = array();
if (!isset($terms[$tid]) || $reset) {
$terms[$tid] = db_query('SELECT * FROM {term_data} WHERE tid = :tid', array(':tid' => $tid))->fetchObject();
}
return $terms[$tid];
}
?>This works great so long as no term ever changes during the course of execution. As soon as some code calls other taxonomy “API” functions, the interface can fail. Suppose we have a term whose tid is SOME_TID and whose name is “my name”. Consider:
<?php
// load SOME_TID and print its name
$term = taxonomy_get_term_data(SOME_TID);
print $term->name . "\n";
// change the name and save the term
$term->name = 'some new name';
taxonomy_save_term($term);
// load the term again and print the new (?) name
$term = taxonomy_get_term_data(SOME_TID);
print $term->name . "\n";
?>This program outputs “my name” twice, not “my name” and then “some new name.” Bottom line: The taxonomy_save_term() has no effect during a single page request!
It is horrible DX to have “API” functions that behave this way. Sadly, not just the taxonomy module has this problem; the pattern is widespread within Drupal.
The $reset non-solution
The typical solution within Drupal is an optional “clear the cache” argument like $reset for taxonomy_get_term_data(). When $reset is TRUE, the static cache is erased. This would be fine if it were always used by all the taxonomy_*() functions to maintain the API’s specification (in that case, the “taxonomy class” would simply be doing internal housekeeping on “private member variables”), but it isn’t. Callers using the taxonomy “API” need to know to pass the $reset argument themselves.
The problem gets even worse when you consider all the myriad contrib modules that use the same technique. It seems everybody has their own static cache and you need to call the right magic “clear the cache” function to get anything done. The API docs rarely spell this out and it is a PITA.
A real-world example of where this can bite badly: Try writing hook_update_N() functions that create or change CCK types, Views, taxonomy terms, etc. Disaster! Why should I have to figure out to call views_invalidate_cache(), content_clear_type_cache(), etc. etc.?
Static caching-induced bugs
The technique can also lead to bugs/security vulnerabilities when the static cache, conceived to operate in a normal page request, is used in a longer-running program.
Case in point: drupal_validate_form(). Here’s an excerpt:
<?php
function drupal_validate_form($form_id, $form, &$form_state) {
static $validated_forms = array();
if (isset($validated_forms[$form_id])) {
return;
}
_form_validate($form, $form_id);
$validated_forms[$form_id] = TRUE;
}
?>drupal_validate_form() caches the fact that a form successfully validated. This means that you cannot call the same form twice via drupal_execute() in one execution safely. The second and all successive calls will not call the form’s validate function. Instead, they will always invoke the submit handler.
Practice Statics
Sometimes the operation being cached is not even that expensive or called that often. It’s just being cached “for practice.” Here’s an excerpt from node.module:
<?php
function _node_revision_access($node, $op = 'view') {
static $access = array();
if (!isset($access[$node->vid])) {
// compute and cache the access for $node and $op here
}
return $access[$node->vid];
}
?>This is another example of static caching introducing a potential security hole. The $access static cache is based on $node->vid but not on $op. Suppose an attacker has permission to view a node but not delete it. If the attacker can cause _node_revision_access($node, 'view') to be invoked before _node_revision_access($node, 'delete), he can perform an unauthorized action.
This would be an important security hole in Drupal except it turns out that _node_revision_access() is never called more than once in a single page request. But… wait a minute! If the function is only ever called once per page request, why does it use a static cache at all? It shouldn’t.
This is just ridiculous. The code is unnecessarily complex in order to optimize a code path whose performance was obviously never measured and as a result introduces what would be a security hole but only if the static cache was ever actually used!
Solving the problem
So, what should we do? My suggestions:
Eliminate unnecessary static caching completely. Unless and until you have empirical data proving that a particular static cache makes an important improvement in real-world usage scenarios, just don’t use one. If you already have a static cache without such data, remove it. The drupal_validate_form() and _node_revision_access() examples fall into this category.
Maintain API integrity. If you must implement a static cache inside an API function, fine, but make sure that the API maintains its documented behavior within a page request. This means you have to make sure to update every “read” static cache in your code every time a “write” function is performed. Do NOT leave this to your callers. In the taxnomony example above, taxonomy_save_term() should invalidate or update the taxonomy_get_term_data() cache entry for the affected term.
http://drupal.org/node/254491 proposes a centralized static cache API for Drupal. I haven’t reviewed it recently, but it will probably help. However, even if it works really well, remember that not doing static caching at all will lead to simpler, less error-prone code. If you do not have a solid reason for using a static cache, just skip it until you do.
- Topic:
Comments
I might get kicked for saying
I might get kicked for saying this, but this might be a time to bring in some OOP. (me ducks).
First, imagine a taxonomy class and a factory function...
<?phpclass Taxonomy {
function save() ...
}
function
taxonomy_load($tid) {static $taxonomy = array();
if (!isset(
$taxonomy[$tid])) {// load the taxonomy.
}
return
$taxonomy[$tid];}
?>
Because of the way PHP references objects in PHP 5.2 we can do things like....
<?php$tax = taxonomy_load(123); // Get a taxonomy object $tax->name = 'testing'; // Change the object
$tax->save(); // Save the changes $newtax = taxonomy_load(123); // This newly loaded object contains the changes.
?>
We can even do operations like
<?phptaxonomy_load(123)->name = 'testing';
taxonomy_load(123)->save();
?>
I'm going to go hide now before anyone beats me up.
A real-world example of where
HA! You should see install profiles.
Many times its easier to just create duplicate functionality in your profile for these problem functions. See crud.inc for an excellent example.
I should have put this in my
I should have put this in my first reply, but in all seriousness, the crud.inc from the install_profile_api is a must have for anyone doing heavy lifting from hook_update_N. It would be a great model of how the exposed API for many things in Drupal should work.
Thanks for bringing this up.
Thanks for bringing this up. I have been bitten by this anti-pattern a couple of times. It is super frustrating. It's like Drupal decided it need a way of accidentally declaring a method final.
And yet, I'm sure that's how
And yet, I'm sure that's how we'd have done it had Drupal began its life with PHP 5 OOP features. Image if you could create a new node whereever by doing this.
<?php$node
= new node();print $node->nid;
print "WoW";
$node->title = 'bob';
$node->teaser = 'is a';
$node->body = "chump";
$node->save();
?>
Instead we have to pretend its a form submit...
<?php$node = array(
'type' => 'page',
'title' => 'bob',
'body' => 'is a chump",
);
drupal_execute('node_form, $node);
?>
I know it works, but to say its an ideal and intuitive way to simulate saving a node outside of a form is to have a good sense of humor. For one, look how many opportunities there are to make a mistake!Can drupal_execute report back about a missing variable, or something being set that you can't set? Hell no's.
Its a "drupalism", a combination of legacies, limited development hours, dependencies, and supporting PHP 4 for some time.... Not to say its not brilliant given the circumstances, but I don't think its productive to immediately poopoo on approaches that are less eccentric than a drupalism.
Who you hiding from?
?>
Good news,you have great
Good news,you have great website.
Thanks, your website is very
Thanks, your website is very helpful
miss you You will surprise
miss you
You will surprise to find the high quality tiffany jewelry in much.Everyone will focus on the shinning of
tiffany co jewelry without awareness of
rovide 100% sterling silver Tiffany jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry.
discount tiffany jewelry, Tiffany Jewelry, Tiffany Jewellery, Tiffany Silver, Tiffany, Tiffanys, Tiffany's Jewelry.We Provide a wide range of fashionable
tiffany co jewelry, including
In 1950 Tiffany's had its biggest boost in popularity as Truman Capote's Breakfast at Tiffany's was published. Tiffany's was shot to worldwide fame in 1961 when the film adaptation staring Audrey Hepburn was released. This film and its star became icons, as did Tiffany's
Tiffany Bracelets
Tiffany Rings
cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store
Tiffany Earrings
Tiffany Necklaces
Links Of London
Links Of London
Official website for Ed Hardy
Official website for Ed Hardy Eyewear. The lifestyle Ed Hardy brand is based on the original Ed Hardy clothing, accessories, energy drink, Ed Hardy randed by
ed hardy ed hardy clothes
Ed hardy online store, whether you are looking for fashion Ed Hardy Clothing items, then you come to the right place, we offer a variety of ed hardy items.
Don cheap ed hardy is an American tattoo artist born and raised in Southern California in 1945. A pupil of Sailor Jerry, Hardy is recognized for incorporating.
Selling ed hardy men longsleeve,ed hardy swimsuit,ed hardy men swimsuit,ed hardy boot.
ed hardy clothes
west ed hardy,edhardy discount,ed hardy clothing,ed hardy shoes Commodity New styles have just arrived!
ED Hardy womens Hoodies
ED Hardy Womens Shoes
Keep up the good work, and
Keep up the good work, and post more articles like the one you have posted above.
Not to say its not brilliant
Not to say its not brilliant given the circumstances, but I don't think its productive to immediately poopoo on approaches that are less eccentric than a drupalism.
human services degree | Psychology Diploma | PhD sociology
It's like Drupal decided it
It's like Drupal decided it need a way of accidentally declaring a method final.
PhD history | Music degree
I have been bitten by this
I have been bitten by this anti-pattern a couple of times. It is super frustrating. It's like Drupal decided it need a way of accidentally declaring a method final.
That little blue box -
That little blue box - everyone woman dreams of seeing it come their way. tiffany jewelry jewelry is world renowned for its stunning quality and top of the line artisanship; however, it also has a reputation for having a price tag that is way out of most people's leagues.
discount tiffany jewelry That doesn't mean you can't get that Tiffany look you want at a price you can afford, however. Thanks to replica Tiffany jewelry, you can look like you are draped in Tiffany jewelry, and yet still have the money left in your pocket to go out and show off your looks.
Thanks, your website is very
Thanks, your website is very helpful.
mens apparel
ralph lauren polo
wholesale tommy hilfiger
wholesale lacoste t shirts
abercrombie & fitch clothing
calvin klein underwear
diesel jeans
ThankS..muhabbet|chat
ThankS..muhabbet|chat siteleri |sohbet|yonja|forum siteleri|mirc indir|sohbet|yonja|sohbet|sohbet|
kizlarla sohbet|dini sohbet|islami sohbet|sohbet/sohbet odalari|sohbet||Sohbet|sohbet|
netlog||sohbet|netlog|yonja|sohbet chat
mirc
cinsel sohbet
Thanks for bringing this
Thanks for bringing this up.
scores lives
soccer live score
Handball live scores
Premier league live score
Live score
Thanks, your website is very
Thanks, your website is very helpful.
Essay Writing Service
Buy Term Paper
Buy Research Paper
Essay Service
Online Term Paper
Research Papers
I should have put this in my
I should have put this in my first reply
Generic Viagra
kamagra supplier
Kamagra Wholesale suppliers
I luv it kamagra kamagra
I luv it
kamagra
kamagra 100mg
Thanks for this long post.
Thanks for this long post. I'll try those codes on Drupal!
Tom
Bed & breakfast rome - rome b&b
This is some very valueable
This is some very valueable information, thank you very much.
There will never the last
There will never the last piece of jewelry in women's casket, for they could not resist the temptation of brilliant jewelry tiffany necklace All in all, take the above information into consideration when choosing your right jewels so you will be lucky and charming by wearing them. tiffanys Since jewelries are believed to make the finishing point of one's dressing, which could set off one's personalities and characters. tiffanys However, to match jewelry is a complex art that many people have no idea about how to properly wear and properly maintain jewelry. tiffany chains The following are some tips which are in the hope of guiding and giving aid to the choosing and matching jewelries. tiffany & co It is essential that the jewels you chose are go well with your skin color, age, dresses, occasions, etc.
The information you posted
The information you posted about replica handbags
replica bags is so useful, I am expecting for your next post.
Good post thank for share :)
Good post thank for share
:)
this definitely makes alot of
this definitely makes alot of sense.
wedding tips
free wedding information
inexpensive wedding ideas
free credit information
help with credit score
help with debt
credit help tips
In the mid 19th century,
In the mid 19th century, Louis Vuitton (pronounced Loui Vuitto) was a renowned trunks and LV Website. Entering into the 20th century, the company expanded in terms of locations and financial success. Beginning in the middle of the century, it entered the fashion world, integrating its Louis Vuitton Monagram into purses and bags. Its merger to create LVMH became a milestone step, and from then on, LV came to acquire its luxury fashion image known today.
Finding a good perder peso
Finding a good perder peso and ways to diabeticos as well vegetarianos
dietas and ways to diabetes
dietas and ways to diabetes as well dieta vegetariana
skin care products | scar surgery
How are you! You know this is
How are you! You know this is the first site, which i really like :) GOOD!.
I am from Salvador and also am speaking English, tell me right I wrote the following sentence: "Still, this round may be explained in child to dealing the cultures of first and unpopular use views where there may be collectively subject other access in earning them."
With best wishes :-D, Bali.
Hi guys. I was the kid next
Hi guys. I was the kid next door's imaginary friend.
I am from Zambia and also now am reading in English, give please true I wrote the following sentence: "Not, the habits of these strata were prior called auditors."
Best regards :(, Benjamin.
tiffany jewelry hot sale now
tiffany jewelry hot sale now with discount. Tiffanyhotsale provides the best Tiffany & Co jewelry, including Necklaces, Bracelets, Earrings, Pendants and so on .
Tiffany Sale Provide 100% sterling silver tiffanys jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry
Stunning silver jewellery from tiffany, Gucci, Links of London; quality designer silver jewelry at online prices. We have silver Bracelets, Solid Silver ...
Nice information.Googasian
Nice information.Googasian p.c malpractise
thank you greats post oyun
thank you greats post
oyun oyna
oyun
thank you greats post oyun
thank you greats post
oyun oyna
oyun
Herve Leger womens fashions
Herve Leger womens fashions at ShopStyle. Shop popular stores to find Herve Leger womens fashions on sale - all in one place. Create and share looks based
Herve Leger
Cheap Herve Leger
Jewelry,Necklaces,Jewelry Necklaces,Links of London Necklaces,online sales a variety of world famous such as Links of London etc,with competitive price.
Links of London , the leading British contemporary jeweller was founded in 1990 by jewellery designer Annoushka Ducas and her husband John Ayton.cheap Links of London jewelry at online linksgif UK store, including Links of London Necklaces, Links of London Charms, Links of London Earrings. Links of London Silver Sweetie Bracelet Medium
Links of London Charm
Links of London
Thanks for great tips. Web
Thanks for great tips.
Web design
pocket watches pocket watch
pocket watches pocket watch mens pocket watches antique pocket watches
Beijing Tour Beijing Tours
Beijing Tour Beijing Tours Tour In Beijing Tours In Beijing Travel In Beijing Beijing Coach Tours Beijing Bus Tour Beijing Bus Tours Beijing Coach Tour Beijing Day Tours Beijing Day Tour
Shop for a nba jersey at
Shop for a nba jersey at SFGate Fan Shop:Why buy a nba jerseys from us? Our competitive prices on discount nba jerseyare a good reason.
I am from Zambia and also now
I am from Zambia and also now am reading in English, give please true I wrote the following sentence: "Not, the habits of these strata were prior called auditors." games
The information in the post
The information in the post is nicely written, I always love to read this kind of stuff. The quality of content is really appreciable. Thanks for sharing your knowledge with us.
Tom Ford Eyeglasses
this is a great article!
this is a great article! fotograf zdjecia slubne and fotografia slubna bielsko Katowice krakow fine art. Psycholog on Line korzysci pomocy online fotografia dziecieca zdjecia dzieci, fotograf slubny fotografia dzieci depresja stres psycholog i pomoc psychologiczna pomoc psychologa and internetowy psycholog psychologia internetowa poradnia psychologiczna and Psychologia and psycholog online psycholog on-line pomoc psychologiczna przez Internet and psycholog online psycholog internetowy and psycholog przez internet
Over the centuries the Swiss
Over the centuries the Swiss is known as the best watchmaker of quality watches. Their splendid timepieces are considered as a combination of precision and art. Their unique way of manufacturing timepieces help established a solid position in the world. Well, why not purchase a replica Swiss watch? Recently, many people swift into purchasing Swiss replica since the authentic timepieces are far beyond their budget. However, most replicas are known as having poor quality. It can compete with the original one in every aspect.The replica Swiss watches manufactured by the talented Swiss specialists are a combination of advanced technology and superior materials, which make them at the top of the buying list when people are settling for replica watches. The Swiss are specialized in creating almost all kinds of watches, including Omega replica watch, Mont Blanc, Rolex, Breitling look-alike, Patek Philippe, Cartier or Jacob & Co. You should check out the Swiss replica watches offered to see which one you like most. And before purchasing, surf the internet to know more details of the watch that you are intending to buy. Always be cautious in every step of purchasing. Though some watches might say that they are made in Switzerland, however, they are made in China or other places. In addition, you might also find that some web sites might offer you a discount, be careful and check every detail of the watch in order not to be treated. In addition, they can endure the test of the time. Buying a Swiss replicas is really a good investment. All the timepieces offer a one year warrantee, which can ensure you the good quality of the timepieces.
I wrote the following
I wrote the following sentence: "Still, this round may be explained in child to dealing the cultures of first and unpopular use views where there may be collectively subject other access in earning them."Belford lawsuit
Yeah.You do a excellent
Yeah.You do a excellent job!Congratulation.You can keep on going.
Best regards.
Its matchless & exacting
Its matchless & exacting purpose, its classic and superb theme, and its matchless arrange in silver diligence seal all creature beings, especially girls. Choose some that can be tattered everyday as family the best expression of links of london storesdear and emotion. How many time have we current you with newest London grandeur authentic silver jewels sets. Every model is well-crafted in front of relations offset and shrunk back at the adore for beauty is a type of all creature beings, especially girls. Every example is well-crafted in 100% actual Silver.
It was a very nice idea! Just
It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.
xbox 360 repair guide|watch satellite tv on pc|quick weight loss tips|xbox red ring of death fix|wow gold guide
Really nice post,thanks for
Really nice post,thanks for sharing....
Links of London is the
Links of London is the everlasting expression of like. Love her; give her links of London, the voucher for engagement and wedding. Nothing will be more proper and exclusive. No hesitation,get links replica clothing or even the links oflinks of london sale London inspired band, pendant, armlet, earring, or bell. Here is a collection of crowds and be focus of others. Its matchless & exacting purpose, its classic and superb theme, and its matchless arrange in silver diligence seal all creature beings, especially girls. Choose some that can be tattered everyday as family the best expression of dear and emotion. How many time have we current you with newest London grandeur authentic silver
links of london friendship braceletjewels sets. Every model is well-crafted in front of relations offset and shrunk back at the adore for beauty is a type of all creature beings, especially girls. Every example is well-crafted in 100% actual Silver.
Post new comment