This repository was archived by the owner on May 2, 2018. It is now read-only.
forked from sitdap/dynamic-image
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDynamicImageCacheManager.cs
More file actions
137 lines (123 loc) · 3.95 KB
/
DynamicImageCacheManager.cs
File metadata and controls
137 lines (123 loc) · 3.95 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using SoundInTheory.DynamicImage.Configuration;
using System.Web.Configuration;
using System.Collections.Generic;
using SoundInTheory.DynamicImage.Sources;
namespace SoundInTheory.DynamicImage.Caching
{
public static class DynamicImageCacheManager
{
private static DynamicImageCacheProvider _provider;
private static bool? _storeMissingImagesInCache;
private static DynamicImageSection _configSection;
private static DynamicImageSection ConfigSection
{
get
{
if (_configSection == null)
_configSection = (DynamicImageSection)ConfigurationManager.GetSection("soundInTheory/dynamicImage");
return _configSection;
}
}
private static DynamicImageCacheProvider Provider
{
get
{
if (_provider == null)
{
DynamicImageSection config = ConfigSection;
if (config == null)
{
// Default to XML cache provider.
_provider = new XmlCacheProvider();
_provider.Initialize("XmlCacheProvider", new NameValueCollection());
}
else
{
switch (config.Caching.Mode)
{
case DynamicImageCachingMode.Off:
goto default;
case DynamicImageCachingMode.InProc:
_provider = new InProcDynamicImageCacheProvider();
break;
case DynamicImageCachingMode.Custom:
string customProvider = config.Caching.CustomProvider;
PropertyInformation propertyInformation = config.ElementInformation.Properties["customProvider"];
if (string.IsNullOrEmpty(customProvider))
throw new ConfigurationErrorsException(
"Invalid DynamicImage caching custom provider: '" + customProvider + "'",
propertyInformation.Source,
propertyInformation.LineNumber);
ProviderSettings providerSettings = config.Caching.Providers[customProvider];
if (providerSettings == null)
throw new ConfigurationErrorsException(
"Missing DynamicImage caching custom provider: '" + customProvider + "'",
propertyInformation.Source,
propertyInformation.LineNumber);
_provider = (DynamicImageCacheProvider) ProvidersHelper.InstantiateProvider(
providerSettings, typeof (DynamicImageCacheProvider));
break;
default:
_provider = new TransientCacheProvider();
break;
}
}
}
return _provider;
}
}
internal static bool StoreMissingImagesInCache
{
get
{
if (_storeMissingImagesInCache == null)
{
DynamicImageSection config = ConfigSection;
if (config == null)
_storeMissingImagesInCache = false;
else
_storeMissingImagesInCache = config.Caching.StoreMissingImagesInCache;
}
return _storeMissingImagesInCache.Value;
}
}
internal static bool Exists(string cacheKey)
{
return Provider.ExistsInCache(cacheKey);
}
internal static void Add(string cacheKey, GeneratedImage generatedImage, Dependency[] dependencies)
{
Provider.AddToCache(cacheKey, generatedImage, dependencies);
}
internal static DateTime GetImageLastModifiedDate(HttpContext context, string cacheProviderKey, string fileExtension)
{
return Provider.GetImageLastModifiedDate(context, cacheProviderKey, fileExtension);
}
internal static ImageProperties GetProperties(string cacheKey)
{
return Provider.GetPropertiesFromCache(cacheKey);
}
public static void Remove(ImageSource source)
{
var dependencies = new List<Dependency>();
source.PopulateDependencies(dependencies);
dependencies.ForEach(RemoveByDependency);
}
public static void RemoveAll()
{
Provider.RemoveAllFromCache();
}
private static void RemoveByDependency(Dependency dependency)
{
Provider.RemoveFromCache(dependency);
}
internal static void SendImageToHttpResponse(HttpContext context, string cacheProviderKey, string fileExtension)
{
Provider.SendImageToHttpResponse(context, cacheProviderKey, fileExtension);
}
}
}