using System; using System.Globalization; using System.Threading.Tasks; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.ErrorLogging; using StackExchange.Redis; namespace Sony.Filtr.Core.LastModified { public class LastModifiedFactory { private readonly ErrorLoggingManager _errorLoggingManager; private readonly IDatabase _redisDatabase; public LastModifiedFactory(ErrorLoggingManager errorLoggingManager, IDatabase redisDatabase) { _errorLoggingManager = errorLoggingManager; _redisDatabase = redisDatabase; } private const string LastModifiedCacheKeyBase = "Last-Modified:"; private string GetCacheKey(Application application, EntityType entityType) { return LastModifiedCacheKeyBase + (application?.ID.ToString() ?? "") + entityType; } public DateTime GetLastModifyDate(Application application, EntityType entityType) { try { var get = _redisDatabase.StringGet(GetCacheKey(application, entityType)); //var get = PersistentCachingManager.Instance.Connection.Strings.GetString(0, GetCacheKey(application, entityType)); if (get.HasValue) { DateTime date; if (DateTime.TryParse(get, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out date)) return date; } } catch (Exception ex) { _errorLoggingManager.LogError(ex); } return DateTime.MinValue; } public async Task GetLastModifyDateAsync(Application application, EntityType entityType) { try { var get = await _redisDatabase.StringGetAsync(GetCacheKey(application, entityType)); //var get = await PersistentCachingManager.Instance.Connection.Strings.GetString(0, GetCacheKey(application, entityType)); if (get.HasValue) { DateTime date; if (DateTime.TryParse(get, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out date)) return date; } } catch (Exception ex) { _errorLoggingManager.LogError(ex); } return DateTime.MinValue; } public void SetLastModifyDate(Application application, EntityType entityType) { try { _redisDatabase.StringSet(GetCacheKey(application, entityType), DateTime.UtcNow.ToString("o")); //PersistentCachingManager.Instance.Connection.Strings.Set(0, GetCacheKey(application, entityType), lastModifiedDate.ToString()); } catch (Exception ex) { _errorLoggingManager.LogError(ex); } } public async Task SetLastModifyDateAsync(Application application, EntityType entityType) { try { await _redisDatabase.StringSetAsync(GetCacheKey(application, entityType), DateTime.UtcNow.ToString("o")); //await PersistentCachingManager.Instance.Connection.Strings.Set(0, GetCacheKey(application, entityType), lastModifiedDate.ToString()); } catch (Exception ex) { _errorLoggingManager.LogError(ex); } } } }