Search code examples
c#utc

C# DateTime.UtcNow value always updated


I have a class that should track the last time it was accessed and I have a public property that exposes that last access time value which is set privately.

My class is defined as follows:

internal class DeviceModelCacheItem
{
    private int _cacheId;
    private List<DeviceModel> _deviceModels;
    private DateTime _lastAccess;

    public DeviceModelCacheItem(int cacheId, List<DeviceModel> deviceModels)
    {
        _cacheId = cacheId;
        _deviceModels = deviceModels;
        _lastAccess = DateTime.UtcNow;
    }

    ...

    public List<DeviceModel> DeviceModels
    {
        get
        {
            _lastAccess = DateTime.UtcNow;
            return _deviceModels;
        }
        set
        {
            _lastAccess = DateTime.UtcNow;
            _deviceModels = value;
        }
    }

    public DateTime LastAccess
    {
        get
        {
            return _lastAccess;
        }
    }
}

I am accessing this value in a seperate class method as follows:

var cacheItem = _deviceModelCache.SingleOrDefault(x => x.CacheId == deviceTypeId);

if(cacheItem != null && DateTime.UtcNow.Subtract(cacheItem.LastAccess).TotalSeconds > 120)
{
    ...
}

Where _deviceModelCache is a collection of DeviceModelCacheItem instances.

I'm finding that whenever I try and access this value, the private value will always be updated making it impossible to use it as a tracking value. Why is this being updated?

EDIT

I've included more code showing how I'm access the DateTime value. The collection of DeviceModelCacheItem instances are held in a singleton as follows:

EDIT 2

I've as also added the db access code and also included where I'm updating the _lastAccess property. I figure I may have a deferred execution problem.

public class DeviceModelRepository
{
    private List<DeviceModelCacheItem> _deviceModelCache;        

    private static readonly Lazy<DeviceModelRepository> instance = new Lazy<DeviceModelRepository>(() => new DeviceModelRepository());

    public static DeviceModelRepository Instance
    {
        get
        {
            return instance.Value;
        }
    }

    private DeviceModelRepository()
    {
        _deviceModelCache = new List<DeviceModelCacheItem>();
    }

    public IEnumerable<DeviceModel> GetAllDeviceModelsByDeviceTypeId(int deviceTypeId)
    {
        return GetAllDeviceModelsByDeviceTypeId(GlobalConfiguration.APP_CACHE_ENABLED, deviceTypeId);
    }

    private IEnumerable<DeviceModel> GetAllDeviceModelsByDeviceTypeId(bool enableCache, int deviceTypeId)
    {
        var cacheItem = _deviceModelCache.SingleOrDefault(x => x.CacheId == deviceTypeId);

        //Debugger attached here
        if(cacheItem != null && DateTime.UtcNow.Subtract(cacheItem.LastAccess).TotalSeconds > 120)
        {
            ... 
            using (GadgetContext db = new GadgetContext())
            {
                var deviceModels = db.DeviceModels.Where(x => x.DeviceTypeId == deviceTypeId).ToList();

                if (enableCache && deviceModels != null && deviceModels.Count > 0)
                {
                    try
                    {
                        if(cacheItem == null)
                        {
                            _deviceModelCache.Add(new DeviceModelCacheItem(deviceTypeId, deviceModels));
                        }
                        else
                        {
                            cacheItem.DeviceModels = deviceModels;
                        }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex.ToString());
                    }
               }
            }
            ...
        }
        ...
    }
}

Solution

  • As pointed out by @JamesLucas the issue of the DateTime.UtcNow value always being updated was down to a deferred execution error in my code. My date time value was being updated every time entity framework deferred execution to grab items from the DB.