ICacheService
Thread-safe cache interface for storing and retrieving Graph data per tenant.ICacheService.cs:7 for the complete interface.
Storage Architecture
Database Location
Encryption
- Database password: Random 32-byte value, base64-encoded
- Password storage: Encrypted using
Microsoft.AspNetCore.DataProtectionwith purposeIntune.Commander.Cache.Password.v1 - DataProtection keys: Stored at
%LocalAppData%/Intune.Commander/keys
CacheService.cs:261 for password management logic.
Cache Schema
TenantId- for tenant-wide invalidationExpiresAtUtc- for cleanup queries
CacheEntry.cs:7 for the model definition.
Methods
Get
Retrieves cached data for a tenant and data type.Azure AD tenant ID
Cache key (e.g.,
"DeviceConfigurations", "CompliancePolicies")Cached data, or
null if missing/expired/corrupted- Returns
nullif entry doesn’t exist - Returns
nullif entry is expired (and deletes the stale entry) - Returns
nullif deserialization fails (schema change, corruption) - Deserializes with polymorphic type resolution for Graph models
CacheService.cs:71 for implementation.
Set
Stores data in the cache with optional TTL.Azure AD tenant ID
Cache key
Data to cache
Time to live. Defaults to 24 hours if not specified.
- Upserts the cache entry (replaces if exists)
- Serializes with runtime type information to preserve polymorphic properties
- Sets
ExpiresAtUtc = DateTime.UtcNow + ttl
CacheService.cs:98 for implementation.
Invalidate
Removes cached data for a tenant.Azure AD tenant ID
Specific data type to invalidate, or
null to clear all data for the tenantCacheService.cs:117 for implementation.
CleanupExpired
Removes all expired entries from the database.Number of entries deleted
CacheService.cs:129 for implementation.
GetMetadata
Retrieves cache metadata without deserializing the data.Azure AD tenant ID
Cache key
When the data was cached (UTC)
Number of items in the cached collection
Tuple with metadata, or
null if entry is missing/expiredCacheService.cs:142 for implementation.
Polymorphic Deserialization
The cache service uses OData type discriminators to preserve derived types during serialization/deserialization.How It Works
- Serialization: Each item is serialized with its runtime type (not generic
T) - Deserialization: Reads the
@odata.typefield from JSON and resolves to the correct C# type - Type cache: Resolved types are cached in a
ConcurrentDictionaryfor performance
List<DeviceConfiguration>, the cache:
- Sees
@odata.type: #microsoft.graph.windows10GeneralConfiguration - Resolves to
Windows10GeneralConfigurationtype - Deserializes with correct derived type
- Returns
DeviceConfigurationlist with runtime types preserved
CacheService.cs:203 for polymorphic deserialization logic.
Usage Patterns
Read-Through Cache
Cache Invalidation on Write
Tenant Isolation
The cache automatically isolates data by tenant ID:Performance Considerations
When to Cache
Good candidates:- Device configurations (low change rate)
- Compliance policies (low change rate)
- Applications (low change rate)
- Scope tags, role definitions (rarely change)
- Managed devices (high change rate, real-time data desired)
- App install status (real-time)
- Compliance reports (real-time)
TTL Recommendations
| Data Type | Recommended TTL | Reason |
|---|---|---|
| Device configurations | 24 hours (default) | Low change rate |
| Groups | 1 hour | Moderate change rate |
| Managed devices | Do not cache | Real-time data |
| Scope tags | 7 days | Rarely change |
Cache Size
The LiteDB database is unbounded but self-manages via TTL expiration. Monitor cache size:Troubleshooting
Cache Corruption
If the cache database or key file is corrupted:- The service detects unreadable data during
Get() - Returns
nulland deletes the stale entry - Next
Set()overwrites with fresh data
- Delete
cache.dbandcache-key.binmanually - Restart the application
- New cache will be created automatically
Key Rotation
If DataProtection keys are rotated (e.g., machine reinstall):- The password file can’t be decrypted
CacheServiceconstructor catches the exception- Deletes
cache.db,cache.db-log, andcache-key.bin - Generates a new password and database
CacheService.cs:261 for key rotation recovery logic.
Migration from Legacy IntuneManager
The cache uses a new database (no migration needed). Legacy cache at%LocalAppData%/IntuneManager/cache.db is not migrated and can be deleted manually.
Next Steps
Export Service
Export configurations to JSON for backup and migration
Graph Services
Return to Graph API service documentation