Copied to clipboard

Flag this post as spam?

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


  • Philip Hayton 58 posts 292 karma points
    1 week ago
    Philip Hayton
    0

    Strange caching behaviour for custom property editor

    Hi guys,

    I have a custom property editor that is being used to specify the days and times certain activities take place on.

    When I iterate over the schedules for the first time they have the correct values, but on subsequent requests every element has the same value - the last item according to 'Sort Order' in Umbraco.

    It's as if the values are being cached somewhere / somehow using the same key, but I cannot for the life of me figure out what is going on.

    I have tried the following to no avail:

    • Set different PropertyCacheLevel on the ValueConverter, tried None, Element, Elements, Unknown, and Snapshot
    • Reload / Collect all of the caches from Settings > Published Status
    • Complete system reboot

    I feel like I am missing something fundamental and probably quite obvious... can anyone shed any light?

    For info here is the value converter and some images of how it looks in Umbraco:

    public class WeeklyScheduleValueConverter : IPropertyValueConverter
    {
        public bool IsConverter(IPublishedPropertyType propertyType)
            => Constants.WeeklySchedule.Alias == propertyType.EditorAlias;
    
        public bool? IsValue(object value, PropertyValueLevel level)
        {
            return level switch
            {
                PropertyValueLevel.Source => value != null &&
                                             (value is not string s || string.IsNullOrWhiteSpace(s) == false),
                _ => throw new NotSupportedException($"Invalid level: {level}.")
            };
        }
    
        public Type GetPropertyValueType(IPublishedPropertyType propertyType)
            => typeof(Models.WeeklySchedule);
    
        public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
            => PropertyCacheLevel.Elements;
    
        public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
        {
            var sourceString = source?.ToString();
    
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return Models.WeeklySchedule.Empty;
            }
    
            return (JsonConvert.DeserializeObject<IEnumerable<WeeklyScheduleDayDto>>(sourceString) ??
                    Array.Empty<WeeklyScheduleDayDto>());
        }
    
        public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType,
            PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            if (inter is not IEnumerable<WeeklyScheduleDayDto> schedule)
                return null;
    
            var enumeratedSchedule = schedule.ToList();
            var weeklySchedule = Models.WeeklySchedule.Empty;
    
            for (var i = 0; i < 7; i++)
            {
                var dayOfWeek = (DayOfWeek)i;
                var dailySchedule = enumeratedSchedule.ElementAt(i).Value;
    
                weeklySchedule[dayOfWeek].Enabled = dailySchedule.Enabled;
                var parsedEntries = dailySchedule.Items.Select(item => new WeeklyScheduleDayItem { Start = item.Value?.Start, End = item.Value?.End }).ToList();
    
                weeklySchedule[dayOfWeek].Items = parsedEntries;
            }
    
            return weeklySchedule;
        }
    
        public object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType,
            PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            return inter?.ToString();
        }
    }
    

    I would expect the value for the 2 hour ticket to have two elements in for Sunday, like this: enter image description here

    But it's actually returning the results of the 3 hour ticket, like this: enter image description here

    As always guys any help is creatlly appreaciated, I think I'm going a little bit crazy.

    TIA

    Phil

  • Philip Hayton 58 posts 292 karma points
    1 week ago
    Philip Hayton
    100

    Ok so it turns out I was being a sausage, as usual...

    I was passing a static value instead of creating a new value in my model...

        public static WeeklySchedule Empty = new()
        {
            { DayOfWeek.Sunday, new WeeklyScheduleDay() },
            { DayOfWeek.Monday, new WeeklyScheduleDay() },
            { DayOfWeek.Tuesday, new WeeklyScheduleDay() },
            { DayOfWeek.Wednesday, new WeeklyScheduleDay() },
            { DayOfWeek.Thursday, new WeeklyScheduleDay() },
            { DayOfWeek.Friday, new WeeklyScheduleDay() },
            { DayOfWeek.Saturday, new WeeklyScheduleDay() }
        };
    

    Should have been...

        public static WeeklySchedule Empty => new()
        {
            { DayOfWeek.Sunday, new WeeklyScheduleDay() },
            { DayOfWeek.Monday, new WeeklyScheduleDay() },
            { DayOfWeek.Tuesday, new WeeklyScheduleDay() },
            { DayOfWeek.Wednesday, new WeeklyScheduleDay() },
            { DayOfWeek.Thursday, new WeeklyScheduleDay() },
            { DayOfWeek.Friday, new WeeklyScheduleDay() },
            { DayOfWeek.Saturday, new WeeklyScheduleDay() }
        };
    

    Is it the weekend yet?

Please Sign in or register to post replies

Write your reply to:

Draft