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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kenorb’s picture

subscribing

christian.bender@fuenfzehndreissig.com’s picture

subscribing

vikramy’s picture

subscribing

netbear’s picture

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.

vikramy’s picture

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

vikramy’s picture

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

nicholas.alipaz’s picture

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

$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.

George2’s picture

FileSize
1.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.

George2’s picture

Status: Active » Needs review
tostinni’s picture

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.

nicholas.alipaz’s picture

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

tostinni’s picture

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

kenorb’s picture

Floop’s picture

#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:

$element['#node']->field_myfield[0]['value'];
Eli Baskin’s picture

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?

Gabriel R.’s picture

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

JGO’s picture

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

Bartezz’s picture

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');
AaronBauman’s picture

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.

AaronBauman’s picture

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
mitrpaka’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
931 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)

AaronBauman’s picture

#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.

grn’s picture

#22, thanks this is great.

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

Thanks.

rollingnet’s picture

#22, thanks this is great for me, too

I give my vote for including it in the next release

colan’s picture

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. ;)

mitrpaka’s picture

Status: Needs work » Needs review
FileSize
2.13 KB

Updated patch attached.

mariacha1’s picture

Status: Needs review » Reviewed & tested by the community

This applies cleanly and works correctly for me. Items with the checkbox for "Recalculate" checked DID refresh on each page load, while items with the checkbox for "Recalculate" unchecked only refreshed with cache refresh. Code looks good as well.

MLZR’s picture

II can confirm the patch (I used # 26) works!
Thanks.

  • colan committed c93e251 on 7.x-1.x authored by mitrpaka
    Issue #332200 by mitrpaka, aaronbauman, George2 | tostinni: Re-compute...
colan’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Status: Fixed » Closed (fixed)

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