Umbraco ContentService GetValue issue. How to refresh the content cache?
Hi Guys,
So I have a problem setting a field. I think its something to do with refreshing the content cache which I am not sure what needs doing.
Any advise would be happily accepted.
So here is my code.....
Basically what I am doing is in the ContentService_Published event I am checking a boolean field to see if it's set or not. If its false I want to set this as true.
The display field is set Ok and when I check in the back office I can see that it is now set to true. The problem is that when I then re-run the code and check the contentService for the display field it is always still set to false like something hasn't updated correctly or more likely something hasn't been refreshed as it should have.
To add it does seem to be some sort of cache or it seems more like the DB updates are scheduled. I have tested the code again after 5 or 10 mins and the field had been updated correctly. I am guessing that all db updates are run every few minutes. Is there any way to push these through instantly?
if(!string.IsNullOrEmpty(location))
{
Udi udi;
if (Udi.TryParse(location, out udi))
{
var contentService = ApplicationContext.Current.Services.ContentService;
var subLocation = contentService.GetById(((Umbraco.Core.GuidUdi)udi).Guid);
if(subLocation != null)
{
int isDisplayed = subLocation.GetValue("display") != null ? (int)subLocation.GetValue("display") : 0;
if (isDisplayed == 0)
{
subLocation.SetValue("display", true);
contentService.SaveAndPublishWithStatus(subLocation);
}
}
}
}
Refreshing the cache can be an expensive operation so if I were you I'd validate that the cache isn't actually updated (hence, it's not a caching problem).
If you would like the force the cache to refresh you could try
var contentTypeCacheRefresher = new ContentTypeCacheRefresher();
contentTypeCacheRefresher.Refresh(1234); //Id of node to refresh
When looking at the location object in the backend content editor the deleted flag has been set ok.
But if I then run the following code to check if the display field has been updated in the DB it is still set to false.
var contentService = ApplicationContext.Current.Services.ContentService;
var subLocation = contentService.GetById(((Umbraco.Core.GuidUdi)udi).Guid);
int isDisplayed = subLocation.GetValue("display") != null ? (int)subLocation.GetValue("display") : 0;
If I then wait for a few minutes and then check the contentService again The field Is then updated as expected. Its almost like the DB updates are added to a queue and don't get fire straight away.
The field eventually updates but it takes a little time.
If I run the SetValue and then save an publish. The value gets saved but then when I re-run the code code immediately after then the initial GetValue check still isn't pulling through the previously set value.
If I then wait for a couple of minutes then rerun the code again the GetValue now has the correct value that was previously set.
Like the SetValue and the GetValue are looking to two different places for the data and there is a moment in time following the SetValue that they are not in sync.
We are seeing the same thing that David is reporting (we're using Umbraco v.7.11) and are not able to immediately retrieve the latest changes for content and instead must wait for a few minutes before the API calls would return the up-to-date snapshot.
Some suggestions would be greatly appreciated.
Thanks
I think I found the answer. It looks like we need to use the GetPublishedVersion() method in place of the GetById() method. Why I'm not sure but presumably it's cache related as the OP suggested.
Umbraco ContentService GetValue issue. How to refresh the content cache?
Hi Guys,
So I have a problem setting a field. I think its something to do with refreshing the content cache which I am not sure what needs doing.
Any advise would be happily accepted.
So here is my code.....
Basically what I am doing is in the
ContentService_Published
event I am checking a boolean field to see if it's set or not. If its false I want to set this as true.The display field is set Ok and when I check in the back office I can see that it is now set to true. The problem is that when I then re-run the code and check the contentService for the display field it is always still set to false like something hasn't updated correctly or more likely something hasn't been refreshed as it should have.
To add it does seem to be some sort of cache or it seems more like the DB updates are scheduled. I have tested the code again after 5 or 10 mins and the field had been updated correctly. I am guessing that all db updates are run every few minutes. Is there any way to push these through instantly?
Thanks in advanced.
Kind Regards
David
Hi David,
Refreshing the cache can be an expensive operation so if I were you I'd validate that the cache isn't actually updated (hence, it's not a caching problem).
If you would like the force the cache to refresh you could try
Marco
Hi Marco,
I am not sure that's the issue. I tested running that code and my delete flag is still set to false for a number of minutes.
So I run the code....
When looking at the location object in the backend content editor the deleted flag has been set ok.
But if I then run the following code to check if the display field has been updated in the DB it is still set to false.
If I then wait for a few minutes and then check the contentService again The field Is then updated as expected. Its almost like the DB updates are added to a queue and don't get fire straight away.
Any ideas?
Kind Regards
David
Hi David,
Could you please tell me what version of Umbraco you are running? Perhaps I can reproduce the steps you take to see if (and where) it goes wrong
Marco
Umbraco version 7.10.4 assembly: 1.0.6684.15725
The field eventually updates but it takes a little time.
If I run the SetValue and then save an publish. The value gets saved but then when I re-run the code code immediately after then the initial GetValue check still isn't pulling through the previously set value.
If I then wait for a couple of minutes then rerun the code again the GetValue now has the correct value that was previously set.
Like the SetValue and the GetValue are looking to two different places for the data and there is a moment in time following the SetValue that they are not in sync.
Kind Regards
David
We are seeing the same thing that David is reporting (we're using Umbraco v.7.11) and are not able to immediately retrieve the latest changes for content and instead must wait for a few minutes before the API calls would return the up-to-date snapshot.
Some suggestions would be greatly appreciated. Thanks
Hi guys,
I'll join the list of users that are seeing this too. Do we have a solution other than forcing the cache to refresh?
Thanks.
I think I found the answer. It looks like we need to use the GetPublishedVersion() method in place of the GetById() method. Why I'm not sure but presumably it's cache related as the OP suggested.
is working on a reply...