private bool TooOld(CacheEnvelope cacheEnvelope)
 {
     if (Options.AbsoluteExpirationRelativeToNow == null)
     {
         return(false);
     }
     return(cacheEnvelope.UpdatedAt.Add(Options.AbsoluteExpirationRelativeToNow.Value) <= DateTimeOffset.Now);
 }
        private UseCacheStrategyEnum GetCacheItemStrategy(CacheEnvelope cacheEnvelope)
        {
            InternalContract.RequireNotNull(cacheEnvelope, nameof(cacheEnvelope));

            if (cacheEnvelope.CacheIdentity != CacheIdentity)
            {
                return(UseCacheStrategyEnum.Remove);
            }
            return(TooOld(cacheEnvelope) ? UseCacheStrategyEnum.Remove : UseCacheStrategyEnum.Use);
        }
        /// <summary>
        /// Serialize the <paramref name="item"/>, put it into an envelope and serialize the envelope
        /// </summary>
        public byte[] ToSerializedCacheEnvelope <T>(T item)
        {
            var serializedItem = SerializingSupport.Serialize(item);
            var cacheEnvelope  = new CacheEnvelope
            {
                CacheIdentity = CacheIdentity,
                UpdatedAt     = DateTimeOffset.Now,
                Data          = serializedItem
            };
            var serializedCacheEnvelope = SerializingSupport.Serialize(cacheEnvelope);

            return(serializedCacheEnvelope);
        }
        private async Task <UseCacheStrategyEnum> GetCacheItemStrategyAsync(TId id, CacheEnvelope cacheEnvelope, CancellationToken token)
        {
            InternalContract.RequireNotDefaultValue(id, nameof(id));
            InternalContract.RequireNotNull(cacheEnvelope, nameof(cacheEnvelope));

            var cacheItemStrategy = GetCacheItemStrategy(cacheEnvelope);

            if (cacheItemStrategy != UseCacheStrategyEnum.Use)
            {
                return(cacheItemStrategy);
            }
            if (UseCacheStrategyMethodAsync == null)
            {
                return(UseCacheStrategyEnum.Use);
            }
            var cachedItemInformation = new CachedItemInformation <TId>
            {
                Id        = id,
                UpdatedAt = cacheEnvelope.UpdatedAt
            };

            return(await UseCacheStrategyMethodAsync(cachedItemInformation, token));
        }