Skip to main content

CacheService.RemoveAsync

Reference

Task RemoveAsync(string key)

Use RemoveAsync to remove an entry from cache by logical key. After removal, subsequent reads for the same key will return the default value of the requested type.

Parameters

  • key: Logical cache key. Do not include any environment-specific prefix. Keys are case-sensitive.

Returns

Task: Completes successfully when the key is removed from the cache backend.

Error Handling & Caveats

  • If key is null or empty, an ArgumentException is thrown.
  • Removing a non-existent key is a no-op and completes successfully.
  • If the cache backend is unavailable, the method may throw a backend-specific exception. Consider best-effort removal for non-critical scenarios or retries for critical operations.

Usage

// Remove a single key
await CacheService.RemoveAsync("Instance:{instanceId}");

// Remove keys as part of a cache invalidation step
await CacheService.RemoveAsync($"UserProfile:{userId}");

Example: Invalidate dependent keys after update

public async Task UpdateUserProfileAsync(Guid InstanceId, Prorigo.Protrak.API.Contracts.Instance userProfile) {
// first Invalidate cached profile
await CacheService.RemoveAsync($"UserProfile:{InstanceId}");
await InstanceService.UpdateInstanceAsync(userProfile, userProfile.Modified);
}

Example: RemoveInstance Cached Data (invalidate after delete)

public async Task DeleteInstanceAsync(Guid InstanceId) {
// first invalidate cache
var cacheKey = $"Instance:{InstanceId}";
await CacheService.RemoveAsync(cacheKey);

var instance = await InstanceService.GetInstanceAsync(InstanceId, new string[] { });
if (instance != null) {
// update logic here
await InstanceService.DeleteInstanceAsync(InstanceId);
}
}

Troubleshooting

  • If removal appears ineffective, confirm that the key format used by the reader and the remover is identical.
  • If the backend reports errors, check service availability and authentication.
  • For distributed caches, ensure replication/synchronization delays are considered