توسيع كتلة التخزين المؤقت لمكتبة المؤسسة - كيفية الحصول على مثيل MyCacheManager؟

StackOverflow https://stackoverflow.com/questions/1069597

سؤال

نظرًا لأن التطبيق الافتراضي لـ CacheManager لا يوفر GetItemsOfType<> (والعديد من التطبيقات الأخرى) فقد فكرت في إنشاء تطبيق خاص بي:

[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager
{
    //The same constructor as in CacheAppBlock - CacheManager, but it's public here:
    public MyCacheManager(Cache realCache, BackgroundScheduler scheduler, ExpirationPollTimer pollTimer)
    {
       this.realCache = realCache;
       this.scheduler = scheduler;
       this.pollTimer = pollTimer;
    }
    //the other code is basically copy/paste from CacheManager in EntLib, with some of my methods like:
    public T[] GetItemsOfType<T>()
    {
        return realCache.CurrentCacheState.Values.OfType<T>().ToArray();
    }
    //I also have some other custom code on the underlying Hashtable in realCache
}

الجزء التكويني ( يكتب يشير الجزء إلى صفي، ولا يتم استخدام التشفير):

<cachingConfiguration defaultCacheManager="SomeCacheManager">
    <cacheManagers>
      <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
        type="MyNamespace.MyCacheManager, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="SomeCacheManager" />
</cacheManagers>
    <backingStores>
      <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Null Storage" />
    </backingStores>
  </cachingConfiguration>

المشكلة التي أواجهها الآن هي كيفية إنشاء MyCacheManager؟ال:

mCityCacheManager = (MyCacheManager)CacheFactory.GetCacheManager("SomeCacheManager");

يطرح استثناءً قائلًا إنه لا يوجد مُنشئ في MyCacheManager (ولكن يوجد، كما هو الحال في CacheManager في EntLib فقط، فهو عام في صفي ...)

هل كانت مفيدة؟

المحلول

ذلك بسبب MyCacheManager ليس تمامًا مثل EntLib!وأنا لا أقصد الطرق الإضافية.نلقي نظرة على التصريحات.

مدير ذاكرة التخزين المؤقت الأصلي:

[ConfigurationElementType(typeof(CacheManagerData))]
public class CacheManager : IDisposable, ICacheManager

مدير ذاكرة التخزين المؤقت:

[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager

بخلاف اختلاف الاسم (ولم تقم بتوسيع IDisposable)، لاحظ سمات نوع العنصر.

أنت تستخدم (عليك) استخدام الخيار المخصص.يتطلب المخصص مُنشئًا يأخذ NameValueCollection كمعلمة.

public MyCacheManager(NameValueCollection collection)

إنه برنامج تشغيل تكوين عام، إذا جاز التعبير، وعلى هذا النحو لا يمكن توقع معرفة كيفية إنشاء المثيل الخاص بك باستخدام مُنشئ مكون من 3 معلمات يتكون من كائن ذاكرة التخزين المؤقت، وجدولة، ومؤقت الاستقصاء كما لديك.بدلاً من ذلك، يقوم بتمرير هذه القيم (أو أي شيء قمت بتعيينه كسمات في ملف التكوين) عبر NameValueCollection الأساسي الذي سيتعين عليك تحليله يدويًا.

أنظر أيضا:
http://bloggingabout.net/blogs/dennis/archive/2009/10/22/create-a-custom-caching-manager-for-enterprise-library-4.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top