/// <summary> /// Gets the specified cached value /// </summary> public CachedValue <T> Get <T>(string key, string region) { string k = GetCacheKey(key, region); byte[] bytes = DistributedCache.Get(k); if (bytes == null) { return(null); } var storage = DistributedStorage.FromBytes(Serializer, bytes); return(storage.ToCachedValue <T>(Serializer)); }
/// <summary> /// Marks the specified cached value as modified /// </summary> public void MarkAsValidated(string key, string region) { var now = DateTime.UtcNow; string k = GetCacheKey(key, region); byte[] bytes = DistributedCache.Get(k); if (bytes == null) { return; } var storage = DistributedStorage.FromBytes(Serializer, bytes); storage.LastValidatedDate = now; DistributedCache.Set(k, bytes); }
/// <summary> /// Sets the specified cached value /// </summary> public CachedValue <T> Set <T>(string key, string region, T value, CacheExpiration cacheExpiration) { var now = DateTime.UtcNow; string k = GetCacheKey(key, region); DistributedStorage storage = null; string serializedValue = Serializer.Serialize(value); byte[] bytes = DistributedCache.Get(k); if (bytes == null) { storage = new DistributedStorage { CacheDate = now, Version = 0L, Value = serializedValue, LastValidatedDate = now }; } else { storage = DistributedStorage.FromBytes(Serializer, bytes); storage.Version++; storage.LastValidatedDate = now; storage.Value = serializedValue; } var options = new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = cacheExpiration.SlidingExpiration }; DistributedCache.Set(k, storage.ToBytes(Serializer), options); return(storage.ToCachedValue <T>(Serializer)); }