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
Comment | File | Size | Author |
---|---|---|---|
#26 | computed_field-recalculate-332200-26.patch | 2.13 KB | mitrpaka |
#22 | computed_field-recalculate_option-332200.patch | 1.78 KB | AaronBauman |
#21 | computed_field-not_re-computed_on_node_load-332200-21.patch | 931 bytes | mitrpaka |
#8 | explain.patch | 1.13 KB | George2 |
Comments
Comment #1
kenorb CreditAttribution: kenorb commentedsubscribing
Comment #2
christian.bender@fuenfzehndreissig.com CreditAttribution: christian.bender@fuenfzehndreissig.com commentedsubscribing
Comment #3
vikramy CreditAttribution: vikramy commentedsubscribing
Comment #4
netbear CreditAttribution: netbear commentedThere 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.
Comment #5
vikramy CreditAttribution: vikramy commentedI am not saving it in the database but still it is caching. But when I clear cache it works.
Comment #6
vikramy CreditAttribution: vikramy commentedWell 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
Comment #7
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedI have tried the suggested solution and it does not seem to work for me. I have tried a bunch of variations:
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.
Comment #8
George2 CreditAttribution: George2 commentedhttp://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.
Comment #9
George2 CreditAttribution: George2 commentedComment #10
tostinni CreditAttribution: tostinni commentedI 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.
Comment #11
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedThanks George2, this fixed the issue for me. I have not tested the patch yet.
Comment #12
tostinni CreditAttribution: tostinni commented@George2
And does my CF is still stored with accurate values in the DB ?
Comment #13
kenorb CreditAttribution: kenorb commented#195013: Recompute all instances of a computed field upon field change
Comment #14
Floop CreditAttribution: Floop commented#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:
Comment #15
Eli Baskin CreditAttribution: Eli Baskin commentedThe 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:
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:
3. What are the side effects, except the cache being deleted on node load?
Comment #16
Gabriel R. CreditAttribution: Gabriel R. commentedIsn't this what the option Store using the database settings below (required for Views use)" does?
Try unchecking it.
Comment #17
JGO CreditAttribution: JGO commentedI tried a million ways to avoid caching in D7. Does not seem to work. Isn't there a solution somehow ? :s
Comment #18
Bartezz CreditAttribution: Bartezz commentedIn D7 I added a rule with a PHP action;
Comment #19
AaronBaumanWhat 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.
Comment #20
AaronBaumanComment #21
mitrpaka CreditAttribution: mitrpaka commentedStore 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)
Comment #22
AaronBauman#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.
Comment #23
grn CreditAttribution: grn commented#22, thanks this is great.
I would vote for getting this in dev and in the next release.
Thanks.
Comment #24
rollingnet CreditAttribution: rollingnet commented#22, thanks this is great for me, too
I give my vote for including it in the next release
Comment #25
colanLooks good; I'd be willing to commit it, but this line will definitely have to come out. ;)
Comment #26
mitrpaka CreditAttribution: mitrpaka commentedUpdated patch attached.
Comment #27
mariacha1 CreditAttribution: mariacha1 commentedThis 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.
Comment #28
MLZRII can confirm the patch (I used # 26) works!
Thanks.
Comment #30
colanThanks everyone!