1.环境:
使用语言c#,运行环境windows iis服务器,开发环境vs 2022,创建工程 web
2.需求:
再开发的过程中,需要一些调度数据,这些数据并不需要持久化,也就是需要一定的生命周期就可以,但是担心系统重启的过程中丢失数据,所以将数据量设置成可以持久化的类,在系统析构的时候调用数据备份。
这样做的目的是保证效率,当然如果完全数据持久化也可以,但是会有效率损失,所以采用了缓存+“析构备份且构造读取”这种方案。
通过试验可以确定,iis服务停止和关闭的时候会调析构函数,没有问题,但是如果采用vs的发布并不调用析构函数。不调也正常,因为发布的时候只是替换了一部分文件,内存有可能不被破坏,于是做了如下的试验。
3.试验:
设置一个对象,可以“析构备份且构造读取”的对象,添加一个计算,每次网页刷新的时候都+1,发布玩的系统重复刷新让数字为10,然后执行一遍发布,看数据是在之前的继承上变更11。如果增加,证明发布并没有对内存造成破坏。
4.试验视图:
5.代码
5.1 HomeControler
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Mvc;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
}
[Serializable]
public class A: PersistenceBase
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static A a = new A();
public static A getMy() { return a; }
public void add()
{
index++;
}
public int index = 0;
public A() {
logger.Info("构造函数");
}
~A()
{
logger.Info("析构函数");
}
protected override void LoadData(object o)
{
A a = (A)o;
this.index = a.index;
}
}
[Serializable]
public abstract class PersistenceBase
{
public PersistenceBase()
{
//构造的时候完成自己数据的读取
load();
}
~PersistenceBase()
{
//析构的时候自动完成数据的备份
backup();
}
/// <summary>
/// 将数据持久化
/// </summary>
public void backup()
{
string name = System.AppDomain.CurrentDomain.BaseDirectory+@"\"+ this.GetType().Name;
using (FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, this);
}
}
public void load()
{
string name = System.AppDomain.CurrentDomain.BaseDirectory + @"\" + this.GetType().Name;
using (FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
if (fileStream.Length > 0)
{
object o = binaryFormatter.Deserialize(fileStream);
LoadData(o);
}
}
}
protected abstract void LoadData(object o);
}
}
5.2 index.cshtml
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
<div class="row">
@using WebApplication2.Controllers;
@{
A.getMy().add();
int b = A.getMy().index;
}
<div>@b</div>
<div class="col-md-4">
<h2>Getting started</h2>
<p>ASP.NET Web API is a framework that makes it easy to build HTTP services that reach
a broad range of clients, including browsers and mobile devices. ASP.NET Web API
is an ideal platform for building RESTful applications on the .NET Framework.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301870">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301871">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301872">Learn more »</a></p>
</div>
</div>
5.1 要点
@using WebApplication2.Controllers;
@{
A.getMy().add();
int b = A.getMy().index;
}
<div>@b</div>
[Serializable]
public class A: PersistenceBase
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static A a = new A();
public static A getMy() { return a; }
public void add()
{
index++;
}
public int index = 0;
public A() {
logger.Info("构造函数");
}
~A()
{
logger.Info("析构函数");
}
protected override void LoadData(object o)
{
A a = (A)o;
this.index = a.index;
}
}
[Serializable]
public abstract class PersistenceBase
{
public PersistenceBase()
{
//构造的时候完成自己数据的读取
load();
}
~PersistenceBase()
{
//析构的时候自动完成数据的备份
backup();
}
/// <summary>
/// 将数据持久化
/// </summary>
public void backup()
{
string name = System.AppDomain.CurrentDomain.BaseDirectory+@"\"+ this.GetType().Name;
using (FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, this);
}
}
public void load()
{
string name = System.AppDomain.CurrentDomain.BaseDirectory + @"\" + this.GetType().Name;
using (FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
if (fileStream.Length > 0)
{
object o = binaryFormatter.Deserialize(fileStream);
LoadData(o);
}
}
}
protected abstract void LoadData(object o);
}