当前位置:编程学习 > C#/ASP.NET >>

asp.net开发中怎样去突破文件依赖缓存

答案:在Web项目中可以使用Session,Application等来缓存数据,也可以使用Cache来缓存。

今天我们特别关注的是Cache缓存。Cache位于命名空间System.Web.Caching命名空间下,看到这里我们想到的是它在Web项目中使用。

说明:Cache 类不能在 ASP.NET 应用程序外使用。它是为在 ASP.NET 中用于为 Web 应用程序提供缓存而设计和测试的。在其他类型的应用程序(如控制台应用程序或 Windows 窗体应用程序)中,ASP.NET 缓存可能无法正常工作。

下面看看一些Cache缓存依赖的使用:

通过指定依赖项向缓存添加项例子:
复制代码 代码如下:

Cache.Insert("CacheItem2", "Cached Item 2");
string[] dependencies = { "CacheItem2" };
Cache.Insert("CacheItem3", "Cached Item 3",
new System.Web.Caching.CacheDependency(null, dependencies));

下面看看一个简单的文件依赖缓存的使用,大家都知道Cache是支持文件依赖缓存的:
Cache.Insert("CacheItem4", "Cached Item 4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
下面是多依赖缓存效果:
复制代码 代码如下:

System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
string[] keyDependencies2 = { "CacheItem1" };
System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDependencies2);
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert("CacheItem5", "Cached Item 5", aggDep);

通过上面的这些代码,基本知道Cache一些依赖缓存方面的用法,也达到了我们最初想要的结果。下面通过一个完整的例子来看看Cache文件依赖缓存的使用。
首先定义一个XML文件,其文件内容如下 并附带一个实体类:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student>
<Name>hechen</Name>
<Sex>男</Sex>
<Age>23</Age>
</Student>
<Student>
<Name>情缘</Name>
<Sex>男</Sex>
<Age>23</Age>
</Student>
</Students>

定义一个读取上面xml文件的类:
复制代码 代码如下:

public class AccessProvider
{
public AccessProvider()
{
}
public List<Student> GetStudentList(string filePath)
{
XElement root = XElement.Load(filePath);
IEnumerable<XElement> enumerable = from e in root.Elements("Student") select e;
List<Student> list = new List<Student>();
Student student = null;
foreach (XElement element in enumerable)
{
student = new Student();
student.Name = element.Element("Name").Value;
student.Age = Convert.ToInt32(element.Element("Age").Value);
student.Sex = element.Element("Sex").Value;
list.Add(student);
}
return list;
}
}

读取缓存并设定文件缓存依赖:
复制代码 代码如下:

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> list = Cache["Items1"] as List<Student>;
if (list != null && list.Count>0)
{
list.ForEach(item => { Response.Write(item.Name + " " + item.Age + " "+item.Sex+"<br/>"); });
}
else
{
AccessProvider provider = new AccessProvider();
string fielPath = Server.MapPath("~/Xml/Student.xml");
list = provider.GetStudentList(fielPath);
Cache.Insert("Items1", list, new System.Web.Caching.CacheDependency(fielPath));
}
}
}

后面将上传这个例子,运行页面之后,可以人工去修改上述定义的xml文件,然后刷新页面看看效果。当你修改过此文件之后,缓存内容就会失效 然后重新去读取xml文件内容再次缓存。这里代码不做过多的解释。
Cache 只能用作Web相聚,那如果遇到Console项目 以及WinForm等其他项目怎么办,没有了依赖缓存,我们该如何去解决这个问题。下面我们来看看如何实现一个文件依赖缓存。
目标:实现当某个特定文件夹下的文件被修改 删除 或添加使得系统中的缓存失效或者重新加载缓存。
程序类型: WinForm程序 Web程序 Console程序
我们使用最为简单的Console程序作为例子,这个最具有通用性。
首先这里自定义一个缓存对象:
复制代码 代码如下:

namespace CacheConsole
{
public class Cache
{
private static int Num=50;
private static object obj = new object();
static Cache()
{
}
public static int Get()
{
return Num;
}
public static void Update(int argValue)
{
lock (obj)
{
Num = argValue;
}
}
}
}

上面的缓存其实就是一个用Static 修饰的全局变量,其中定义了一个缓存数据获取的方法和一个缓存更新的方法,静态变量Num作为一个缓存容器,默认初始值为50.这个缓存容器虽然简单了点,但是也能够达到我们的要求。
假设我们程序依赖的文件位于F:\File\ 目录下面,所以我们要去监控这些文件,实现监控更新缓存的代码如下:
复制代码 代码如下:

private static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"F:\File\";
watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastAccess | NotifyFilters.Size;
watcher.Filter = "*.txt";
watcher.Created += delegate(object source, FileSystemEventArgs e) { Console.WriteLine("创建新的文件:" + DateTime.Now.ToString()); Cache.Update(10); };
watcher.Changed += delegate(object source, FileSystemEventArgs e) { Console.WriteLine("文件修改:" + DateTime.Now.ToString()); Cache.Update(20); };
watcher.Deleted += delegate(object source, FileSystemEventArgs e) { Console.WriteLine("文件删除:" + DateTime.Now.ToString()); Cache.Update(30); };
watcher.Renamed += delegate(object source, RenamedEventArgs e) { Console.WriteLine("文件重命名:" + DateTime.Now.ToString()); Cache.Update(40); };
watcher.EnableRaisingEvents = true;
}

这个程序监听了特定目录下的文件创建,修改 ,删除,以及重命名。注意这里程序过滤了只监听.txt文件。
然后我们用用一个程序去不但读取缓存数据
复制代码 代码如下:

static void Main(string[] args)
{
Run();
for (int i = 1; i <= 10000; i++)
{
int value = Cache.Get();
Console.WriteLine("第"+i+"次取值: "+value);
Thread.Sleep(3000);
}
}

启动文件的监听,然后不但得读取缓存数据。运行效果如下:

对文件不做任何修改的情况下运行效果:

对文件的创建运行效果图如下:

对文

上一个:Silverlight中同步调用WebClient的解决办法,是同步!
下一个:在asp.NET 中使用SMTP发送邮件的实现代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,