Skip to content

Commit

Permalink
Merge branch 'master' of github.com:imperugo/StackExchange.Redis.Exte…
Browse files Browse the repository at this point in the history
…nsions
  • Loading branch information
imperugo committed Sep 1, 2019
2 parents 4c607d8 + 1ede3da commit ed72505
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1228,5 +1228,35 @@ public interface IRedisDatabase
/// True if the object has been removed. Otherwise false
/// </returns>
Task<IEnumerable<T>> SortedSetRangeByScoreAsync<T>(string key, double start = double.NegativeInfinity, double stop = double.PositiveInfinity, Exclude exclude = Exclude.None, Order order = Order.Ascending, long skip = 0L, long take = -1L, CommandFlags flag = CommandFlags.None);

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
double SortedSetAddIncrement<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None);

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
Task<double> SortedSetAddIncrementAsync<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None);
}
}
32 changes: 31 additions & 1 deletion src/StackExchange.Redis.Extensions.Core/ICacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,5 +1168,35 @@ IEnumerable<T> SortedSetRangeByScore<T>(string key, double start = double.Negati
Task<IEnumerable<T>> SortedSetRangeByScoreAsync<T>(string key, double start = double.NegativeInfinity, double stop = double.PositiveInfinity, Exclude exclude = Exclude.None, Order order = Order.Ascending,
long skip = 0L,
long take = -1L, CommandFlags commandFlags = CommandFlags.None);
}

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
double SortedSetAddIncrement<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None);

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
Task<double> SortedSetAddIncrementAsync<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -963,5 +963,43 @@ private Dictionary<string, string> ParseInfo(string info)

return data;
}
}
/// <summary>
/// Add the entry to a sorted set with an incremen score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
public double SortedSetAddIncrement<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
var entryBytes = Serializer.Serialize(value);
return Database.SortedSetIncrement(key, entryBytes, score, commandFlags);
}

/// <summary>
/// Add the entry to a sorted set with an incremen score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
///
public async Task<double> SortedSetAddIncrementAsync<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
var entryBytes = Serializer.Serialize(value);
return await Database.SortedSetIncrementAsync(key, entryBytes, score, commandFlags);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,5 +579,15 @@ public Task<IEnumerable<T>> SortedSetRangeByScoreAsync<T>(string key, double sta
{
return cacheClient.GetDbFromConfiguration().SortedSetRangeByScoreAsync<T>(key, start, stop, exclude, order, skip, take, flag);
}

public double SortedSetAddIncrement<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
return cacheClient.GetDbFromConfiguration().SortedSetAddIncrement(key, value, score, commandFlags);
}

public Task<double> SortedSetAddIncrementAsync<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
return cacheClient.GetDbFromConfiguration().SortedSetAddIncrementAsync(key, value, score, commandFlags);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2203,5 +2203,46 @@ public async Task<IEnumerable<T>> SortedSetRangeByScoreAsync<T>(string key, doub

return result.Select(m => m == RedisValue.Null ? default(T) : Serializer.Deserialize<T>(m));
}
}

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
public double SortedSetAddIncrement<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
var entryBytes = Serializer.Serialize(value);

return Database.SortedSetIncrement(key, entryBytes, score, commandFlags);
}

/// <summary>
/// Add the entry to a sorted set with an increment score
/// </summary>
/// <remarks>
/// Time complexity: O(1)
/// </remarks>
/// <param name="key">Key of the set</param>
/// <param name="value">The instance of T.</param>
/// <param name="score">Score of the entry</param>
/// <param name="commandFlags">Command execution flags</param>
/// <returns>
/// if the object has been added return previous score. Otherwise return 0.0 when first add
/// </returns>
///
public async Task<double> SortedSetAddIncrementAsync<T>(string key, T value, double score, CommandFlags commandFlags = CommandFlags.None)
{
var entryBytes = Serializer.Serialize(value);

return await Database.SortedSetIncrementAsync(key, entryBytes, score, commandFlags);
}
}
}
23 changes: 20 additions & 3 deletions tests/StackExchange.Redis.Extensions.Tests/CacheClientTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,24 @@ public async Task Return_items_ordered_with_exclude()
Assert.Equal(descendingList[1], testobject3);
Assert.Equal(2, descendingList.Count);
}

#endregion // Sorted tests
}
[Fact]
public async Task Add_IncrementItemt_To_Sorted_Set()
{
var testobject = new TestClass<DateTime>();
var defaultscore = 1;
var nextscore = 2;
var added = await Sut.GetDbFromConfiguration().SortedSetAddIncrementAsync("my Key", testobject, defaultscore);
var added2 = await Sut.GetDbFromConfiguration().SortedSetAddIncrementAsync("my Key", testobject, nextscore);
var result = Db.SortedSetScan("my Key").First();

Assert.Equal(defaultscore, added);
Assert.Equal(defaultscore+ nextscore, result.Score);
var obj = Serializer.Deserialize<TestClass<DateTime>>(result.Element);

Assert.NotNull(obj);
Assert.Equal(testobject.Value.ToUniversalTime(), obj.Value.ToUniversalTime());
}
#endregion
// Sorted tests
}
}

0 comments on commit ed72505

Please sign in to comment.