protected virtual TValue PutIfAbsentInternal(IData keyData, TValue value, long ttl, TimeUnit timeunit) { var valueData = ToData(value); var request = MapPutIfAbsentCodec.EncodeRequest(Name, keyData, valueData, GetThreadId(), timeunit.ToMillis(ttl)); var clientMessage = Invoke(request, keyData); var response = MapPutIfAbsentCodec.DecodeResponse(clientMessage).Response; return(ToObject <TValue>(response)); }
public V PutIfAbsent(K key, V value, long ttl, TimeUnit timeunit) { var keyData = ToData(key); var valueData = ToData(value); var request = MapPutIfAbsentCodec.EncodeRequest(GetName(), keyData, valueData, ThreadUtil.GetThreadId(), timeunit.ToMillis(ttl)); var clientMessage = Invoke(request, keyData); var response = MapPutIfAbsentCodec.DecodeResponse(clientMessage).response; return(ToObject <V>(response)); }
/// <summary> /// Adds an entry with a time-to-live, if no entry with the key exists. /// </summary> /// <param name="keyData">A key.</param> /// <param name="valueData">The value.</param> /// <param name="timeToLive">A time to live.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>The existing value, if any; otherwise the default value.</returns> /// <remarks> /// <para>The value is automatically expired, evicted and removed after the <paramref name="timeToLive"/> has elapsed..</para> /// <para>If the <paramref name="timeToLive"/> is <see cref="Timeout.InfiniteTimeSpan"/>, the entry lives forever.</para> /// </remarks> protected virtual async Task <TValue> GetOrAdd(IData keyData, IData valueData, TimeSpan timeToLive, CancellationToken cancellationToken) { var timeToLiveMs = timeToLive.CodecMilliseconds(-1000); var requestMessage = MapPutIfAbsentCodec.EncodeRequest(Name, keyData, valueData, ContextId, timeToLiveMs); var responseMessage = await Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, keyData, cancellationToken).CAF(); var response = MapPutIfAbsentCodec.DecodeResponse(responseMessage).Response; return(ToObject <TValue>(response)); }
/// <summary> /// Adds an entry with a time-to-live, if no entry with the key exists. /// </summary> /// <param name="keyData">A key.</param> /// <param name="valueData">The value.</param> /// <param name="timeToLive">A time to live.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>The existing value, if any; otherwise the default value.</returns> /// <remarks> /// <para>The value is automatically expired, evicted and removed after the <paramref name="timeToLive"/> has elapsed..</para> /// <para>If the <paramref name="timeToLive"/> is -1ms, the entry lives forever.</para> /// </remarks> protected virtual async Task <TValue> GetOrAdd(IData keyData, IData valueData, TimeSpan timeToLive, TimeSpan maxIdle, CancellationToken cancellationToken) { var timeToLiveMs = timeToLive.RoundedMilliseconds(false); // codec: 0 is infinite, -1 is server var maxIdleMs = maxIdle.RoundedMilliseconds(false); // codec: 0 is infinite, -1 is server var withMaxIdle = maxIdleMs != -1; var requestMessage = withMaxIdle ? MapPutIfAbsentWithMaxIdleCodec.EncodeRequest(Name, keyData, valueData, ContextId, timeToLiveMs, maxIdleMs) : MapPutIfAbsentCodec.EncodeRequest(Name, keyData, valueData, ContextId, timeToLiveMs); var responseMessage = await Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, keyData, cancellationToken).CfAwait(); var response = withMaxIdle ? MapPutIfAbsentWithMaxIdleCodec.DecodeResponse(responseMessage).Response : MapPutIfAbsentCodec.DecodeResponse(responseMessage).Response; return(ToObject <TValue>(response)); }