vs web 发布是否会情况缓存

该文描述了一个C#Web应用程序在IIS服务器上开发的场景,使用VS2022。为了平衡效率和数据完整性,开发者采用了一个结合缓存和析构时数据备份的策略。通过测试发现,IIS停止和关闭时会触发析构,但VS发布时不一定会。文章通过一个简单的计数器实验验证了发布过程对内存的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值