我最近问了一个关于在ASP.NET MVC WebAPI应用程序中缓存应用程序数据的问题,这引出了一个新问题.ASP.NET中可用的不同缓存方法的优缺点是什么?
我来了:
内存缓存
http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
使用静态成员变量:
private static Northwind.SuppliersDataTable suppliers = null;
Run Code Online (Sandbox Code Playgroud)申请状态:
HttpContext.Current.Application["key"] ="Value"
Run Code Online (Sandbox Code Playgroud)数据缓存:
HttpRuntime.Cache.Insert(
/* key */ "key",
/* value */ "value",
/* dependencies */ null,
/* absoluteExpiration */ Cache.NoAbsoluteExpiration,
/* slidingExpiration */ Cache.NoSlidingExpiration,
/* priority */ CacheItemPriority.NotRemovable,
/* onRemoveCallback */ null);
Run Code Online (Sandbox Code Playgroud)我确信还有其他的,我知道它们都在技术上将数据存储在内存中......所以我知道应该使用什么来构建ASP.NET MVC webapi?
我以前的问题: 在内存中缓存应用程序数据:MVC Web API
我正在编写一个MVC webAPI,它将用于返回将绑定到下拉框的值或在网站上用作提前输入文本框结果的值,我想在内存中缓存值,这样我就不需要执行数据库请求每次API被击中.
我将使用MemoryCache类,我知道我可以在第一个请求进入时填充缓存但我不希望API的第一个请求比其他请求慢.我的问题是:当WebAPI首次启动时,我有办法自动填充缓存吗?我看到有一个"App_Start"文件夹,也许我只是在这里扔东西?
在初始填充之后,我可能会按小时/每日运行请求以根据需要更新缓存.
MemoryCache:http: //msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
UDPATE
Ela在下面的回答就是这个伎俩,基本上我只需要看看Global.asax的功能.感谢您的快速帮助,这为我提出了一个单独的问题,关于不同缓存类型的优缺点.
我有以下型号:
[Required (ErrorMessage="Server Name Required")]
[StringLength(15, ErrorMessage = "Server Name Cannot Exceed 15 Characters")]
public string servername { get; set; }
[Required(ErrorMessage = "Server OS Type Required")]
public string os { get; set; }
public string[] applications;
Run Code Online (Sandbox Code Playgroud)
我使用以下代码将文本框绑定到服务器名称,工作正常:
@Html.TextBoxFor(m => Model.servername, new {@class="fixed", @id="serverName"})
Run Code Online (Sandbox Code Playgroud)
我正在使用操作系统的下拉列表和应用程序的列表框,这两个列表框都没有在提交时正确填充模型.
@Html.DropDownListFor(m => m.os , new SelectList( ((List<SelectListItem>)ViewData["osTypes"]),"Value","Text"), new { @class = "fixed" })
@Html.ListBoxFor(m => m.applications, new MultiSelectList((List<SelectListItem>)ViewData["appList"]), new {style="display:none;"})
Run Code Online (Sandbox Code Playgroud)
对我在这里做错了什么的想法?
更新:我认为我没有提供足够的信息
在控制器中,ViewData ["osTypes"]设置为List,其中包含从WebAPI中提取的一些默认值:
List<string> osTypes = FastFWD_SITE.Helpers.GetJsonObj._download_serialized_json_data<List<string>>(getOsTypeUrl);
List<SelectListItem> osTypeList = new List<SelectListItem>();
foreach (string …Run Code Online (Sandbox Code Playgroud) 我已经实现了一个 OWIN 自托管 webapi,并且正在尝试使用数据注释和 ActionFilterAttribute 将格式化的错误返回给用户。我在数据注释上设置了自定义错误消息,但是当我尝试从 ModelState 检索消息时,它始终是一个空字符串(如下图所示)。
模型:
public class JobPointer
{
[Required(ErrorMessage = "JobId Required")]
public Guid JobId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
筛选:
public class ModelValidationFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid) return;
string errors = actionContext.ModelState.SelectMany(state => state.Value.Errors).Aggregate("", (current, error) => current + (error.ErrorMessage + ". "));
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, errors);
}
}
Run Code Online (Sandbox Code Playgroud)
端点:
[HttpPost]
public HttpResponseMessage DescribeJob(JobPointer jobId)
{
Job job = _jobhelper.GetJob(jobId.JobId);
return Request.CreateResponse(HttpStatusCode.OK, job);
}
Run Code Online (Sandbox Code Playgroud)
请求正文:
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Grafana v2.5 环境中安装饼图插件,无论我做什么,面板现在都会在 UI 中显示为一个选项。我按照文档将存储库克隆到 /var/lib/grafana/plugins 并重新启动了 grafana-server 服务,但这不起作用。我还尝试将插件放在单独的目录中并将其引用为:
[plugin.piechart]
path = /home/usr/share/grafana/panel-plugin-piechart
Run Code Online (Sandbox Code Playgroud)
我确保grafana服务拥有插件目录的所有权,并检查了grafana日志,但它没有有用的信息。
我在互联网上广泛搜索,寻找一种查询 WSUS 数据库以查看需要在服务器上安装的更新数量的方法。我在以下博客中找到了部分答案,但此答案将假设更新是否正在等待批准,服务器仍然需要它(WSUS 也在其 UI 中假设了这一点)。
http://theboywonder.co.uk/2010/11/04/sql-query-for-wsus-3-needed-updates/