Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
this.cache.AddOrUpdate(1, 1);
this.cache.AddOrUpdate(2, 2);

this.cache.Trim(1);
this.cache.Policy.Eviction.Trim(1);

this.cache.TryGet(0, out var value).Should().BeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
this.cache.AddOrUpdate(1, 1);
this.cache.AddOrUpdate(2, 2);

this.cache.Trim(1);
this.cache.Policy.Eviction.Trim(1);

this.cache.TryGet(0, out var value).Should().BeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
26 changes: 26 additions & 0 deletions BitFaster.Caching.UnitTests/CachePolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Xunit;

namespace BitFaster.Caching.UnitTests
{
public class CachePolicyTests
{
[Fact]
public void WhenCtorFieldsAreAssigned()
{
var eviction = new Mock<IBoundedPolicy>();
var expire = new Mock<ITimePolicy>();

var cp = new CachePolicy(eviction.Object, expire.Object);

cp.Eviction.Should().Be(eviction.Object);
cp.ExpireAfterWrite.Should().Be(expire.Object);
}
}
}
7 changes: 7 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ private void OnItemRemoved(object sender, ItemRemovedEventArgs<int, string> e)
throw new NotImplementedException();
}

[Fact]
public void ExpireAfterWriteIsDisabled()
{
lru.Policy.ExpireAfterWrite.Should().Be(NoneTimePolicy.Instance);
lru.Policy.ExpireAfterWrite.CanExpire.Should().BeFalse();
}

[Fact]
public void WhenKeyIsRequestedItIsCreatedAndCached()
{
Expand Down
6 changes: 3 additions & 3 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void AsAsyncTestMetricsTLru()
.Build();

lru.Should().BeOfType<ConcurrentTLru<int, int>>();
lru.Capacity.Should().Be(128);
lru.Policy.Eviction.Capacity.Should().Be(128);
}


Expand Down Expand Up @@ -268,7 +268,7 @@ public void AsAsyncWithScoped()

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();

lru.Capacity.Should().Be(3);
lru.Policy.Eviction.Capacity.Should().Be(3);
}

// 7
Expand All @@ -282,7 +282,7 @@ public void WithScopedAsAsync()
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
lru.Policy.Eviction.Capacity.Should().Be(3);
}

// 8
Expand Down
12 changes: 12 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ public void WhenRefToMetricsIsCapturedResultIsCorrect()
m.HitRatio.Should().Be(0.5);
}

[Fact]
public void CanExpireIsFalse()
{
this.lru.CanExpire.Should().BeFalse();
}

[Fact]
public void TimeToLiveIsInfinite()
{
this.lru.TimeToLive.Should().Be(NoneTimePolicy.Infinite);
}

[Fact]
public void WhenKeyIsRequestedItIsCreatedAndCached()
{
Expand Down
12 changes: 12 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void ConstructPartitionCtorReturnsCapacity()
x.Capacity.Should().Be(3);
}

[Fact]
public void CanExpireIsTrue()
{
this.lru.CanExpire.Should().BeTrue();
}

[Fact]
public void TimeToLiveIsCtorArg()
{
this.lru.TimeToLive.Should().Be(timeToLive);
}

[Fact]
public void WhenItemIsNotExpiredItIsNotRemoved()
{
Expand Down
6 changes: 6 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/LruPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public class LruPolicyTests
{
private readonly LruPolicy<int, int> policy = new LruPolicy<int, int>();

[Fact]
public void TimeToLiveIsInfinite()
{
this.policy.TimeToLive.Should().Be(NoneTimePolicy.Infinite);
}

[Fact]
public void CreateItemInitializesKeyAndValue()
{
Expand Down
6 changes: 6 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/TlruDateTimePolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class TLruDateTimePolicyTests
{
private readonly TLruDateTimePolicy<int, int> policy = new TLruDateTimePolicy<int, int>(TimeSpan.FromSeconds(10));

[Fact]
public void TimeToLiveShouldBeTenSecs()
{
this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10));
}

[Fact]
public void CreateItemInitializesKeyAndValue()
{
Expand Down
6 changes: 6 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class TLruLongTicksPolicyTests
{
private readonly TLruLongTicksPolicy<int, int> policy = new TLruLongTicksPolicy<int, int>(TimeSpan.FromSeconds(10));

[Fact]
public void TimeToLiveShouldBeTenSecs()
{
this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10));
}

[Fact]
public void CreateItemInitializesKeyAndValue()
{
Expand Down
6 changes: 6 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/TlruTicksPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class TLruTicksPolicyTests
{
private readonly TLruTicksPolicy<int, int> policy = new TLruTicksPolicy<int, int>(TimeSpan.FromSeconds(10));

[Fact]
public void TimeToLiveShouldBeTenSecs()
{
this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10));
}

[Fact]
public void CreateItemInitializesKeyAndValue()
{
Expand Down
33 changes: 33 additions & 0 deletions BitFaster.Caching.UnitTests/NoneTimePolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace BitFaster.Caching.UnitTests
{
public class NoneTimePolicyTests
{
[Fact]
public void CanExpireIsFalse()
{
NoneTimePolicy.Instance.CanExpire.Should().BeFalse();
}

[Fact]
public void TimeToLiveIsInfinite()
{
NoneTimePolicy.Instance.TimeToLive.Should().Be(NoneTimePolicy.Infinite);
}

[Fact]
public void TrimExpiredIsNoOp()
{
Action trimExpired = () => NoneTimePolicy.Instance.TrimExpired();

trimExpired.Should().NotThrow();
}
}
}
4 changes: 2 additions & 2 deletions BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected ScopedAsyncCacheTestBase(IScopedAsyncCache<int, Disposable> cache)
[Fact]
public void WhenCreatedCapacityPropertyWrapsInnerCache()
{
this.cache.Capacity.Should().Be(capacity);
this.cache.Policy.Eviction.Capacity.Should().Be(capacity);
}

[Fact]
Expand Down Expand Up @@ -138,7 +138,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
this.cache.AddOrUpdate(1, new Disposable());
this.cache.AddOrUpdate(2, new Disposable());

this.cache.Trim(1);
this.cache.Policy.Eviction.Trim(1);

this.cache.ScopedTryGet(0, out var lifetime).Should().BeFalse();
}
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
this.cache.AddOrUpdate(1, new Disposable());
this.cache.AddOrUpdate(2, new Disposable());

this.cache.Trim(1);
this.cache.Policy.Eviction.Trim(1);

this.cache.ScopedTryGet(0, out var lifetime).Should().BeFalse();
}
Expand Down
7 changes: 2 additions & 5 deletions BitFaster.Caching/Atomic/AtomicFactoryAsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public AtomicFactoryAsyncCache(ICache<K, AsyncAtomicFactory<K, V>> cache)

public ICollection<K> Keys => this.cache.Keys;

public CachePolicy Policy => this.cache.Policy;

public void AddOrUpdate(K key, V value)
{
cache.AddOrUpdate(key, new AsyncAtomicFactory<K, V>(value));
Expand All @@ -49,11 +51,6 @@ public ValueTask<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
return synchronized.GetValueAsync(key, valueFactory);
}

public void Trim(int itemCount)
{
cache.Trim(itemCount);
}

public bool TryGet(K key, out V value)
{
AsyncAtomicFactory<K, V> output;
Expand Down
7 changes: 2 additions & 5 deletions BitFaster.Caching/Atomic/AtomicFactoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public AtomicFactoryCache(ICache<K, AtomicFactory<K, V>> cache)

public ICollection<K> Keys => this.cache.Keys;

public CachePolicy Policy => this.cache.Policy;

public void AddOrUpdate(K key, V value)
{
this.cache.AddOrUpdate(key, new AtomicFactory<K, V>(value));
Expand All @@ -49,11 +51,6 @@ public V GetOrAdd(K key, Func<K, V> valueFactory)
return atomicFactory.GetValue(key, valueFactory);
}

public void Trim(int itemCount)
{
this.cache.Trim(itemCount);
}

public bool TryGet(K key, out V value)
{
AtomicFactory<K, V> output;
Expand Down
9 changes: 2 additions & 7 deletions BitFaster.Caching/Atomic/AtomicFactoryScopedAsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public AtomicFactoryScopedAsyncCache(ICache<K, ScopedAsyncAtomicFactory<K, V>> c
this.eventProxy = new EventProxy(cache.Events);
}

public int Capacity => this.cache.Capacity;

public int Count => this.cache.Count;

public ICacheMetrics Metrics => this.cache.Metrics;

public ICacheEvents<K, Scoped<V>> Events => this.eventProxy;

public CachePolicy Policy => this.cache.Policy;

///<inheritdoc/>
public ICollection<K> Keys => this.cache.Keys;

Expand Down Expand Up @@ -83,11 +83,6 @@ public bool ScopedTryGet(K key, out Lifetime<V> lifetime)
return false;
}

public void Trim(int itemCount)
{
this.cache.Trim(itemCount);
}

public bool TryRemove(K key)
{
return this.cache.TryRemove(key);
Expand Down
7 changes: 2 additions & 5 deletions BitFaster.Caching/Atomic/AtomicFactoryScopedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public AtomicFactoryScopedCache(ICache<K, ScopedAtomicFactory<K, V>> cache)

public ICacheEvents<K, Scoped<V>> Events => this.eventProxy;

public CachePolicy Policy => this.cache.Policy;

///<inheritdoc/>
public ICollection<K> Keys => this.cache.Keys;

Expand Down Expand Up @@ -81,11 +83,6 @@ public bool ScopedTryGet(K key, out Lifetime<V> lifetime)
return false;
}

public void Trim(int itemCount)
{
this.cache.Trim(itemCount);
}

public bool TryRemove(K key)
{
return this.cache.TryRemove(key);
Expand Down
21 changes: 21 additions & 0 deletions BitFaster.Caching/CachePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BitFaster.Caching
{
public class CachePolicy
{
public CachePolicy(IBoundedPolicy eviction, ITimePolicy expireAfterWrite)
{
this.Eviction = eviction;
this.ExpireAfterWrite = expireAfterWrite;
}

public IBoundedPolicy Eviction { get; }

public ITimePolicy ExpireAfterWrite { get; }
}
}
13 changes: 2 additions & 11 deletions BitFaster.Caching/IAsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace BitFaster.Caching
/// <typeparam name="V">The type of values in the cache.</typeparam>
public interface IAsyncCache<K, V> : IEnumerable<KeyValuePair<K, V>>
{
/// <summary>
/// Gets the total number of items that can be stored in the cache.
/// </summary>
int Capacity { get; }

/// <summary>
/// Gets the number of items currently held in the cache.
/// </summary>
Expand All @@ -33,6 +28,8 @@ public interface IAsyncCache<K, V> : IEnumerable<KeyValuePair<K, V>>
/// </summary>
ICacheEvents<K, V> Events { get; }

CachePolicy Policy { get; }

/// <summary>
/// Gets a collection containing the keys in the cache.
/// </summary>
Expand Down Expand Up @@ -82,11 +79,5 @@ public interface IAsyncCache<K, V> : IEnumerable<KeyValuePair<K, V>>
/// Removes all keys and values from the cache.
/// </summary>
void Clear();

/// <summary>
/// Trim the specified number of items from the cache.
/// </summary>
/// <param name="itemCount">The number of items to remove.</param>
void Trim(int itemCount);
}
}
Loading