private void SetupEntry(ICacheEntry entry, CacheKey key) { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(key.CacheTime); entry.AddExpirationToken(new CancellationChangeToken(_clearToken.Token)); foreach (var keyPrefix in key.Prefixes) { var tokenSource = _prefixes.GetOrAdd(keyPrefix, new CancellationTokenSource()); entry.AddExpirationToken(new CancellationChangeToken(tokenSource.Token)); } }
/// <summary> /// Applies the values of an existing <see cref="MemoryCacheEntryOptions{TKey}"/> to the entry. /// </summary> /// <param name="entry">The <see cref="ICacheEntry{TKey}"/>.</param> /// <param name="options">Set the values of these options on the <paramref name="entry"/>.</param> /// <returns>The <see cref="ICacheEntry{TKey}"/> for chaining.</returns> public static ICacheEntry <TKey> SetOptions <TKey>(this ICacheEntry <TKey> entry, MemoryCacheEntryOptions <TKey> options) where TKey : notnull { if (options == null) { throw new ArgumentNullException(nameof(options)); } entry.AbsoluteExpiration = options.AbsoluteExpiration; entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; entry.SlidingExpiration = options.SlidingExpiration; entry.Priority = options.Priority; entry.Size = options.Size; foreach (var expirationToken in options.ExpirationTokens) { entry.AddExpirationToken(expirationToken); } foreach (var postEvictionCallback in options.PostEvictionCallbacks) { entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback, postEvictionCallback.State); } return(entry); }
/// <summary> /// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry. /// </summary> /// <param name="entry">The <see cref="ICacheEntry"/>.</param> /// <param name="options">Set the values of these options on the <paramref name="entry"/>.</param> /// <returns>The <see cref="ICacheEntry"/> for chaining.</returns> public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } entry.AbsoluteExpiration = options.AbsoluteExpiration; entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; entry.SlidingExpiration = options.SlidingExpiration; entry.Priority = options.Priority; entry.Size = options.Size; foreach (IChangeToken expirationToken in options.ExpirationTokens) { entry.AddExpirationToken(expirationToken); } foreach (PostEvictionCallbackRegistration postEvictionCallback in options.PostEvictionCallbacks) { entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback, postEvictionCallback.State); } return(entry); }
public static TItem Set <TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken) { using ICacheEntry entry = cache.CreateEntry(key); entry.AddExpirationToken(expirationToken); entry.Value = value; return(value); }
public static TItem Set <TKey, TItem>(this IMemoryCache <TKey> cache, TKey key, TItem value, IChangeToken expirationToken) where TKey : notnull { using ICacheEntry <TKey> entry = cache.CreateEntry(key); entry.AddExpirationToken(expirationToken); entry.Value = value; return(value); }
private static User PrepareUserResult(ICacheEntry cacheEntry, AutoRestClients.PlatformModuleApi.Models.ApplicationUserExtended userDto) { if (userDto != null) { cacheEntry.SetAbsoluteExpiration(TimeSpan.FromMinutes(1)); cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeToken(userDto.Id)); return(userDto.ToUser()); } return(null); }
private async Task <T?> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent) { var fileProvider = _hostingEnvironment.WebRootFileProvider; var changeToken = fileProvider.Watch(path); entry.SetPriority(CacheItemPriority.NeverRemove); entry.AddExpirationToken(changeToken); var file = fileProvider.GetFileInfo(path); if ((file is not { Exists : true }))
private async Task <T?> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent) where T : class { var fileProvider = _webHostEnvironment.WebRootFileProvider; var changeToken = fileProvider.Watch(path); entry.SetPriority(CacheItemPriority.NeverRemove); entry.AddExpirationToken(changeToken); var file = fileProvider.GetFileInfo(path); if (file == null || !file.Exists) { return(default);
private async Task <string?> Get(ICacheEntry entry, string path) { IFileProvider fileProvider = HostingEnvironment.WebRootFileProvider; IChangeToken changeToken = fileProvider.Watch(path); entry.SetPriority(CacheItemPriority.NeverRemove); entry.AddExpirationToken(changeToken); IFileInfo file = fileProvider.GetFileInfo(path); if (!file.Exists) { return(null); } return(await ReadFileContent(file)); }
public void NestedLinkContextsCanAggregate() { var clock = new TestClock(); var cache = CreateCache(clock); var obj = new object(); string key1 = "myKey1"; string key2 = "myKey2"; var expirationToken1 = new TestExpirationToken() { ActiveChangeCallbacks = true }; var expirationToken2 = new TestExpirationToken() { ActiveChangeCallbacks = true }; ICacheEntry entry1 = null; ICacheEntry entry2 = null; using (entry1 = cache.CreateEntry(key1)) { entry1.SetValue(obj); entry1 .AddExpirationToken(expirationToken1) .SetAbsoluteExpiration(TimeSpan.FromSeconds(10)); using (entry2 = cache.CreateEntry(key2)) { entry2.SetValue(obj); entry2 .AddExpirationToken(expirationToken2) .SetAbsoluteExpiration(TimeSpan.FromSeconds(15)); } } Assert.Equal(2, ((CacheEntry)entry1)._expirationTokens.Count()); Assert.NotNull(((CacheEntry)entry1)._absoluteExpiration); Assert.Equal(clock.UtcNow + TimeSpan.FromSeconds(10), ((CacheEntry)entry1)._absoluteExpiration); Assert.Single(((CacheEntry)entry2)._expirationTokens); Assert.NotNull(((CacheEntry)entry2)._absoluteExpiration); Assert.Equal(clock.UtcNow + TimeSpan.FromSeconds(15), ((CacheEntry)entry2)._absoluteExpiration); }
private async Task <T> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent) { var urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext); path = urlHelper.Content(path); var fileProvider = _hostingEnvironment.WebRootFileProvider; var changeToken = fileProvider.Watch(path); entry.SetPriority(CacheItemPriority.NeverRemove); entry.AddExpirationToken(changeToken); var file = fileProvider.GetFileInfo(path); if (file == null || !file.Exists) { return(default(T)); } return(await getContent(file)); }
/// <summary> /// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry. /// </summary> /// <param name="entry">The <see cref="ICacheEntry"/>.</param> /// <param name="options">Set the values of these options on the <paramref name="entry"/>.</param> /// <returns>The <see cref="ICacheEntry"/> for chaining.</returns> public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions options) { ThrowHelper.ThrowIfNull(options); entry.AbsoluteExpiration = options.AbsoluteExpiration; entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; entry.SlidingExpiration = options.SlidingExpiration; entry.Priority = options.Priority; entry.Size = options.Size; foreach (IChangeToken expirationToken in options.ExpirationTokens) { entry.AddExpirationToken(expirationToken); } foreach (PostEvictionCallbackRegistration postEvictionCallback in options.PostEvictionCallbacks) { entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback !, postEvictionCallback.State); } return(entry); }
/// <summary> /// Adds an expiration token, stored in the web context, to the cache entry. /// </summary> public static ICacheEntry AddExpirationToken(this ICacheEntry cacheEntry, IWebContext webContext) { cacheEntry.AddExpirationToken(new CancellationChangeToken(webContext.CacheExpirationTokenSource.Token)); return(cacheEntry); }
/// <summary> /// Sets the default cache entry options. /// </summary> public static ICacheEntry SetDefaultOptions(this ICacheEntry cacheEntry, IWebContext webContext) { cacheEntry.SetSlidingExpiration(CacheExpiration); cacheEntry.AddExpirationToken(webContext); return(cacheEntry); }
private void AddEvictionToken(ICacheEntry entry) { var cts = GetEvictionSource(true); entry.AddExpirationToken(new CancellationChangeToken(cts.Token)); }