Sync Services
Sync Services synchronize data between your database layer and cache layer, offering a seamless way to move data between them.
What Is a Sync Service?
A Sync Service implements ISyncService and defines two key operations:
PullAsync()— loads data from a remote source (e.g., database) into the local in-memory cachePushAsync()— pushes data from the cache to a remote target (e.g., database)
Example
[Service(typeof(ISyncService))]
public class LeaderboardSyncService : AbstractVaultCacheSyncService<LeaderboardModel>
{
public LeaderboardSyncService(ICacheProvider cache, IVault<LeaderboardModel>? vault = null)
: base(cache, vault) { }
public override async Task PullAsync()
{
ValidateVault();
var top100 = await _vault
.OrderByDescending(x => x.Score)
.Take(100)
.ToListAsync();
foreach (var entry in top100)
{
await SaveAsync(entry);
}
}
}
The [Service(typeof(ISyncService))] attribute registers the service — Altruist auto-discovers it and calls PullAsync at startup.
Behavior
- Altruist automatically calls
PullAsyncat startup to preload cache from the database - Runtime syncs must be triggered manually
- If no database is configured, sync services gracefully fall back to cache-only operations
Best Practices
- Keep
PullAsync()logic lightweight - Only override
PullAsync()if you need filtered or partial data - Use
PushAsync()for periodic flushing of cache to database