Exemple #1
0
        /// <summary>
        /// Reads the data from given path and deserializes it taking given expirationTime in consideration.
        /// </summary>
        /// <typeparam name="T">Data Type to retrieve.</typeparam>
        /// <param name="path">Path to the file. Can be just filename.</param>
        /// <param name="expiration">Specifies how much time could have passed since last write.</param>
        /// <returns>Deserialized data or default if file does not exist or is malformed.</returns>
        /// <exception cref="DataExpiredException"> Thrown when data is expired.</exception>
        public async Task <T> RetrieveData <T>(string path, TimeSpan?expiration = null)
        {
            try
            {
                var json = await _fileStorageProvider.ReadTextAsync(path);

                var holder = JsonConvert.DeserializeObject <TimedHolder <T> >(json);

                if (expiration != null && DateTime.UtcNow - holder.CreatedAt > expiration)
                {
                    throw new DataExpiredException($"Data stored in {path} is expired as per provided expiration time {expiration}");
                }

                return(holder.Value);
            } // file not exists or malformed
            catch (Exception e) when(!(e is DataExpiredException))
            {
                return(default);