Skip to content

Commit

Permalink
Merge pull request #393 from reisenberger/v570tidyTtlStrategies
Browse files Browse the repository at this point in the history
More on `ResultTtl`, merged to `v5.7.0` branch
  • Loading branch information
reisenberger authored Jan 6, 2018
2 parents a10dbd2 + 40d376c commit 4df2018
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

## 5.7.0
- Minor cache fixes
- Add ability to calculate cache Ttl based on item to cache
- Allow user-created custom policies

## 5.6.1
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ For details of changes by release see the [change log](https://github.com/App-vN
* [@reisenberger](https://github.com/reisenberger) - Add new .HandleInner<TException>(...) syntax for handling inner exceptions natively.
* [@rjongeneelen](https://github.com/rjongeneelen) and [@reisenberger](https://github.com/reisenberger) - Allow PolicyWrap configuration to configure policies via interfaces.
* [@reisenberger](https://github.com/reisenberger) - Performance improvements.
* [@awarrenlove](https://github.com/awarrenlove) - Add ability to calculate cache Ttl based on item to cache.
# Sample Projects

Expand Down
1 change: 1 addition & 0 deletions src/Polly.Net40Async.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
5.7.0
---------------------
- Minor cache fixes
- Add ability to calculate cache Ttl based on item to cache
- Allow user-created custom policies

5.6.1
Expand Down
20 changes: 14 additions & 6 deletions src/Polly.Shared/Caching/ResultTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@
namespace Polly.Caching
{
/// <summary>
/// Defines a ttl strategy which will cache items with some calculation from the result of the execution.
/// Defines a ttl strategy which can calculate a duration to cache items dynamically based on the execution context and result of the execution.
/// </summary>

/// <typeparam name="TResult">The type of results that the ttl calculation function will take as an input parameter.</typeparam>
public class ResultTtl<TResult> : ITtlStrategy<TResult>
{
private readonly Func<TResult, Ttl> ttlFunc;
private readonly Func<Context, TResult, Ttl> _ttlFunc;

/// <summary>
/// Constructs a new instance of the <see cref="ResultTtl{TResult}"/> ttl strategy.
/// Constructs a new instance of the <see cref="ResultTtl{TResult}"/> ttl strategy, with a func calculating <see cref="Ttl"/> based on the <typeparamref name="TResult"/> value to cache.
/// </summary>
/// <param name="ttlFunc">The function to calculate the TTL for which cache items should be considered valid.</param>
public ResultTtl(Func<TResult, Ttl> ttlFunc)
{
if (ttlFunc == null) throw new ArgumentNullException(nameof(ttlFunc));
_ttlFunc = (context, result) => ttlFunc(result);
}

this.ttlFunc = ttlFunc;
/// <summary>
/// Constructs a new instance of the <see cref="ResultTtl{TResult}"/> ttl strategy, with a func calculating <see cref="Ttl"/> based on the execution <see cref="Context"/> and <typeparamref name="TResult"/> value to cache.
/// </summary>
/// <param name="ttlFunc">The function to calculate the TTL for which cache items should be considered valid.</param>
public ResultTtl(Func<Context, TResult, Ttl> ttlFunc)
{
_ttlFunc = ttlFunc ?? throw new ArgumentNullException(nameof(ttlFunc));
}

/// <summary>
Expand All @@ -32,7 +40,7 @@ public ResultTtl(Func<TResult, Ttl> ttlFunc)

public Ttl GetTtl(Context context, TResult result)
{
return ttlFunc(result);
return _ttlFunc(context, result);
}
}
}
41 changes: 35 additions & 6 deletions src/Polly.SharedSpecs/Caching/ResultTtlSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using FluentAssertions;
using Polly.Caching;
using Polly.Specs.Helpers.Caching;
using Xunit;

namespace Polly.Specs.Caching
Expand All @@ -12,15 +11,31 @@ public class ResultTtlSpecs
[Fact]
public void Should_throw_when_func_is_null()
{
Action configure = () => new ResultTtl<dynamic>(null);
Action configure = () => new ResultTtl<object>((Func<object, Ttl>)null);

configure.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("ttlFunc");
}

[Fact]
public void Should_throw_when_func_is_null_using_context()
{
Action configure = () => new ResultTtl<object>((Func<Context, object, Ttl>)null);

configure.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("ttlFunc");
}

[Fact]
public void Should_not_throw_when_func_is_set()
{
Action configure = () => new ResultTtl<dynamic>((result) => new Ttl());
Action configure = () => new ResultTtl<object>((result) => new Ttl());

configure.ShouldNotThrow();
}

[Fact]
public void Should_not_throw_when_func_is_set_using_context()
{
Action configure = () => new ResultTtl<object>((context, result) => new Ttl());

configure.ShouldNotThrow();
}
Expand All @@ -29,13 +44,27 @@ public void Should_not_throw_when_func_is_set()
public void Should_return_func_result()
{
TimeSpan ttl = TimeSpan.FromMinutes(1);
Func<dynamic, Ttl> func = (result) => { return new Ttl(ttl); };
Func<dynamic, Ttl> func = (result) => { return new Ttl(result.Ttl); };

ResultTtl<dynamic> ttlStrategy = new ResultTtl<dynamic>(func);

Ttl retrieved = ttlStrategy.GetTtl(new Context("someExecutionKey"), new { Ttl = ttl });
retrieved.Timespan.Should().BeCloseTo(ttl);
retrieved.Timespan.Should().Be(ttl);
retrieved.SlidingExpiration.Should().BeFalse();
}

[Fact]
public void Should_return_func_result_using_context()
{
const string specialKey = "specialKey";

TimeSpan ttl = TimeSpan.FromMinutes(1);
Func<Context, dynamic, Ttl> func = (context, result) => { return context.ExecutionKey == specialKey ? new Ttl(TimeSpan.Zero) : new Ttl(result.Ttl); };

ResultTtl<dynamic> ttlStrategy = new ResultTtl<dynamic>(func);

ttlStrategy.GetTtl(new Context("someExecutionKey"), new { Ttl = ttl }).Timespan.Should().Be(ttl);
ttlStrategy.GetTtl(new Context(specialKey), new { Ttl = ttl }).Timespan.Should().Be(TimeSpan.Zero);
}
}
}
1 change: 1 addition & 0 deletions src/Polly.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
5.7.0
---------------------
- Minor cache fixes
- Add ability to calculate cache Ttl based on item to cache
- Allow user-created custom policies

5.6.1
Expand Down

0 comments on commit 4df2018

Please sign in to comment.