-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathMemoryFileCacheCore.cs
92 lines (84 loc) · 2.83 KB
/
MemoryFileCacheCore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2016-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#if NETSTANDARD1_6
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
namespace React.AspNet
{
/// <summary>
/// Memory cache implementation for React.ICache. Uses IMemoryCache from .NET Core.
/// </summary>
public class MemoryFileCacheCore : ICache
{
private readonly IMemoryCache _cache;
private readonly IHostingEnvironment _hostingEnv;
/// <summary>
/// Initializes a new instance of the <see cref="MemoryFileCacheCore" /> class.
/// </summary>
/// <param name="cache">The cache to use</param>
/// <param name="hostingEnv">The ASP.NET hosting environment.</param>
public MemoryFileCacheCore(IMemoryCache cache, IHostingEnvironment hostingEnv)
{
_cache = cache;
_hostingEnv = hostingEnv;
}
/// <summary>
/// Get an item from the cache. Returns <paramref name="fallback"/> if the item does
/// not exist.
/// </summary>
/// <typeparam name="T">Type of data</typeparam>
/// <param name="key">The cache key</param>
/// <param name="fallback">Value to return if item is not in the cache</param>
/// <returns>Data from cache, otherwise <paramref name="fallback"/></returns>
public T Get<T>(string key, T fallback = default(T))
{
return (T)(_cache.Get(key) ?? fallback);
}
/// <summary>
/// Sets an item in the cache.
/// </summary>
/// <typeparam name="T">Type of data</typeparam>
/// <param name="key">The cache key</param>
/// <param name="data">Data to cache</param>
/// <param name="slidingExpiration">
/// Sliding expiration, if cache key is not accessed in this time period it will
/// automatically be removed from the cache
/// </param>
/// <param name="cacheDependencyFiles">
/// Filenames this cached item is dependent on. If any of these files change, the cache
/// will be cleared automatically
/// </param>
public void Set<T>(string key, T data, TimeSpan slidingExpiration, IEnumerable<string> cacheDependencyFiles = null)
{
if (data == null)
{
_cache.Remove(key);
return;
}
var options = new MemoryCacheEntryOptions
{
SlidingExpiration = slidingExpiration,
};
if (cacheDependencyFiles != null)
{
foreach (var file in cacheDependencyFiles)
{
var relativePath = file.Replace(_hostingEnv.WebRootPath, string.Empty).TrimStart('\\', '/');
options.AddExpirationToken(_hostingEnv.WebRootFileProvider.Watch(relativePath));
}
}
_cache.Set(key, data, options);
}
}
}
#endif