On #2148255: [meta] Make better D8 api.d.o landing page, linked to high-level overview topics, and put it in Core api.php files, we made a patch that included a stub Topic page for api.drupal.org (i.e., a @defgroup) titled:

Cache API

This can be found in file core/modules/system/core.api.php where it says

@defgroup cache

The documentation to go on this page needs to be written. The idea is:

a) Write a few paragraphs about the topic.

b) Link to more detailed documentation on
https://drupal.org/developing/api/8

c) If the more detailed documentation does not yet exist, create stub page(s), link to the stub pages, and add a note to this issue stating that the stub pages need to be filled out.

d) If the topic has related classes, interfaces, and functions -- appropriate for an overview -- add

@ingroup cache

to their documentation headers. That will make these classes etc. show up on the Topic page on api.drupal.org. Only include classes/functions that are appropriate for an overview page please!

For more info -- documentation standards for @defgroup/@ingroup:
https://drupal.org/coding-standards/docs#defgroup

Comments

berdir’s picture

Status: Active » Needs review
StatusFileSize
new10.69 KB
new230.66 KB

Starting this..

There is some documentation on CacheBackendInterface right now, but this is a better place for it. Added some @section's, otherwise it's 1:1 copy. And while there's extensive documentation on how to configure a custom cache backend and deletion, there's nothing about the "normal usage".

Someone else is probably more qualified to start writing something there, so posting this as an intermediate step...

jhodgdon’s picture

Status: Needs review » Needs work

Wow! Thanks! Great start!

A few things to fix:

a)

+++ b/core/modules/system/core.api.php
@@ -171,12 +171,64 @@
 /**
  * @defgroup cache Cache API
  * @{
- * Information about the Drupal Cache API

We don't actually want to lose that line. The first line after the { is for the one-line description of the defgroup (appears on Topics lists on api.d.o). So we should have something there.

b) Let's put @todo and a little summary of what should go into them, for the sections that still need to be written, like

+ * @section basics Basics
  *
(nothing there)

c) I also think maybe we should change the order here, but maybe if the Basics section was filled in I wouldn't think so? :)

d) Since this is copy/pasted from an interface header, there are all kinds of references to class methods like:

+ * - Deletion (using delete(), deleteMultiple(), deleteTags() or deleteAll()):
+ *   Permanently removes the item from the cache.

These do not really make sense out of context -- probably need an interface or class name with them?

berdir’s picture

c) I would assume that basics should come first, tags probably deserve their own section too.

d) True. But expanding that to Drupal\Core\Cache\CacheBackendInterface::delete() and so on would also be far less readable.

jhodgdon’s picture

c) I was just a bit surprised to get to "Deletion" before I found out how to add stuff to the cache. But I think that would be covered in Basics hopefully/maybe, hence my "if Basics was filled in I wouldn't think [the order was wrong]". :)

d) The docs could say something like "The following methods are all on ...." and then just use the method names? Something like that would be fine. But having just methods there without any context, in a @defgroup doc, doesn't seem good. Keep in mind some people will not be reading the whole doc top to bottom either -- they will skim headers to find their information.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.42 KB
new5.84 KB
new408.21 KB

Ok, spent some more time on writing additional documentation and re-structuring a bit :)

Tried to address the feedback as much as possible, for d), I added a note about the location of the methods at the top, I don't think we should repeat that for every section?

Added a @todo's with issues that I know about that will affect ths.

jhodgdon’s picture

StatusFileSize
new13.65 KB
new9.05 KB

I think the solution for (d) is fine, and this is looking pretty good -- thanks for all the writing!

I had a bunch of suggestions for how to make this documentation clearer (and there were some proofreeding changes that were also needed)... I decided the most efficient way to present them was by making a new patch. Hope that is OK. Please review carefully!

Note: I'm not sure an interdiff is all that useful... most of the core.api.php lines changed and nothing else changed. But I added one anyway.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, changes look good.

berdir’s picture

Assigned: Unassigned » webchick

Assigning to @webchick per @jhodgdon's suggestion, as she worked on it a bit too.

wim leers’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/system/core.api.php
@@ -173,10 +173,142 @@
+ * \Drupal\Core\Cache\CacheBackendInterface.
...
+ * - Request a cache object through \Drupal::cache() or by injecting a cache
...
+ * - Define a Cache ID (cid) value for your data. A cid is a string, which must
...
+ * - Call the get() method to attempt a cache read, to see if the cache already
...
+ *   cache using the set() method. The third argument of set() can be used to

Will all of the code terms (function names and "cid") be wrapped in code tags?

I think that's essential for legibility of docs.

jhodgdon’s picture

Status: Needs work » Reviewed & tested by the community

No, we do not do that in documentation blocks normally.

wim leers’s picture

Are there then at least plans to do so? It seems that since we have https://api.drupal.org/api/drupal/8, https://drupal.org/documentation will become less important, because most docs will live at https://api.drupal.org/api/drupal/8 instead. If none of that has the proper formatting, then that's a huge regression.

jhodgdon’s picture

On the contrary, the plan for the docs on api.drupal.org (the ones going into @defgroups) is that they are minimal summaries, and should link to sections of https://drupal.org/developing/api for more in-depth information.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

eojthebrave’s picture

Status: Fixed » Needs work

Shoot. I was just about to add some comments to this one. Here they are:

+ * Example:
+ * @code
+ * $cache = \Drupal::cache();
+ * $cid = 'mymodule_example:' . \Drupal::languageManager()->getCurrentLanguage()->id();
+ *
+ * $data = NULL;
+ * if ($cache = \Drupal::cache()->get($cid)) {
+ *   $data = $cache->data;
+ * }
+ * else {
+ *   $data = my_module_complicated_calculation();
+ *   \Drupal::cache()->set($cid, $data);
+ * }

This code seems wrong. We create the $cache object in the first line, but never use it. Then recreate it in the if statement. Seems like maybe the if statement should use $cache->get() instead? Lets make sure this is right and reflects best practices because people are going to copy/paste this example like crazy if it's in the docs.

+ * @section bins Cache bins
+ *
+ * Cache storage is separated into "bins", each containing various cache items.
+ * Each bin can be configured separately; see @ref configuration.

It would be nice to have a better explination of why there are different cache bins and how I should decide when to use the default vs. another existing option vs. creating my own. Though perhaps this is something better for a handbook page? In which case we should create the stub of that page and link to it from here.

+ * Each cache bin can be configured separately; for instance, each bin can use a
+ * different cache backend, such as APC or Memcache. The default backend stores
+ * the cached data in the Drupal database.

I find this a little confusing and think it would benefit from changing the order of statements. I think changing the backend of the entire system is likely more common than changing and individual bin. What about something like:

"By default cached data is stored in the database. This can be configured though so that all cached data, or that of an individual cache bin, uses a different cache backend, such as APC or Memcache, for storage."

I'm going to re-open this because at a minimum we should confirm that that code sample is correct. If it is and I'm just missing something feel free to tell me to stop being silly. :)

jhodgdon’s picture

Yes, please fix the code, that would be great! And if you can make a patch with better wording, that would also be great.

berdir’s picture

the only thing that's wrong about the code is the initial $cache definition, I forgot to remove that.

It would be nice to have a better explination of why there are different cache bins and how I should decide when to use the default vs. another existing option vs. creating my own. Though perhaps this is something better for a handbook page? In which case we should create the stub of that page and link to it from here.

That is what I added the @todo to the re-organize cache bins issue (#1194136: Re-organise core cache bins). Documenting it right now not make sense as that issue will change a lot there.

jhodgdon’s picture

When we fix this up a bit, could we also stress that doing $cache->get() doesn't give you your original data? Instead you have to do:
$cache = (get the Cache object)->get(...);
if ($cache) {
return $cache->data;
}

This code is correct in the example in the doc block, but this point is not stressed at all. Very confusing! (See #2225087: CacheBackendInterface->get() docs missing crucial information for an example of someone (me!) getting confused.)

eojthebrave’s picture

StatusFileSize
new1.49 KB

Here's a patch that:

1. Removes the unnecessary $cache = line of code from the example.

2. Adds some clarification about the use of $cache->data & $data variables.

3. Rewords the paragraph in the configuration section that was a little bit confusing.

Thanks for taking care of that other @todo in #1194136: Re-organise core cache bins. I like that part now. :)

eojthebrave’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, looks good. We have an issue to make the returned object a typed object with an interface/methods, but that got sidetracked on different things...

jhodgdon’s picture

Thanks! Agreed, it's good. Committed to 8.x.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

  • Commit 5b32c7b on 8.x by jhodgdon:
    Issue #2216543 by eojthebrave: Fix code example and some text in Config...
jhodgdon’s picture

Assigned: webchick » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.