public MongoDbCache(String cacheCollectionName, MongoDbCachePolicy policy) { this.policy = policy ?? throw new ArgumentNullException(nameof(policy)); if (!string.IsNullOrEmpty(policy.ConnectionName)) { this.connectionString = CacheManager.GetConnectionString(policy.ConnectionName)?.ConnectionString; if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentException( $"{nameof(ICacheConnectionString.ConnectionString)} not found for {nameof(policy.ConnectionName)} {policy.ConnectionName}", $"{nameof(policy)}.{nameof(policy.ConnectionName)}"); } } else if (!string.IsNullOrEmpty(policy.ConnectionString)) { this.connectionString = policy.ConnectionString; } else { throw new ArgumentException( $"{nameof(policy.ConnectionString)} is undefined", $"{nameof(policy)}.{nameof(policy.ConnectionString)}"); } this.cacheDbName = !string.IsNullOrEmpty(policy.DatabaseName) ? policy.DatabaseName : "CacheDb"; this.cacheCollectionName = cacheCollectionName; if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue) { this.expireWithin = policy.SlidingExpiration.Value; this.useSlidingExpiration = true; this.expireAt = null; } else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue) { this.expireWithin = policy.ExpirationFromAdd.Value; this.useSlidingExpiration = false; this.expireAt = null; } else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue) { this.expireWithin = null; this.useSlidingExpiration = false; this.expireAt = policy.AbsoluteExpiration.Value.LocalDateTime; } else { this.expireWithin = null; this.useSlidingExpiration = false; this.expireAt = null; } this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue); this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider); }
public MongoDbCache(String cacheCollectionName, MongoDbCachePolicy policy) { if (policy == null) { throw new ArgumentNullException("policy"); } this.connectionString = policy.ConnectionString; this.cacheDbName = !string.IsNullOrEmpty(policy.DatabaseName) ? policy.DatabaseName : "CacheDb"; this.cacheCollectionName = cacheCollectionName; if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue) { this.expireWithin = policy.SlidingExpiration.Value; this.useSlidingExpiration = true; this.expireAt = null; } else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue) { this.expireWithin = policy.ExpirationFromAdd.Value; this.useSlidingExpiration = false; this.expireAt = null; } else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue) { this.expireWithin = null; this.useSlidingExpiration = false; this.expireAt = policy.AbsoluteExpiration.Value.LocalDateTime; } else { this.expireWithin = null; this.useSlidingExpiration = false; this.expireAt = null; } this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue); this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider); }