1+ /*
2+ * Copyright (c) 2014-2015, 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+ using System ;
11+ using System . Collections . Generic ;
12+ using System . Linq ;
13+ using System . Runtime . Caching ;
14+
15+ namespace React
16+ {
17+ /// <summary>
18+ /// Memory cache implementation for React.ICache. Uses System.Runtime.Caching.
19+ /// </summary>
20+ public class MemoryFileCache : ICache
21+ {
22+ private readonly ObjectCache _cache ;
23+
24+ /// <summary>
25+ /// Initializes a new instance of the <see cref="MemoryFileCache"/> class.
26+ /// </summary>
27+ public MemoryFileCache ( )
28+ {
29+ _cache = MemoryCache . Default ;
30+ }
31+
32+ /// <summary>
33+ /// Get an item from the cache. Returns <paramref name="fallback"/> if the item does
34+ /// not exist.
35+ /// </summary>
36+ /// <typeparam name="T">Type of data</typeparam>
37+ /// <param name="key">The cache key</param>
38+ /// <param name="fallback">Value to return if item is not in the cache</param>
39+ /// <returns>Data from cache, otherwise <paramref name="fallback"/></returns>
40+ public T Get < T > ( string key , T fallback = default ( T ) )
41+ {
42+ return ( T ) ( _cache . Get ( key ) ?? fallback ) ;
43+ }
44+
45+ /// <summary>
46+ /// Sets an item in the cache.
47+ /// </summary>
48+ /// <typeparam name="T">Type of data</typeparam>
49+ /// <param name="key">The cache key</param>
50+ /// <param name="data">Data to cache</param>
51+ /// <param name="slidingExpiration">
52+ /// Sliding expiration, if cache key is not accessed in this time period it will
53+ /// automatically be removed from the cache
54+ /// </param>
55+ /// <param name="cacheDependencyFiles">
56+ /// Filenames this cached item is dependent on. If any of these files change, the cache
57+ /// will be cleared automatically
58+ /// </param>
59+ /// <param name="cacheDependencyKeys">
60+ /// Other cache keys this cached item is dependent on. If any of these keys change, the
61+ /// cache will be cleared automatically
62+ /// </param>
63+ public void Set < T > ( string key , T data , TimeSpan slidingExpiration , IEnumerable < string > cacheDependencyFiles = null , IEnumerable < string > cacheDependencyKeys = null )
64+ {
65+ if ( data == null )
66+ {
67+ _cache . Remove ( key ) ;
68+ return ;
69+ }
70+
71+ var policy = new CacheItemPolicy { SlidingExpiration = slidingExpiration } ;
72+
73+ if ( cacheDependencyFiles != null && cacheDependencyFiles . Any ( ) )
74+ policy . ChangeMonitors . Add ( new HostFileChangeMonitor ( cacheDependencyFiles . ToList ( ) ) ) ;
75+
76+ if ( cacheDependencyKeys != null && cacheDependencyKeys . Any ( ) )
77+ policy . ChangeMonitors . Add ( _cache . CreateCacheEntryChangeMonitor ( cacheDependencyKeys ) ) ;
78+
79+ _cache . Set ( key , data , policy ) ;
80+ }
81+ }
82+ }
0 commit comments