Skip to content

Commit a3bac4d

Browse files
authored
Implement UpdateTimeToLive [HZ-5249][API-1789] (#997)
Implemented missing `Map.UpdateTimeToLive` API.
1 parent d5561dd commit a3bac4d

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

src/Hazelcast.Net.Tests/Remote/ClientMapTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414
using System;
1515
using System.Collections.Generic;
16+
using System.Diagnostics;
1617
using System.Linq;
1718
using System.Threading;
1819
using System.Threading.Tasks;
@@ -1407,5 +1408,57 @@ public async Task TestValuesPredicate()
14071408
Assert.IsTrue(enumerator.MoveNext());
14081409
Assert.AreEqual("value1", enumerator.Current);
14091410
}
1411+
1412+
[Test]
1413+
public async Task TestTTLUpdated()
1414+
{
1415+
var map = await Client.GetMapAsync<string, string>(CreateUniqueName());
1416+
// in seconds
1417+
var maxTTL = 100;
1418+
var minTTL = 3;
1419+
var key = "key1";
1420+
var latch = new ManualResetEvent(false);
1421+
1422+
var starTime = Stopwatch.StartNew();
1423+
await map.SetAsync(key, "value1", TimeSpan.FromSeconds(maxTTL));
1424+
1425+
// Notify when key removed
1426+
var parallelCheck = Task.Run(async () =>
1427+
{
1428+
await AssertEx.SucceedsEventually(
1429+
async () => Assert.False(await map.ContainsKeyAsync(key)),
1430+
maxTTL * 1000,
1431+
500);
1432+
latch.Set();
1433+
});
1434+
1435+
// Reduce the TTL
1436+
Assert.True(await map.SetTTL(key, TimeSpan.FromSeconds(minTTL)));
1437+
1438+
await latch.WaitOneAsync();
1439+
starTime.Stop();
1440+
var elapsed = starTime.ElapsedMilliseconds;
1441+
1442+
// Definitively, less then maxTTL
1443+
Assert.Less(elapsed, maxTTL * 1000);
1444+
}
1445+
1446+
[Test]
1447+
public async Task TestTTLReturnsFalseWhenKeyDoesNotExist()
1448+
{
1449+
var map = await Client.GetMapAsync<string, string>(CreateUniqueName());
1450+
Assert.False(await map.SetTTL("non-existing-key", TimeSpan.FromSeconds(10)));
1451+
}
1452+
1453+
1454+
[Test]
1455+
public async Task TestTTLReturnsFalseWhenKeyAlreadyExpired()
1456+
{
1457+
var map = await Client.GetMapAsync<string, string>(CreateUniqueName());
1458+
var key = "non-existing-key";
1459+
await map.SetAsync(key, "value1", TimeSpan.FromMilliseconds(10));
1460+
await Task.Delay(100);
1461+
Assert.False(await map.SetTTL("non-existing-key", TimeSpan.FromMilliseconds(10)));
1462+
}
14101463
}
14111464
}

src/Hazelcast.Net/DistributedObjects/IHMap.Setting.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ public partial interface IHMap<TKey, TValue> // Setting
250250
/// <remarks>
251251
/// <para>The value is automatically expired, evicted and removed after the
252252
/// <paramref name="timeToLive"/> has elapsed. If <paramref name="timeToLive"/> is infinite (0ms),
253-
/// the value is retained indefinitely. If it is -1ms,
254-
/// it lives for the duration of the server-configured time-to-live.</para>
253+
/// the value is retained indefinitely. If it is negative,
254+
/// it lives for the duration of the server-configured (default: forever) time-to-live.</para>
255255
/// <para>The new time-to-live value is valid starting from the time this operation is invoked,
256256
/// not since the time the entry was created.</para>
257257
/// <para>
@@ -265,6 +265,6 @@ public partial interface IHMap<TKey, TValue> // Setting
265265
/// Time resolution for <paramref name="timeToLive"></paramref> is seconds. The given value is rounded to the next closest second value.
266266
/// </para>
267267
/// </remarks>
268-
Task<bool> UpdateTimeToLive(TKey key, TimeSpan timeToLive);
268+
Task<bool> SetTTL(TKey key, TimeSpan timeToLive);
269269
}
270270
}

src/Hazelcast.Net/DistributedObjects/Impl/HMap.Setting.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,15 @@ Task SetTransientAsync(IData keyData, IData valueData, TimeSpan timeToLive, Time
392392
#endif
393393
}
394394

395-
public Task<bool> UpdateTimeToLive(TKey key, TimeSpan timeToLive)
395+
public async Task<bool> SetTTL(TKey key, TimeSpan timeToLive)
396396
{
397-
throw new NotImplementedException();
397+
var keyData = ToSafeData(key);
398+
var millis = Convert.ToInt64(timeToLive.TotalMilliseconds);
399+
var requestMessage = MapSetTtlCodec.EncodeRequest(Name, keyData, millis);
400+
401+
var result= await Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, keyData).CfAwait();
402+
var decoded = MapSetTtlCodec.DecodeResponse(result);
403+
return decoded.Response;
398404
}
399405
}
400406
}

0 commit comments

Comments
 (0)