Last active
August 29, 2015 14:13
-
-
Save rcurlette/235e1294a9d3ca041e1a to your computer and use it in GitHub Desktop.
Tridion Event System app.config helpers
This file contains 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
using System; | |
using System.Configuration; | |
using System.Reflection; | |
using System.Runtime.Caching; | |
namespace Tridion.EventSystem | |
{ | |
/// <summary> | |
/// Configuration file should be named the same as the Event System DLL with '.config' at the end. | |
/// ie. TridionEventSystem.dll.config | |
/// The location should be the same folder as where the .dll file is. | |
/// | |
/// Jan 8,2015 | |
/// Original code from: https://code.google.com/p/yet-another-tridion-blog/source/browse/trunk/Yet+Another+Event+System/Com.Mitza.YAES/ConfigurationManager.cs | |
/// Code-haircut provided by R. Curlette | |
/// </summary> | |
public static class ConfigurationManagerEventSystem | |
{ | |
private const double CACHE_TIME_IN_MINUTES = 6000; | |
private static ObjectCache Cache | |
{ | |
get | |
{ | |
return MemoryCache.Default; | |
} | |
} | |
/// <summary> | |
/// Returns the configuration value corresponding to the given key from Configuration corresponding to the current executing DLL AppSettings section | |
/// </summary> | |
public static string GetAppSetting(string key) | |
{ | |
try | |
{ | |
if (!Cache.Contains(key)) | |
{ | |
KeyValueConfigurationElement configElement = DllConfiguration.AppSettings.Settings[key]; | |
if (configElement != null) | |
{ | |
string value = configElement.Value; | |
if (!string.IsNullOrEmpty(value)) | |
{ | |
StoreInCache(key, value); | |
return value; | |
} | |
} | |
} | |
else // get from cache | |
{ | |
return Cache[key].ToString(); | |
} | |
} catch(Exception ex) | |
{ | |
// do nothing | |
} | |
return string.Empty; | |
} | |
/// <summary> | |
/// Returns the Configuration object next to the current executing DLL | |
/// </summary> | |
private static Configuration _dllConfiguration = null; | |
private static Configuration DllConfiguration | |
{ | |
get | |
{ | |
if (_dllConfiguration == null) | |
{ | |
try | |
{ | |
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() | |
{ | |
ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config" | |
}; | |
_dllConfiguration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); | |
} catch(Exception ex) | |
{ | |
// do nothing | |
} | |
} | |
return _dllConfiguration; | |
} | |
} | |
/// <summary> | |
/// Store any object in the cache | |
/// </summary> | |
/// <param name="key">Identification of the item</param> | |
/// <param name="item">The object to store (can be a page, component, schema, etc) </param> | |
public static void StoreInCache(string key, object item) | |
{ | |
CacheItemPolicy policy = new CacheItemPolicy(); | |
policy.AbsoluteExpiration = DateTime.Now.AddMinutes(CACHE_TIME_IN_MINUTES); | |
policy.Priority = CacheItemPriority.Default; | |
Cache.Add(key, item, policy); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment