Skip to content

Commit

Permalink
Adds ttlInSeconds to example Dapr application
Browse files Browse the repository at this point in the history
Signed-off-by: joshvanl <me@joshvanl.dev>
  • Loading branch information
JoshVanL committed Oct 12, 2023
1 parent 2f0cd2a commit 0718834
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions examples/Actor/DemoActor/DemoActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public DemoActor(ActorHost host, BankService bank)
this.bank = bank;
}

public async Task SaveData(MyData data)
public async Task SaveData(MyData data, int? ttlInSeconds = null)
{
Console.WriteLine($"This is Actor id {this.Id} with data {data}.");

// Set State using StateManager, state is saved after the method execution.
await this.StateManager.SetStateAsync<MyData>(StateName, data);
await this.StateManager.SetStateAsync<MyData>(StateName, data, ttlInSeconds: ttlInSeconds);
}

public Task<MyData> GetData()
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task ReceiveReminderAsync(string reminderName, byte[] state, TimeSp
// This method is invoked when an actor reminder is fired.
var actorState = await this.StateManager.GetStateAsync<MyData>(StateName);
actorState.PropertyB = $"Reminder triggered at '{DateTime.Now:yyyy-MM-ddTHH:mm:ss}'";
await this.StateManager.SetStateAsync<MyData>(StateName, actorState);
await this.StateManager.SetStateAsync<MyData>(StateName, actorState, ttlInSeconds: 360);
}

class TimerParams
Expand Down Expand Up @@ -164,7 +164,7 @@ public async Task TimerCallback(byte[] data)
{
var state = await this.StateManager.GetStateAsync<MyData>(StateName);
state.PropertyA = $"Timer triggered at '{DateTime.Now:yyyyy-MM-ddTHH:mm:s}'";
await this.StateManager.SetStateAsync<MyData>(StateName, state);
await this.StateManager.SetStateAsync<MyData>(StateName, state, ttlInSeconds: 360);
var timerParams = JsonSerializer.Deserialize<TimerParams>(data);
Console.WriteLine("Timer parameter1: " + timerParams.IntParam);
Console.WriteLine("Timer parameter2: " + timerParams.StringParam);
Expand Down
5 changes: 3 additions & 2 deletions examples/Actor/IDemoActor/IDemoActor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,8 +27,9 @@ public interface IDemoActor : IActor
/// Method to save data.
/// </summary>
/// <param name="data">DAta to save.</param>
/// <param name="ttlInSeconds">Optional TTL in seconds.</param>
/// <returns>A task that represents the asynchronous save operation.</returns>
Task SaveData(MyData data);
Task SaveData(MyData data, int? ttlInSeconds = null);

/// <summary>
/// Method to get data.
Expand Down

0 comments on commit 0718834

Please sign in to comment.