Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis Add IncrementItemt object to SortedSet,by hulk huang;30/Aug/2019 #196

Merged
merged 1 commit into from
Sep 1, 2019
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 @@ -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
}
}