|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-Present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#if NETSTANDARD1_6 |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using Microsoft.Extensions.Caching.Memory; |
| 14 | +using System.Linq; |
| 15 | +using Microsoft.Extensions.FileProviders; |
| 16 | + |
| 17 | +namespace React.AspNet |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Memory cache implementation for React.ICache. Uses IMemoryCache from .NET Core. |
| 21 | + /// </summary> |
| 22 | + public class MemoryFileCacheCore : ICache |
| 23 | + { |
| 24 | + private readonly IMemoryCache _cache; |
| 25 | + private readonly IFileProvider _fileProvider; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new instance of the <see cref="MemoryFileCacheCore" /> class. |
| 29 | + /// </summary> |
| 30 | + /// <param name="cache">The cache to use</param> |
| 31 | + /// <param name="fileProvider">The file provider.</param> |
| 32 | + public MemoryFileCacheCore(IMemoryCache cache, IFileProvider fileProvider) |
| 33 | + { |
| 34 | + _cache = cache; |
| 35 | + _fileProvider = fileProvider; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Get an item from the cache. Returns <paramref name="fallback"/> if the item does |
| 40 | + /// not exist. |
| 41 | + /// </summary> |
| 42 | + /// <typeparam name="T">Type of data</typeparam> |
| 43 | + /// <param name="key">The cache key</param> |
| 44 | + /// <param name="fallback">Value to return if item is not in the cache</param> |
| 45 | + /// <returns>Data from cache, otherwise <paramref name="fallback"/></returns> |
| 46 | + public T Get<T>(string key, T fallback = default(T)) |
| 47 | + { |
| 48 | + return (T)(_cache.Get(key) ?? fallback); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Sets an item in the cache. |
| 53 | + /// </summary> |
| 54 | + /// <typeparam name="T">Type of data</typeparam> |
| 55 | + /// <param name="key">The cache key</param> |
| 56 | + /// <param name="data">Data to cache</param> |
| 57 | + /// <param name="slidingExpiration"> |
| 58 | + /// Sliding expiration, if cache key is not accessed in this time period it will |
| 59 | + /// automatically be removed from the cache |
| 60 | + /// </param> |
| 61 | + /// <param name="cacheDependencyFiles"> |
| 62 | + /// Filenames this cached item is dependent on. If any of these files change, the cache |
| 63 | + /// will be cleared automatically |
| 64 | + /// </param> |
| 65 | + /// <param name="cacheDependencyKeys"> |
| 66 | + /// Other cache keys this cached item is dependent on. If any of these keys change, the |
| 67 | + /// cache will be cleared automatically |
| 68 | + /// </param> |
| 69 | + public void Set<T>(string key, T data, TimeSpan slidingExpiration, IEnumerable<string> cacheDependencyFiles = null, IEnumerable<string> cacheDependencyKeys = null) |
| 70 | + { |
| 71 | + if (data == null) |
| 72 | + { |
| 73 | + _cache.Remove(key); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var options = new MemoryCacheEntryOptions |
| 78 | + { |
| 79 | + SlidingExpiration = slidingExpiration, |
| 80 | + }; |
| 81 | + |
| 82 | + if (cacheDependencyFiles != null) |
| 83 | + { |
| 84 | + foreach (var file in cacheDependencyFiles) |
| 85 | + { |
| 86 | + options.AddExpirationToken(_fileProvider.Watch(file)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (cacheDependencyKeys != null && cacheDependencyKeys.Any()) |
| 91 | + { |
| 92 | + // https://github.com/aspnet/Docs/issues/1938 |
| 93 | + throw new NotImplementedException(); |
| 94 | + } |
| 95 | + |
| 96 | + _cache.Set(key, data, options); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +#endif |
0 commit comments