CacheService.GetAsync
Reference
Task<T> GetAsync<T>(string key)
Use GetAsync to retrieve a value from the cache by logical key. If the key is not found the method returns the default value of T (for example null for reference types, 0 for numeric types).
Parameters
key: Logical cache key. Do not include any environment-specific prefix. Keys are case-sensitive.
Returns
Task<T>: The cached value or default(T) when the key is not present.
Error Handling & Caveats
- If
keyisnullor empty, an ArgumentException is thrown. - If the cache backend is unavailable, the method may throw a backend-specific exception (for example RedisConnectionException). Callers should handle transient errors and implement retry/backoff where appropriate.
- The cache returns
default(T)when a key is absent or expired; callers should treatdefault(T)as "not present" rather than a guaranteed persisted zero-like value. - Values are deserialized into
T; deserialization errors will raise exceptions (e.g.JsonException).
Usage
// Synchronous style in async context
var value = await CacheService.GetAsync<Prorigo.Protrak.API.Contracts.User>("my:logical:key");
if (value != null) {
LoggingService?.Debug($"Cached value: {value}");
} else {
LoggingService?.Debug("Value not found in cache");
}
Example: Safe read with fallback loader
public async Task<Prorigo.Protrak.API.Contracts.User> GetUserAsync(Guid userId)
{
var cacheKey = $"UserProfile:{userId}";
var cached = await CacheService.GetAsync<Prorigo.Protrak.API.Contracts.User>(cacheKey);
if (cached != null) return cached;
// Fallback to database
var user = await UserService.GetUserAsync(userId);
if (user != null) {
await CacheService.SetAsync(cacheKey, user, ttlSeconds: 3600);
}
return user;
}
Example: GetInstance / UpdateInstance
public async Task<Prorigo.Protrak.API.Contracts.Instance> GetInstanceAsync(Guid instanceId)
{
var cacheKey = $"Instance:{instanceId}";
var cached = await CacheService.GetAsync<Prorigo.Protrak.API.Contracts.Instance>(cacheKey);
if (cached != null) return cached;
// Load from primary store using InstanceService (synchronous API as documented)
var instance = await InstanceService.GetInstanceAsync(instanceId, new string[] { });
if (instance != null) {
// Cache for subsequent reads
await CacheService.SetAsync(cacheKey, instance, ttlSeconds: 3600);
}
return instance;
}
public async Task UpdateInstanceAsync(Guid instanceId, Prorigo.Protrak.API.Contracts.Instance instance)
{
// Persist update via InstanceService.UpdateInstance(instance, lastModified)
InstanceService.UpdateInstance(instance, instance.Modified);
// Update cache so readers see latest value
// ideally use the lazy caching pattern
var cacheKey = $"Instance:{instanceId}";
await CacheService.SetAsync(cacheKey, instance, ttlSeconds: 3600);
}
Troubleshooting
- If the cache always returns
default(T), verify the key format and that the value was previously set. - If deserialization fails, confirm that the stored value was serialized with a compatible contract for
T. - For high-throughput reads, monitor cache hit ratio and tune TTLs to balance staleness vs backend load.