动态地从文件中读取资源 [英] Dynamically reading resources from a file
问题描述
我才能有一个集中的地方改变他们一直在使用静态字符串RESX文件。问题是,我不能改变他们项目的生成和部署之后。
有一些字符串,我想部署后更改,无需重新启动的过程(以.config文件都出来了)。
这是可以写code,它解析一个配置文件(XML / JSON / YAML /?)有效,例如缓存结果X秒或监视它与变化FileSystemWatcher的,但这样的事情已经做了什么?
编辑:使用 Json.NET 并Rashmi潘迪特的指针的CacheDependency
我写了这个JSON解析类,直到该文件被更改缓存解析的结果:
公共类CachingJsonParser
{
公共静态CachingJsonParser的Create()
{
返回新CachingJsonParser(
HttpContext.Current.Server,
HttpContext.Current.Cache);
} 私人只读HttpServerUtility _SERVER;
私人只读缓存_cache; 公共CachingJsonParser(HttpServerUtility服务器,缓存缓存)
{
_ SERVER =服务器;
_cache =高速缓存;
} 公共ŧ解析< T>(串relativePath)
{
变种cacheKey =cached_json_file:+ relativePath;
如果(_cache [cacheKey] == NULL)
{
VAR mappedPath = _server.MapPath(relativePath);
VAR JSON = File.ReadAllText(mappedPath);
VAR的结果= JavaScriptConvert.DeserializeObject(JSON的typeof(T));
_cache.Insert(cacheKey,因此,新的CacheDependency(mappedPath));
}
返回(T)_cache [cacheKey]
}
}
用法
JSON文件:
{
用户名:富,
密码:QWERTY
}
相应的数据类:
类LoginData
{
公共字符串用户名{获得;组; }
公共字符串密码{搞定;组; }
}
解析和缓存:
VAR解析器= CachingJsonParser.Create();
VAR数据= parser.Parse< LoginData>(〜/ App_Data文件/ login_data.json);
您可以使用XML文件,并将其存储在缓存中。您可以使用的CacheDependency重新加载缓存时的任何变化都对文件进行的。
链接:
的CacheDependency :的 CacheItemUpdateCallback :
在你的情况你应该缓存存储一个XmlDocument作为值
编辑:
这是我的样本code
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
XmlDocument的permissionsDoc = NULL; 如果(高速缓存[权限] == NULL)
{
字符串路径=使用Server.Mappath(〜/ XML / Permissions.xml);
permissionsDoc =新的XmlDocument();
permissionsDoc.Load(使用Server.Mappath(〜/ XML / Permissions.xml));
Cache.Add(权限,permissionsDoc,
新的CacheDependency(使用Server.Mappath(〜/ XML / Permissions.xml)),
Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,
CacheItemPriority.Default,新CacheItemRemovedCallback(ReloadPermissionsCallBack));
}
其他
{
permissionsDoc =(XmlDocument的)高速缓存[权限];
}
}私人无效ReloadPermissionsCallBack(字符串键,对象的值,CacheItemRemovedReason原因)
{
XmlDocument的DOC =新的XmlDocument();
doc.Load(使用Server.Mappath(〜/ XML / Permissions.xml));
Cache.Insert(权限,DOC,
新的CacheDependency(使用Server.Mappath(〜/ XML / Permissions.xml)),
Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,
CacheItemPriority.Default,新CacheItemRemovedCallback(ReloadPermissionsCallBack));
}
I've been using resx files for static strings in order to have a central place for changing them. The problem is that I can't change them after the project is built and deployed.
There are some strings that I would like to change after deployment, without restarting the process (so .config files are out).
It's possible to write code that parses a config file (XML/JSON/YAML/?) efficiently, e.g. caches the result for X seconds or monitors it for changes with FileSystemWatcher, but has something like this already been done?
EDIT: using Json.NET and Rashmi Pandit's pointer to CacheDependency
I wrote this JSON parsing class that caches the parsed results until the file is changed:
public class CachingJsonParser
{
public static CachingJsonParser Create()
{
return new CachingJsonParser(
HttpContext.Current.Server,
HttpContext.Current.Cache);
}
private readonly HttpServerUtility _server;
private readonly Cache _cache;
public CachingJsonParser(HttpServerUtility server, Cache cache)
{
_server = server;
_cache = cache;
}
public T Parse<T>(string relativePath)
{
var cacheKey = "cached_json_file:" + relativePath;
if (_cache[cacheKey] == null)
{
var mappedPath = _server.MapPath(relativePath);
var json = File.ReadAllText(mappedPath);
var result = JavaScriptConvert.DeserializeObject(json, typeof(T));
_cache.Insert(cacheKey, result, new CacheDependency(mappedPath));
}
return (T)_cache[cacheKey];
}
}
Usage
JSON file:
{
"UserName": "foo",
"Password": "qwerty"
}
Corresponding data class:
class LoginData
{
public string UserName { get; set; }
public string Password { get; set; }
}
Parsing and caching:
var parser = CachingJsonParser.Create();
var data = parser.Parse<LoginData>("~/App_Data/login_data.json");
You can use xml files and store it in the Cache. You can use CacheDependency to reload the cache when any change is made to the file. Links: CacheDependency: CacheItemUpdateCallback :
In your case your Cache should store an XmlDocument as value
Edit: This is my sample code
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument permissionsDoc = null;
if (Cache["Permissions"] == null)
{
string path = Server.MapPath("~/XML/Permissions.xml");
permissionsDoc = new XmlDocument();
permissionsDoc.Load(Server.MapPath("~/XML/Permissions.xml"));
Cache.Add("Permissions", permissionsDoc,
new CacheDependency(Server.MapPath("~/XML/Permissions.xml")),
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, new CacheItemRemovedCallback(ReloadPermissionsCallBack));
}
else
{
permissionsDoc = (XmlDocument)Cache["Permissions"];
}
}
private void ReloadPermissionsCallBack(string key, object value, CacheItemRemovedReason reason)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/XML/Permissions.xml"));
Cache.Insert("Permissions", doc ,
new CacheDependency(Server.MapPath("~/XML/Permissions.xml")),
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, new CacheItemRemovedCallback(ReloadPermissionsCallBack));
}
这篇关于动态地从文件中读取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!