系统用单例模式保存缓存的机制引用问题处理
c/s系统没有b/s的session,所以我们就自己读取放置大内存中已达到缓存的效果
为了保持缓存在系统中的一致性,所以就要用到单例模式了。。
View Code
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { /// <summary> /// 顶层调用类 /// </summary> class Program { private cClass cc = cClass.Instance; static void Main(string[] args) { Program m = new Program(); m.tt(); m.t1(); Console.Read(); } void t1() { Console.WriteLine(cc.GetName()); } void tt() { cClass n = cClass.Instance; Console.WriteLine(n.GetName()); n.Dispose(); } } /// <summary> /// Facade类 /// </summary> class cClass : baseClass { public cClass(string name) : base(name) {} public override void Dispose() { var temp = instance; instance = new cClass("test2"); temp = null; } private static cClass instance = new cClass("test"); public static cClass Instance { get { if (instance == null) { instance = new cClass("test2"); } return instance; } } public string GetName() { return this.GetOName(); } } /// <summary> /// 2级别类 /// </summary> class baseClass : IDisposable { private model o; protected baseClass(string name) { o = new model(){ Name=name}; } public virtual void Dispose() { if (o != null) o = null; } protected string GetOName() { return o.Name; } } /// <summary> /// 1底层变化类 /// </summary> class model { public string Name { get; set; } } }
但是在模拟的单例缓存中。会遇到,类中定义的
private cClass cc = cClass.Instance;
被方法中的
cClass n = cClass.Instance; Console.WriteLine(n.GetName()); n.Dispose();
释放。导致运行时报错了,或者我dispose,没有清理掉单例的内容,导致类级别引用对象没有情况造成不一致问题
后来发现我吧单例放的层太高,只要下降一层,就可以解决问题了。。
View Code
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class ProgramTwo { private DClass dd = new DClass("dd"); private CClass cc = new CClass("cc"); static void Main(string[] args) { ProgramTwo m = new ProgramTwo(); m.ttd(); m.td(); m.ttc(); m.tc(); Console.Read(); } void td() { Console.WriteLine(dd.GetName()); } void ttd() { DClass n = new DClass("testdd"); Console.WriteLine(n.GetName()); n.Dispose(); } void tc() { Console.WriteLine(dd.GetName()); } void ttc() { CClass n = new CClass("testcc"); Console.WriteLine(n.GetName()); n.Dispose(); } } class basecClass { protected baseClass bc; public basecClass(baseClass _bc) { bc = _bc; } public string GetName() { return bc.GetOName(); } } class CClass : basecClass, IDisposable { private static baseClass sbc = new baseClass("testc"); public CClass(string name) : base(sbc) { sbc.CreateDateTime = DateTime.Now; } public void Dispose() { sbc.Dispose(); } } class DClass : basecClass, IDisposable { private static baseClass dbc = new baseClass("testd"); public DClass(string name) : base(dbc) { dbc.CreateDateTime = DateTime.Now; } public void Dispose() { dbc.Dispose(); } } class baseClass : IDisposable { public DateTime CreateDateTime { get; set; } private model o; public baseClass(string name) { o = new model() { Name = name }; } public void Dispose() { o.Name = "Clear"; } public string GetOName() { return o.Name; } } class model { public string Name { get; set; } } }
这样就可以处理单例的问题了。。
因为很少写博客。可能表达不好,还希望大家原谅