简体   繁体   English

C#使用语句缓存

[英]C# using statement cache

It must be a very dump question but I am wondering if I can use a cached object as part of the using statement eg 它必须是一个非常转储问题,但我想知道我是否可以使用缓存对象作为using语句的一部分,例如

using(Class1 sample = Cache.GetClass<Class1>())

Cache.class is a static class which uses memoryCache to store a copy of Class1, and the GetClass is to get a copy of the stored object from cache if it is already there. Cache.class是一个静态类,它使用memoryCache存储Class1的副本,而GetClass是从缓存中获取存储对象的副本(如果已存在)。

In my real life (almost, but simpilfied) exmaple, I have got this: 在我的现实生活中(几乎,但是简单)exmaple,我有这个:

using (dataDesignerClass dds = Cache.GetClass<dataDesignerClass>()){
   ...
   Dataset ds = new Dataset();
   dds.dataadapter1.fill(ds); //dds is the data designer which contains all the sqlconnection, sql commands, adapters..etc which can get quite big
   ...
}

..which seems to be ok to me, but I find that SOMETIMES the dataset (ds) is not filled by the dataadapter1, without returning error. ..对我来说似乎没问题,但是我发现有些数据集(ds)没有被dataadapter1填充,没有返回错误。

My GetClass static class: 我的GetClass静态类:

 public static T GetClass<T> () where T: class
        {
            string keyName = "CACHE_" + typeof(T).Name.ToUpper();
            CacheItem cacheItem = null;

            cacheItem = GetCache(keyName); //a function to return the cache item
            if (cacheItem == null)
            {
                T daClass = Activator.CreateInstance(typeof(T)) as T;  //the constructor will call the initilalization routine        
                AddCache(keyName, daClass);
                return daClass;
            }
            return (T)cacheItem.Value;            
        }

Can someone explain why it fails? 有人可以解释它失败的原因吗?

I think it is a bad idea to use using on something you cache. 我认为这是一个坏主意,用using你的东西缓存。

The idea behind using is that it disposes all unmanaged memory allocation and handles an object has before it is destructed. 背后的想法using的是其配置的所有非托管内存分配,处理的对象有被破坏之前。 You should not use your object after it is disposed. 处置后不应使用您的对象。 The problem here is it is not your intention to destruct and get rid of the object, hence you save it in a cache! 这里的问题是你不打算破坏和删除对象,因此你将它保存在缓存中!

Also, a DataReader is somewhat of a cursor typed object. 此外, DataReader在某种程度上是光标类型的对象。 It will not like you for reusing it, especially when you use more than one thread. 它不会让您重复使用它,尤其是当您使用多个线程时。

Disposing the object will most likely break your software and give unexpected and unwanted result. 处置对象很可能会破坏您的软件并产生意外和不需要的结果。 Don't use using in this scenario. 不要使用using此方案。

Reusing a shared object is sometimes good practice, but you need to make sure it can be reused. 重用共享对象有时是很好的做法,但您需要确保它可以重用。 In your program, you are storing a data adapter in the cache and trying to reuse it between different threads, that causes strange results sometimes because the data adapter can't be shared. 在您的程序中,您将数据适配器存储在缓存中并尝试在不同的线程之间重用它,这有时会导致奇怪的结果,因为数据适配器无法共享。 Imaging two threads get a same instance of your adapter and modify it at the same time! 映像两个线程获取适配器的相同实例并同时修改它! IMO the data adapter is quite lite and you can create a new instance for each db read, it's unnecessary to cache and reuse it, that makes things complex. IMO数据适配器非常精简,您可以为每个数据库读取创建一个新实例,不需要缓存和重用它,这会使事情变得复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM