Browse Source

[CacheControl] Implemented file cache

feature/package-cache-control
Robin Thoni 9 years ago
parent
commit
b230c4ad90

+ 3
- 2
CacheControl/Business/Attributes/CacheControlAttribute.cs View File

99
 
99
 
100
             var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, _responseMediaType, CacheType, ExcludeQueryStringFromCacheKey);
100
             var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, _responseMediaType, CacheType, ExcludeQueryStringFromCacheKey);
101
 
101
 
102
-            var data = _webCache.Get(cachekey) as CacheDbo;
103
-            if (data == null) return;
102
+            var data = _webCache.Get<CacheDbo>(cachekey);
103
+            if (data == null)
104
+                return;
104
 
105
 
105
             if (!IsValid(data))
106
             if (!IsValid(data))
106
             {
107
             {

+ 26
- 18
CacheControl/Business/OutputCache/FileOuputCache.cs View File

1
-using System;
2
-using System.Collections.Generic;
1
+using System.IO;
2
+using System.Runtime.Serialization.Formatters.Binary;
3
+using System.Web;
3
 
4
 
4
 namespace iiie.CacheControl.Business.OutputCache
5
 namespace iiie.CacheControl.Business.OutputCache
5
 {
6
 {
6
     public class FileOuputCache : IOutputCache
7
     public class FileOuputCache : IOutputCache
7
     {
8
     {
8
-        public void RemoveStartsWith(string key)
9
-        {
10
-            throw new NotImplementedException();
11
-        }
9
+        public string TempPath { get; set; }
12
 
10
 
13
-        public T Get<T>(string key) where T : class
11
+        public string GetPath(string key)
14
         {
12
         {
15
-            throw new NotImplementedException();
13
+            key = HttpUtility.UrlEncode(key);
14
+            return Path.Combine(TempPath, key);
16
         }
15
         }
17
 
16
 
18
-        public object Get(string key)
17
+        public override object Get(string key)
19
         {
18
         {
20
-            throw new NotImplementedException();
19
+            key = GetPath(key);
20
+            var formatter = new BinaryFormatter();
21
+            var stream = new FileStream(key, FileMode.Open);
22
+            var obj = formatter.Deserialize(stream);
23
+            stream.Close();
24
+            return obj;
21
         }
25
         }
22
 
26
 
23
-        public void Remove(string key)
27
+        public override void Remove(string key)
24
         {
28
         {
25
-            throw new NotImplementedException();
29
+            key = GetPath(key);
30
+            File.Delete(key);
26
         }
31
         }
27
 
32
 
28
-        public bool Contains(string key)
33
+        public override bool Contains(string key)
29
         {
34
         {
30
-            throw new NotImplementedException();
35
+            key = GetPath(key);
36
+            return File.Exists(key);
31
         }
37
         }
32
 
38
 
33
-        public void Add(string key, object o, string dependsOnKey = null)
39
+        public override void Add(string key, object o)
34
         {
40
         {
35
-            throw new NotImplementedException();
41
+            key = GetPath(key);
42
+            var formatter = new BinaryFormatter();
43
+            var stream = new FileStream(key, FileMode.Open);
44
+            formatter.Serialize(stream, o);
45
+            stream.Close();
36
         }
46
         }
37
-
38
-        public IEnumerable<string> AllKeys { get; private set; }
39
     }
47
     }
40
 }
48
 }

+ 15
- 10
CacheControl/Business/OutputCache/IOutputCache.cs View File

1
-using System.Collections.Generic;
2
-
1
+
3
 namespace iiie.CacheControl.Business.OutputCache
2
 namespace iiie.CacheControl.Business.OutputCache
4
 {
3
 {
5
-    public interface IOutputCache
4
+    public abstract class IOutputCache
6
     {
5
     {
7
-        void RemoveStartsWith(string key);
8
-        T Get<T>(string key) where T : class;
9
-        object Get(string key);
10
-        void Remove(string key);
11
-        bool Contains(string key);
12
-        void Add(string key, object o, string dependsOnKey = null);
13
-        IEnumerable<string> AllKeys { get; }
6
+        public virtual T Get<T>(string key) where T : class
7
+        {
8
+            var o = Get(key) as T;
9
+            return o;
10
+        }
11
+
12
+        public abstract object Get(string key);
13
+
14
+        public abstract void Remove(string key);
15
+
16
+        public abstract bool Contains(string key);
17
+
18
+        public abstract void Add(string key, object o);
14
     }
19
     }
15
 }
20
 }

+ 6
- 38
CacheControl/Business/OutputCache/MemoryOutputCache.cs View File

1
-using System.Collections.Generic;
2
-using System.Linq;
3
-using System.Runtime.Caching;
1
+using System.Runtime.Caching;
4
 
2
 
5
 namespace iiie.CacheControl.Business.OutputCache
3
 namespace iiie.CacheControl.Business.OutputCache
6
 {
4
 {
8
     {
6
     {
9
         private static readonly MemoryCache Cache = MemoryCache.Default;
7
         private static readonly MemoryCache Cache = MemoryCache.Default;
10
 
8
 
11
-        public void RemoveStartsWith(string key)
12
-        {
13
-            lock (Cache)
14
-            {
15
-                Cache.Remove(key);
16
-            }
17
-        }
18
-
19
-        public T Get<T>(string key) where T : class
20
-        {
21
-            var o = Cache.Get(key) as T;
22
-            return o;
23
-        }
24
-
25
-        public object Get(string key)
9
+        public override object Get(string key)
26
         {
10
         {
27
             return Cache.Get(key);
11
             return Cache.Get(key);
28
         }
12
         }
29
 
13
 
30
-        public void Remove(string key)
14
+        public override void Remove(string key)
31
         {
15
         {
32
             lock (Cache)
16
             lock (Cache)
33
             {
17
             {
35
             }
19
             }
36
         }
20
         }
37
 
21
 
38
-        public bool Contains(string key)
22
+        public override bool Contains(string key)
39
         {
23
         {
40
             return Cache.Contains(key);
24
             return Cache.Contains(key);
41
         }
25
         }
42
 
26
 
43
-        public void Add(string key, object o, string dependsOnKey = null)
27
+        public override void Add(string key, object o)
44
         {
28
         {
45
-            var cachePolicy = new CacheItemPolicy();
46
-
47
-            if (!string.IsNullOrWhiteSpace(dependsOnKey))
48
-            {
49
-                cachePolicy.ChangeMonitors.Add(
50
-                    Cache.CreateCacheEntryChangeMonitor(new[] { dependsOnKey })
51
-                );
52
-            }
53
             lock (Cache)
29
             lock (Cache)
54
             {
30
             {
55
-                Cache.Add(key, o, cachePolicy);
56
-            }
57
-        }
58
-
59
-        public IEnumerable<string> AllKeys
60
-        {
61
-            get
62
-            {
63
-                return Cache.Select(x => x.Key);
31
+                Cache.Add(key, o, new CacheItemPolicy());
64
             }
32
             }
65
         }
33
         }
66
     }
34
     }

+ 1
- 0
CacheControl/CacheControl.csproj View File

42
       <HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
42
       <HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
43
     </Reference>
43
     </Reference>
44
     <Reference Include="System.Runtime.Caching" />
44
     <Reference Include="System.Runtime.Caching" />
45
+    <Reference Include="System.Web" />
45
     <Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46
     <Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46
       <SpecificVersion>False</SpecificVersion>
47
       <SpecificVersion>False</SpecificVersion>
47
       <HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
48
       <HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>

Loading…
Cancel
Save