/// <summary> /// Creates a new instance of <see cref="CacheItem"/> object. /// </summary> /// <param name="key">The key of the cache item.</param> /// <param name="value">The value of the cache item.</param> /// <param name="expirationInMinutes">The expiration in minutes of the cache item.</param> public CacheItem(string key, object value, int expirationInMinutes) { if (expirationInMinutes < 0) { throw new ArgumentOutOfRangeException("Expiration in minutes must not be negative values."); } Key = key; Value = value; CreatedDate = DateTime.UtcNow; Expiration = CreatedDate.AddMinutes(expirationInMinutes); }
/// <summary> /// Updates the value of the current item based from the source item. /// </summary> /// <param name="item">The source item.</param> /// <param name="throwException">Throws an exception if the operation has failed to update an item.</param> internal void UpdateFrom(CacheItem <T> item, bool throwException = true) { if (!IsExpired() && throwException) { throw new InvalidOperationException($"Cannot update the item that is not yet expired."); } Value = item.Value; CreatedDate = item.CreatedDate; Expiration = CreatedDate.AddMinutes(item.CacheItemExpiration ?? Constant.DefaultCacheItemExpirationInMinutes); }
/// <summary> /// Updates the value of the current item based from the source item. /// </summary> /// <param name="item">The source item.</param> internal void UpdateFrom(CacheItem <T> item) { if (!IsExpired()) { throw new InvalidOperationException($"Cannot update the item that is not yet expired."); } else { Value = item.Value; CreatedDate = DateTime.UtcNow; Expiration = CreatedDate.AddMinutes(Constant.DefaultCacheItemExpirationInMinutes); } }
/// <summary> /// Creates a new instance of <see cref="CacheItem"/> object. /// </summary> /// <param name="key">The key of the cache item.</param> /// <param name="value">The value of the cache item.</param> /// <param name="cacheItemExpiration">The expiration in minutes of the cache item.</param> public CacheItem(string key, T value, int cacheItemExpiration = Constant.DefaultCacheItemExpirationInMinutes) { if (cacheItemExpiration < 0) { throw new ArgumentOutOfRangeException("Expiration in minutes must not be negative values."); } Key = key; Value = value; CacheItemExpiration = cacheItemExpiration; CreatedDate = DateTime.UtcNow; Expiration = CreatedDate.AddMinutes(cacheItemExpiration); }
public void SetOffset(int minutes) { CreatedDate = CreatedDate?.AddMinutes(minutes); }