|
1 | | -using System; |
2 | | -using System.Runtime.Caching; |
3 | | -using Microsoft.ApplicationServer.Caching; |
4 | | -using SharpRepository.Repository.Caching; |
5 | | - |
6 | | -// Reference: http://stackoverflow.com/questions/4739548/appfabric-caching-examples-using-c-sharp |
7 | | - |
8 | | -namespace SharpRepository.Caching.AppFabric |
9 | | -{ |
10 | | - /// <summary> |
11 | | - /// Uses AppFabric Caching as the caching provider. <a href="http://msdn.microsoft.com/en-us/library/ff383731(v=azure.10).aspx">Get more info on AppFabric Caching</a> |
12 | | - /// </summary> |
13 | | - public class AppFabricCachingProvider : ICachingProvider |
14 | | - { |
15 | | - protected DataCacheFactory CacheFactory { get; set;} |
16 | | - protected DataCache Cache { get; set; } |
17 | | - |
18 | | - private static readonly object LockObject = new object(); |
19 | | - |
20 | | - /// <summary> |
21 | | - /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
22 | | - /// </summary> |
23 | | - /// <param name="cacheName">Name of the cache.</param> |
24 | | - public AppFabricCachingProvider(string cacheName = null) |
25 | | - : this(new DataCacheFactory(), cacheName) |
26 | | - { |
27 | | - } |
28 | | - |
29 | | - /// <summary> |
30 | | - /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
31 | | - /// </summary> |
32 | | - /// <param name="configuration">The AbbFabric Caching configuration.</param> |
33 | | - /// <param name="cacheName">Name of the cache.</param> |
34 | | - public AppFabricCachingProvider(DataCacheFactoryConfiguration configuration, string cacheName = null) |
35 | | - : this(new DataCacheFactory(configuration), cacheName) |
36 | | - { |
37 | | - if (configuration == null) throw new ArgumentNullException("configuration"); |
38 | | - } |
39 | | - |
40 | | - /// <summary> |
41 | | - /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
42 | | - /// </summary> |
43 | | - /// <param name="cacheFactory">The cache factory.</param> |
44 | | - /// <param name="cacheName">Name of the cache.</param> |
45 | | - public AppFabricCachingProvider(DataCacheFactory cacheFactory, string cacheName = null) |
46 | | - { |
47 | | - if (cacheFactory == null) throw new ArgumentNullException("cacheFactory"); |
48 | | - |
49 | | - CacheFactory = cacheFactory; |
50 | | - |
51 | | - // TODO: don't know enough about AppFabric to know if we should use the GetDefaultCache() if no cache name provided, or if we should use our own name like SharpRepository |
52 | | - Cache = String.IsNullOrEmpty(cacheName) ? cacheFactory.GetDefaultCache() : cacheFactory.GetCache(cacheName); |
53 | | - } |
54 | | - |
55 | | - public void Set<T>(string key, T value, CacheItemPriority priority = CacheItemPriority.Default, int? cacheTime = null) |
56 | | - { |
57 | | - if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
58 | | - |
59 | | - if (!cacheTime.HasValue) |
60 | | - { |
61 | | - Cache.Put(key, value); |
62 | | - } |
63 | | - else |
64 | | - { |
65 | | - Cache.Put(key, value, TimeSpan.FromSeconds(cacheTime.Value)); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - public void Clear(string key) |
70 | | - { |
71 | | - if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
72 | | - |
73 | | - Cache.Remove(key); |
74 | | - } |
75 | | - |
76 | | - public bool Exists(string key) |
77 | | - { |
78 | | - if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
79 | | - |
80 | | - return Cache.Get(key) != null; |
81 | | - } |
82 | | - |
83 | | - public bool Get<T>(string key, out T value) |
84 | | - { |
85 | | - if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
86 | | - |
87 | | - value = default(T); |
88 | | - |
89 | | - try |
90 | | - { |
91 | | - var item = Cache.Get(key); |
92 | | - if (item == null) |
93 | | - return false; |
94 | | - |
95 | | - value = (T)item; |
96 | | - } |
97 | | - catch (Exception) |
98 | | - { |
99 | | - return false; |
100 | | - } |
101 | | - |
102 | | - return true; |
103 | | - } |
104 | | - |
105 | | - public int Increment(string key, int defaultValue, int value, CacheItemPriority priority = CacheItemPriority.Default) |
106 | | - { |
107 | | - if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
108 | | - |
109 | | - lock (LockObject) |
110 | | - { |
111 | | - int current; |
112 | | - if (!Get(key, out current)) |
113 | | - { |
114 | | - current = defaultValue; |
115 | | - } |
116 | | - |
117 | | - var newValue = current + value; |
118 | | - Set(key, newValue, priority); |
119 | | - return newValue; |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - public void Dispose() |
124 | | - { |
125 | | - Cache = null; |
126 | | - CacheFactory.Dispose(); |
127 | | - } |
128 | | - } |
129 | | -} |
| 1 | +using System; |
| 2 | +using System.Runtime.Caching; |
| 3 | +using Microsoft.ApplicationServer.Caching; |
| 4 | +using SharpRepository.Repository.Caching; |
| 5 | + |
| 6 | +// Reference: http://stackoverflow.com/questions/4739548/appfabric-caching-examples-using-c-sharp |
| 7 | + |
| 8 | +namespace SharpRepository.Caching.AppFabric |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Uses AppFabric Caching as the caching provider. <a href="http://msdn.microsoft.com/en-us/library/ff383731(v=azure.10).aspx">Get more info on AppFabric Caching</a> |
| 12 | + /// </summary> |
| 13 | + public class AppFabricCachingProvider : ICachingProvider |
| 14 | + { |
| 15 | + protected DataCacheFactory CacheFactory { get; set;} |
| 16 | + protected DataCache Cache { get; set; } |
| 17 | + |
| 18 | + private static readonly object LockObject = new object(); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
| 22 | + /// </summary> |
| 23 | + /// <param name="cacheName">Name of the cache.</param> |
| 24 | + public AppFabricCachingProvider(string cacheName = null) |
| 25 | + : this(new DataCacheFactory(), cacheName) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
| 31 | + /// </summary> |
| 32 | + /// <param name="configuration">The AbbFabric Caching configuration.</param> |
| 33 | + /// <param name="cacheName">Name of the cache.</param> |
| 34 | + public AppFabricCachingProvider(DataCacheFactoryConfiguration configuration, string cacheName = null) |
| 35 | + : this(new DataCacheFactory(configuration), cacheName) |
| 36 | + { |
| 37 | + if (configuration == null) throw new ArgumentNullException("configuration"); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initializes a new instance of the <see cref="AppFabricCachingProvider"/> class. |
| 42 | + /// </summary> |
| 43 | + /// <param name="cacheFactory">The cache factory.</param> |
| 44 | + /// <param name="cacheName">Name of the cache.</param> |
| 45 | + public AppFabricCachingProvider(DataCacheFactory cacheFactory, string cacheName = null) |
| 46 | + { |
| 47 | + if (cacheFactory == null) throw new ArgumentNullException("cacheFactory"); |
| 48 | + |
| 49 | + CacheFactory = cacheFactory; |
| 50 | + |
| 51 | + // TODO: don't know enough about AppFabric to know if we should use the GetDefaultCache() if no cache name provided, or if we should use our own name like SharpRepository |
| 52 | + Cache = String.IsNullOrEmpty(cacheName) ? cacheFactory.GetDefaultCache() : cacheFactory.GetCache(cacheName); |
| 53 | + } |
| 54 | + |
| 55 | + public void Set<T>(string key, T value, CacheItemPriority priority = CacheItemPriority.Default, int? cacheTime = null) |
| 56 | + { |
| 57 | + if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
| 58 | + |
| 59 | + if (!cacheTime.HasValue) |
| 60 | + { |
| 61 | + Cache.Put(key, value); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + Cache.Put(key, value, TimeSpan.FromSeconds(cacheTime.Value)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void Clear(string key) |
| 70 | + { |
| 71 | + if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
| 72 | + |
| 73 | + Cache.Remove(key); |
| 74 | + } |
| 75 | + |
| 76 | + public bool Exists(string key) |
| 77 | + { |
| 78 | + if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
| 79 | + |
| 80 | + return Cache.Get(key) != null; |
| 81 | + } |
| 82 | + |
| 83 | + public bool Get<T>(string key, out T value) |
| 84 | + { |
| 85 | + if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
| 86 | + |
| 87 | + value = default(T); |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + var item = Cache.Get(key); |
| 92 | + if (item == null) |
| 93 | + return false; |
| 94 | + |
| 95 | + value = (T)item; |
| 96 | + } |
| 97 | + catch (Exception) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + public int Increment(string key, int defaultValue, int value, CacheItemPriority priority = CacheItemPriority.Default) |
| 106 | + { |
| 107 | + if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); |
| 108 | + |
| 109 | + lock (LockObject) |
| 110 | + { |
| 111 | + int current; |
| 112 | + if (!Get(key, out current)) |
| 113 | + { |
| 114 | + current = defaultValue; |
| 115 | + } |
| 116 | + |
| 117 | + var newValue = current + value; |
| 118 | + Set(key, newValue, priority); |
| 119 | + return newValue; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public void Dispose() |
| 124 | + { |
| 125 | + Cache = null; |
| 126 | + CacheFactory.Dispose(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments