Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Armitage 510 posts 2081 karma points
    May 30, 2018 @ 04:31
    David Armitage
    0

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

    Thanks in advanced.

    Kind Regards

    David

  • Marco 12 posts 93 karma points c-trib
    May 30, 2018 @ 06:17
    Marco
    1

    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

    var contentTypeCacheRefresher = new ContentTypeCacheRefresher();
    contentTypeCacheRefresher.Refresh(1234); //Id of node to refresh
    

    Marco

  • David Armitage 510 posts 2081 karma points
    May 30, 2018 @ 07:19
    David Armitage
    0

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

    subLocation.SetValue("display", true);
    contentService.SaveAndPublishWithStatus(subLocation);
    

    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.

    Any ideas?

    Kind Regards

    David

  • Marco 12 posts 93 karma points c-trib
    May 30, 2018 @ 13:25
    Marco
    0

    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

  • David Armitage 510 posts 2081 karma points
    Jun 12, 2018 @ 04:18
    David Armitage
    0

    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

  • Samoura 5 posts 97 karma points
    Nov 07, 2018 @ 21:32
    Samoura
    0

    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

  • Craig 34 posts 270 karma points
    Nov 26, 2018 @ 20:40
    Craig
    0

    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.

  • Craig 34 posts 270 karma points
    Nov 26, 2018 @ 22:27
    Craig
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft