Skip to content

[ACTORS] [Feature Request] Unload state operation #1728

@olitomlinson

Description

@olitomlinson

Describe the feature

it would be great to have an UnloadStateAsync() method on StateManager

This would only remove from key from the in-memory cache / tracking store, but not remove from the underlying store.

In memory-intensive applications, it would be great to be able unload keys from memory when they're not needed, and simply re-load them via the usual GetStateAsync / TryGetStateAsync methods.

Risk

Of course there is a risk that someone unloads a key, which, already had been modified but not yet persisted, so guards could be put in place to prevent that by default.

For example, assuming that Key X had been added/modified but not yet saved / flushed,

When calling this.StateManager.UnloadStateAsync("X");

I would expect this to throw an exception e.g. "UnloadingModifiedState" exception.

However, for use-cases where this is acceptable, an option could be provided which would suppress that exception

this.StateManager.UnloadStateAsync("X", options = new UnloadStateOptions{ AllowUnloadingWhenStateModified = true} );


Example

// add this big 1mb payload to the actor state
await this.StateManager.AddStateAsync("1mb-important-data", payload);

// flush to disk
await this.StateManager.SaveStateAsync();

// unload from memory, as we aren't going to need it until some point in the future
await this.StateManager.UnloadStateAsync("1mb-important-data");

// minutes / hour / days / weeks later - load the state 
var payload = await this.StateManager.GetStateAsync<object>("1mb-important-data");

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions