在iis中添加过期标头非常简单,但这会缓存所有静态文件.现在我只想为图像添加过期标题,我该怎么做?即使我想要缓存特定文件?
这样的场景:要在表中插入一定数量的数据,当达到阈值时不再插入,我模拟了这种情况,在多线程(例如asp.net)出现并发问题的情况下.
我的问题是如何解决并发问题,不要使用lock案例
void Main()
{
Enumerable.Range(0,20).ToList().ForEach(i=>{
MockMulit();
});
}
//Start a certain number of threads for concurrent simulation
void MockMulit()
{
int threadCount=100;
ClearData();//delete all data for test
var tasks=new List<Task>(threadCount);
Enumerable.Range(1,threadCount).ToList().ForEach(i=>{
var j=i;
tasks.Add(Task.Factory.StartNew(()=>T3(string.Format("Thread{0}-{1}",Thread.CurrentThread.ManagedThreadId,j))));
});
Task.WaitAll(tasks.ToArray());
CountData().Dump();//show that the result
}
Run Code Online (Sandbox Code Playgroud)
方法一 - 并发非常严重
void T1(string name)
{
using(var conn=GetOpendConn())
{
var count=conn.Query<int>(@"select count(*) from dbo.Down").Single();
if(count<20)
{
conn.Execute(@"insert into dbo.Down (UserName) values (@UserName)",new{UserName=name});
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法二 - 把sql放在一起可以减少并发,但仍然存在
void T2(string name)
{
using(var conn=GetOpendConn())
{ …Run Code Online (Sandbox Code Playgroud) void Main()
{
test((int?)null);
test((bool?)null);
test((DateTime?)null);
}
void test(object p)
{
//**How to get the type of parameter p**
}
Run Code Online (Sandbox Code Playgroud) 我想 在asp.net mvc中异步写日志,代码是这样的:
public ActionResult Index()
{
byte[] buffer = Encoding.UTF8.GetBytes(string.Format("log:{0}{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"), Environment.NewLine));
FileStream fs = new FileStream(@"c:\log.txt", FileMode.Append, FileAccess.Write, FileShare.Write, 1024, true);
fs.BeginWrite(buffer, 0, buffer.Length, ar =>
{
fs.EndWrite(ar);
fs.Close();
}, null);
}
Run Code Online (Sandbox Code Playgroud)
但结果不是我预期的,有些日志没有记录,任何人都可以告诉我代码有什么问题