Hi,
I have users' profiles created with CCK and I added a computed field which count the number of post created by this user.
The problem I faced is that data seems to be cached. If a user create his profile, it displays correctly the number of post he wrote. But then, if the user wrote other posts, this computed field is not updated. I think it's caught by Drupal cache mechanism, but I don't know if it's possible to avoid this behaviour.
To get the field updated I have 2 options: edit the user profile or clearing "cached data" in the performance admin page. But clearly this won't work on a normal site.

If I set up the same query in a block, data is refreshed correctly as blocks aren't cached the same way pages are.

Any idea on how to avoid computed fields data being cached ?

Thanks a lot.
Regards

Comments

subscribing

subscribing

subscribing

There is such setting for computed field - to save the value of the field in database or not, so if is choosen not to save the value of the field will be computed everytime and not cached. The only problem with such way is that you can't display it in views it seems to me. May be I'm wrong - need checking.

I am not saving it in the database but still it is caching. But when I clear cache it works.

Well for now, I found a way. This works for me. But not sure, is it a good way!

cache_clear_all('*', 'cache_content', TRUE);

Also, this may help us.

http://drupal.org/node/17620

I have tried the suggested solution and it does not seem to work for me. I have tried a bunch of variations:

<?php
$revid
= $node->vid;
$nodeid = $node->nid;
$cacherow = 'content:'.$nodeid.':'.$revid;
cache_clear_all($nodeid, 'cache_content', TRUE);
cache_clear_all($cacherow, 'cache_content');
db_query("DELETE FROM {'cache_content'} WHERE cid = '%s'", $cacherow);
db_query("DELETE FROM 'cache_content' WHERE cid = $cacherow);
db_query("
DELETE FROM cache_content WHERE cid = $cacherow);
cache_clear_all('*', 'cache_content', TRUE);
?>

Nothing seems to clear the needed info on viewing the post. If I go to the table and delete that one row it works as expected.

StatusFileSize
new1.13 KB

http://drupal.org/node/134661#comment-1007219

use the display code box to store any code that won't be cached, unless page caching is enabled, and you're anonymous. i think a nice little patch explaining this on the display code box is in order....ahh, look what i've found.

Status:Active» Needs review

I ended up loading and saving my users' profiles (using hook_nodeapi) each time the wrote a piece of content and this way the computed field get recalculated.
I'm not thrilled about it but it works fine.

Thanks George2, this fixed the issue for me. I have not tested the patch yet.

@George2
And does my CF is still stored with accurate values in the DB ?

#8 - That's good to know. You can also use another node fields inside your code, but $node class is inside $element variable. "myfield" field value should be available this way:

<?php
$element
['#node']->field_myfield[0]['value'];
?>

The solution that worked for me:

I have a content type called 'books' with a computed field in it. I have inserted the following code into node-books.tpl.php, at the beginning of the file:

<?php
  $query
= sprintf("DELETE FROM cache_content WHERE cid LIKE '%%%d%%'", $node->nid);
 
db_query($query);
?>

It deletes the row that has the nid of the current node from cache_content.

I am not the number 1 expert on Drupal, so I am adding the following questions:

1. cid in cache_content table looks like: "content:6071:6073", where 6071 is the actual nid. What is 6073 then?
2. I am sure that this query can be written in one row. What is this row? Is it:

  $query = db_query("DELETE FROM cache_content WHERE cid LIKE '%%%d%%'", $node->nid);

3. What are the side effects, except the cache being deleted on node load?

Isn't this what the option Store using the database settings below (required for Views use)" does?
Try unchecking it.

I tried a million ways to avoid caching in D7. Does not seem to work. Isn't there a solution somehow ? :s

Version:6.x-1.0-beta2» 7.x-1.0-beta1

In D7 I added a rule with a PHP action;

cache_clear_all('field:node:[node-registration:nid]', 'cache_field');

What about reducing the value of "page_cache_maximum_age" variable (e.g. "Expiration of cached pages")?

edit: cached field value is served even to authenticated user, well after globally configured expiration date has passed. in fact, the entry in cache_field has expire = 0, which means it's cached indefinitely, which is definitely not what i want for a non-database-storage computed field.

Title:How to avoid data being cached ?Computed field value is not re-computed on node load
Version:7.x-1.0-beta1» 7.x-1.x-dev
Category:support» bug
Status:Needs review» Active

Issue summary:View changes
Status:Active» Needs review
StatusFileSize
new931 bytes

Store value in the database -setting is applied to hook_field_load() only (where field values get cached by the field cache). In order to overcome cache hook_field_prepare_view() should be used instead (hook is invoked before the field values are handed to formatters for display).

Here is the patch to computed_field_field_prepare_view() function to allow re-computation of field values on node load (if Store value in the database -setting is unset)

#21 is a great solution, thank you mitrpaka.

I'm attaching an updated version that adds an explicit field setting called "re-calculate this field on entity load".
This approach does not change existing behavior after patching.
Admins have to explicitly update field settings to make this happen.

Additionally, some folks might want a non-db stored field that does get cached.

Can we get a maintainer to weigh in here?
This issue has been open for OVER 5 YEARS.

#22, thanks this is great.

I would vote for getting this in dev and in the next release.

Thanks.

#22, thanks this is great for me, too

I give my vote for including it in the next release

Status:Needs review» Needs work

+++ computed_field.module (working copy)
@@ -222,7 +228,8 @@
+dpm(__LINE__);

Looks good; I'd be willing to commit it, but this line will definitely have to come out. ;)

Status:Needs work» Needs review
StatusFileSize
new2.13 KB

Updated patch attached.