c#覆盖文件,IIS 7.5使用错误文件
我有一个程序可以覆盖我的网站所需的一组特定文件,但是我网站上的流量增加了很多,以至于我现在收到错误文件正在使用。这导致它无法更新文件。
该程序每 5 分钟运行一次以更新指定的文件。
我让这个程序处理文件而不是程序的写入的原因是因为我还需要将其更新到不同的网络服务器(通过 ftp)。这样我还可以确保文件每 5 分钟更新一次,而不是在用户查看页面时更新一次。
因此我的问题是;我可以告诉 IIS7.5 在文件更新后(比如 5 秒到 1 分钟)缓存该文件吗?这应该确保程序下次运行更新文件时不会遇到任何问题。
I have a program that overwrites a certain set of files required for my website, however the traffic on my website has increased so much that i now get the error File in use. Which results in it being unable to update the file..
This program runs every 5 minutes to update the specified files.
The reason I let this program handle the writing of the file and not the program, is cause I also need to update it to a different webserver (through ftp). this way I also ensure that the file gets updated every 5 minutes, instead of when a user would take a look at page.
My question therefore is; Can i tell IIS7.5 to cache the file after (say 5 seconds to 1 minute) it has been updated? This should ensure that the next time the program runs to update the file it won't encounter any problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是更改刷新文件的程序以将新信息存储在数据库中,而不是文件系统中。
但如果你不能使用数据库,我会采取不同的方法,将文件内容存储到 System.Web.Caching.Cache 以及上次修改的时间,然后检查文件是否更改(如果不使用缓存版本),如果更改则存储同一缓存变量中的新内容和时间。
当然,您必须检查是否可以读取该文件,然后刷新缓存内容,如果无法读取该文件,您可以简单地从缓存中获取最新版本。
初始读取文件必须在 application_start 中以确保缓存已初始化,并且必须等到可以读取文件才能将其首次存储在缓存中。
检查是否可以从文件中读取的最佳方法是捕获异常,因为检查后可能会发生锁定,请参阅这篇文章: 如何检查文件锁定?
Simplest solution would be change that program that refreshes the file to store that new information in database, not in filesystem.
But if you can't use database I would take different approach, store file contents to System.Web.Caching.Cache with time when was last modified, and then check if file is changed if not use cached version, and if was changed store new contents and time in same cache variable.
Of course you will have to check if you can read the file, and only then refresh cache contents, and if you can not read the file you can simply get last version from the cache.
Initial reading of file would have to be in application_start to ensure that cache has been initialized, and there you will have to wait until you can read file to store it in the cache for the first time.
Best way to check that you can read from file is to catch exception, because lock can happen after your check, see this post : How to check for file lock?