Created
May 19, 2011 14:46
-
-
Save takeshik/980914 to your computer and use it in GitHub Desktop.
[PATCH] Solar: Icon File Cache Mechanism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- S:/solar/Solar/Library/UriBitmapConverter.cs Thu May 19 23:44:03 2011 | |
| +++ S:/solar/Solar/Library/UriBitmapConverter.cs Thu May 19 23:45:04 2011 | |
| @@ -1,5 +1,6 @@ | |
| using System; | |
| using System.Collections.Concurrent; | |
| +using System.IO; | |
| using System.Linq; | |
| using System.Net; | |
| using System.Windows.Media; | |
| @@ -24,6 +25,11 @@ | |
| } | |
| } | |
| + static UriBitmapConverter() | |
| + { | |
| + Directory.CreateDirectory(".imageCache"); | |
| + } | |
| + | |
| public static void Clean() | |
| { | |
| var items = images.Where(_ => _.Value.LastReference < DateTime.Now - TimeSpan.FromHours(1)); | |
| @@ -48,17 +54,28 @@ | |
| try | |
| { | |
| + MemoryStream ms; | |
| lock (value) | |
| - using (var wc = new WebClient | |
| + { | |
| + if (File.Exists(GetCachePath(value))) | |
| { | |
| - Headers = | |
| - { | |
| - { HttpRequestHeader.UserAgent, "Solar" }, | |
| - }, | |
| - }) | |
| - using (var ns = wc.OpenRead(value)) | |
| - using (var ms = ns.Freeze()) | |
| + ms = File.OpenRead(GetCachePath(value)).Freeze(); | |
| + } | |
| + else | |
| { | |
| + using (var wc = new WebClient | |
| + { | |
| + Headers = | |
| + { | |
| + { HttpRequestHeader.UserAgent, "Solar" }, | |
| + }, | |
| + }) | |
| + using (var ns = wc.OpenRead(value)) | |
| + ms = ns.Freeze(); | |
| + } | |
| + using (ms) | |
| + { | |
| + File.WriteAllBytes(GetCachePath(value), ms.ToArray()); | |
| var rt = new BitmapImage(); | |
| rt.BeginInit(); | |
| @@ -73,6 +90,7 @@ | |
| return images.AddOrUpdate(value, new CacheValue(rt), (_, oldValue) => new CacheValue(rt)).Value; | |
| } | |
| + } | |
| } | |
| catch (Exception ex) | |
| { | |
| @@ -80,6 +98,11 @@ | |
| return null; | |
| } | |
| + } | |
| + | |
| + private static String GetCachePath(Uri uri) | |
| + { | |
| + return @".imageCache\" + String.Concat(uri.Segments.Skip(2)).Replace('/', '.'); | |
| } | |
| class CacheValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment