public void ShouldSetExpirationAndRefreshToGivenValues() { CacheBuilder <string> builder = new CacheBuilder <string>(); builder.ExpireAfterWrite(1, TimeUnit.Seconds); Assert.AreEqual(builder.ExpiryAfterWriteNanos, TimeUnitHelper.ToNanos(1, TimeUnit.Seconds)); builder.ExpireAfterAccess(2, TimeUnit.Seconds); Assert.AreEqual(builder.ExpiryAfterAccessNanos, TimeUnitHelper.ToNanos(2, TimeUnit.Seconds)); }
public void ShouldLoadTheValueForMissingKey() { CacheBuilder <string> cache_builder = new CacheBuilder <string>(); LoadingCacheMock <string> cache = new LoadingCacheMock <string>(cache_builder); CacheLoader <string> loader = new StringCacheLoader(); string cached = cache.GetIfPresent("missing-key"); Assert.IsNull(cached); cached = cache.Get("missing-key", loader); Assert.IsNotNull(cached); }
public void ShouldThrowExceptionWhenLoadFail() { CacheBuilder <long> cache_builder = new CacheBuilder <long>(); LoadingCacheMock <long> cache = new LoadingCacheMock <long>(cache_builder); ThrowableLongCacheLoader long_cache_loader = new ThrowableLongCacheLoader(); try { cache.Get("missing-key", long_cache_loader); } catch (ExecutionException exception) { Assert.IsAssignableFrom <ArgumentException>(exception.InnerException); } }
public void ShouldReplaceCachedValueWhenKeyAlreadyExists() { CacheBuilder <string> cache_builder = new CacheBuilder <string>(); LoadingCacheMock <string> ref_cache = new LoadingCacheMock <string>(cache_builder); ref_cache.Put("already-in-cache-key", "original-value"); string cached = ref_cache.GetIfPresent("already-in-cache-key"); Assert.AreEqual("original-value", cached); ref_cache.Put("already-in-cache-key", "new-value"); cached = ref_cache.GetIfPresent("already-in-cache-key"); Assert.AreEqual("new-value", cached); }
public void ShouldReturnsDefaultValueWhenKeyIsNotFound() { CacheBuilder <string> ref_cache_builder = new CacheBuilder <string>(); LoadingCacheMock <string> ref_cache = new LoadingCacheMock <string>(ref_cache_builder); string cached = ref_cache.GetIfPresent("missing-key"); Assert.AreEqual(null, cached); CacheBuilder <long> val_cache_builder = new CacheBuilder <long>(); LoadingCacheMock <long> val_cache = new LoadingCacheMock <long>(val_cache_builder); long val_cached = val_cache.GetIfPresent("missing-key"); Assert.AreEqual(default(long), val_cached); }
public void ShouldThrowExceptionWhenKeyIsNull() { CacheBuilder <string> cache_builder = new CacheBuilder <string>(); LoadingCacheMock <string> cache = new LoadingCacheMock <string>(cache_builder); CacheLoader <string> loader = new StringCacheLoader(); Assert.Throws <ArgumentNullException>( delegate() { cache.Get(null, loader); }); Assert.Throws <ArgumentNullException>( delegate() { cache.GetIfPresent(null); }); Assert.Throws <ArgumentNullException>( delegate() { cache.Put(null, string.Empty); }); Assert.Throws <ArgumentNullException>(delegate() { cache.Remove(null); }); }
public void ShouldStoreValueInCache() { CacheBuilder <string> ref_cache_builder = new CacheBuilder <string>(); LoadingCacheMock <string> ref_cache = new LoadingCacheMock <string>(ref_cache_builder); CacheBuilder <long> val_cache_builder = new CacheBuilder <long>(); LoadingCacheMock <long> val_cache = new LoadingCacheMock <long>(val_cache_builder); ref_cache.Put("ref-cache-key", "ref-cache-value"); string ref_cached = ref_cache.GetIfPresent("ref-cache-key"); Assert.AreEqual(ref_cached, "ref-cache-value"); val_cache.Put("val-cache-key", 50); long val_cached = val_cache.GetIfPresent("val-cache-key"); Assert.AreEqual(50, val_cached); }
/// <summary> /// Initializes a new instance of the <see cref="LoadingCache{T}"/> class /// by using the specified cache provider and builder. /// </summary> /// <param name="provider"> /// A <see cref="ICacheProvider"/> that is used to cache object. /// </param> /// <param name="builder"> /// A <see cref="CacheBuilder{T}"/> object that contains information about /// the cache configuration. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="provider"/> or <paramref name="builder"/> are /// <c>null</c>. /// </exception> public LoadingCache(ICacheProvider provider, CacheBuilder <T> builder) { if (provider == null || builder == null) { Thrower.ThrowArgumentNullException(provider == null ? ExceptionArgument.provider : ExceptionArgument.builder); } cache_provider_ = provider; cache_guid_ = Guid.NewGuid().ToString("N"); expire_after_access_nanos_ = builder.ExpiryAfterAccessNanos; expire_after_write_nanos_ = builder.ExpiryAfterWriteNanos; refresh_nanos_ = builder.RefreshNanos; strength_type_ = builder.ValueStrength; mutex_ = new object(); t_is_value_type_ = typeof(T).IsValueType; default_cache_loader_ = CacheLoader <T> .From(delegate { throw new NotSupportedException(); }); }
public void ShouldNotAcceptNegativeExpirarionOrRefreshValues() { CacheBuilder <string> builder = new CacheBuilder <string>(); Assert.Throws <ArgumentOutOfRangeException>( delegate() { builder.ExpireAfterAccess(-1, TimeUnit.Seconds); }); Assert.Throws <ArgumentOutOfRangeException>( delegate() { builder.ExpireAfterWrite(-1, TimeUnit.Seconds); }); Assert.Throws <ArgumentOutOfRangeException>( delegate() { builder.RefreshAfterWrite(-1, TimeUnit.Seconds); }); }
public LoadingCacheMock(CacheBuilder <T> builder) : base(new CacheProviderMock(), builder) { }
/// <summary> /// Initializes a new instance of the <see cref="LocalManualCache{T}"/> by /// using the specified cache provider, builder and automatic loader. /// </summary> /// <param name="provider"> /// A <see cref="ICacheProvider"/> object that is used to store the cached /// items. /// </param> /// <param name="builder"> /// A <see cref="CacheBuilder{T}"/> containing the configured options for /// this cache. /// </param> /// <param name="loader"> /// A <see cref="loader"/> that is used to automatically load values. /// </param> protected LocalManualCache(ICacheProvider provider, CacheBuilder <T> builder, CacheLoader <T> loader) : base(provider, builder, loader) { }
/// <summary> /// Initializes a new instance of the <see cref="LocalManualCache{T}"/> by /// using the specified cache provider and builder. /// </summary> /// <param name="provider"> /// A <see cref="ICacheProvider"/> object that is used to store the cached /// items. /// </param> /// <param name="builder"> /// A <see cref="CacheBuilder{T}"/> containing the configured options for /// this cache. /// </param> internal LocalManualCache(ICacheProvider provider, CacheBuilder <T> builder) : base(provider, builder) { }