mardi 1 septembre 2020

What is correct to store UpdatedAt or ExpireAt timestamps to limit updates by timeout

I need to implement function to store some value with limit on updates once per week. I'm implemented in following way:

class Example
{
    //Stored in db
    public int _value;

    //Stored in db
    public DateTime _updatedAt;

    //Stored in db
    public DateTime _canUpdateAfter;

    //Constant in code
    public TimeSpan _updateTimeout = TimeSpan.FromMinutes(1);
        
    public void StoreValue1(int value)
    {
        if (DateTime.Now - _updatedAt < _updateTimeout)
        {
            return;
        }

        _value = value;
        _updatedAt = DateTime.Now;
    }

    public void StoreValue2(int value)
    {
        if (_canUpdateAfter > DateTime.Now)
        {
            return;
        }

        _value = value;
        _canUpdateAfter = DateTime.Now + _updateTimeout;
    }

}

I have two ways of implementing it:

  1. Store updated time in db and calculate if timeout is passed in .net code.
  2. Store value when timeout expire in db and compare it with current in .net code.

Which to use and why?

Aucun commentaire:

Enregistrer un commentaire