2011-09-17 16 views
5

मेरे पास पोस्टशर्प का उपयोग करके लागू एक सरल कैश विशेषता है। जब मैं कैश नीति सेट करता हूं तो मैं नीचे जैसे अपडेट कॉलबैक सेट करने में सक्षम होना चाहता हूं।मैं किसी अन्य वर्ग से कैशइटेम पॉलिसी पर अपडेट कैलबैक कैसे कार्यान्वित कर सकता हूं?

private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry) 
    { 
     var policy = new CacheItemPolicy(); 

     switch (type) 
     { 
      case (CacheType.Absolute): 
       policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry); 
       policy.UpdateCallback = new CacheEntryUpdateCallback(UpdateHandler); 
       break; 
      case (CacheType.Sliding): 
       policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry); 
       break; 
     } 

     return policy; 
    } 

यह ठीक है अगर मैं ऐसा करना चाहते हैं:

private static void UpdateHandler(CacheEntryUpdateArguments arguments) 
    { 
     throw new NotImplementedException(); 
    } 

हालांकि, मैं गतिशील में एक प्रतिनिधि/विधि/विधि नाम और मानकों में गुजरती हैं और कि निष्पादित करने में सक्षम होना चाहता हूँ। अतः मैं की तरह कुछ देखने की उम्मीद कर सकता है (जाहिर है वाक्य रचना गलत है):

private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry Func<?,?> method) 
    { 
     var policy = new CacheItemPolicy(); 

     switch (type) 
     { 
      case (CacheType.Absolute): 
       policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry); 
       policy.UpdateCallback = new CacheEntryUpdateCallback(method); 
       break; 
      case (CacheType.Sliding): 
       policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry); 
       break; 
     } 

     return policy; 
    } 

** * ** * ** * *अद्यतन* * * * ****

मुझे यह काम मिल गया। सबसे सुरुचिपूर्ण विधि नहीं है, यह काम करता है।

मेरे पहलू कोड इस प्रकार है:

private static void UpdateHandler(CacheEntryUpdateArguments arguments) 
    { 
     CacheObject cacheObject = (CacheObject)arguments.Source.Get(arguments.Key); 
     cacheObject.Context.Proceed(); 
     cacheObject.CacheValue = cacheObject.Context.ReturnValue; 

     CacheItem updatedItem = new CacheItem(arguments.Key, cacheObject); 
     arguments.UpdatedCacheItem = updatedItem; 
    } 
+0

आप प्रश्न में अपने पहलू कोड शामिल कर सकता है? मैं समझ नहीं पा रहा हूं कि आप क्या हासिल करने की कोशिश कर रहे हैं। GetCachePolicy विधि कहां से बुलाया जाता है और कहां पहलू लागू किया जाता है? –

उत्तर

6

आप ऐसा कर सकते हैं:

private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry, 
            Action<CacheEntryUpdateArguments> method) 
{ 
    ... 
    policy.UpdateCallback = (a) => method(a); 
    ... 
    return policy; 
} 

या सिर्फ इस:

[Serializable] 
public sealed class CacheAttribute : MethodInterceptionAspect 
{ 
    private readonly CacheType m_cacheType; 
    private readonly int m_expiry; 
    private readonly bool m_useCallBack; 
    private KeyBuilder m_keyBuilder; 

    public KeyBuilder KeyBuilder 
    { 

     get { return m_keyBuilder ?? (m_keyBuilder = new KeyBuilder()); } 

    } 

    public CacheAttribute(CacheType cacheType, int expiry, bool useCallBack) 
    { 
     m_cacheType = cacheType; 
     m_expiry = expiry; 
     m_useCallBack = useCallBack; 
    } 

    public CacheAttribute(CacheType cacheType, int expiry) 
    { 
     m_cacheType = cacheType; 
     m_expiry = expiry; 
     m_useCallBack = false; 
    } 


    //Method executed at build time. 

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo) 
    { 

     KeyBuilder.MethodParameters = method.GetParameters(); 

     KeyBuilder.MethodName = string.Format("{0}.{1}", method.DeclaringType.FullName, method.Name); 

    } 

    public override void OnInvoke(MethodInterceptionArgs context) 
    { 
     object value; 


     string key = KeyBuilder.BuildCacheKey(context, context.Arguments); 
     if (!CacheHelper.Get(key, out value)) 
     { 
      // Do lookup based on caller's logic. 
      context.Proceed(); 
      value = context.ReturnValue; 
      var cacheObject = new CacheObject {CacheValue = value, Context = context}; 
      CacheHelper.Add(cacheObject, key, m_cacheType, m_expiry, m_useCallBack); 
     } 

     context.ReturnValue = value; 
    } 
} 

मेरे कॉलबैक इस प्रकार है

private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry, 
              CacheEntryUpdateCallback method) 
{ 
    ... 
    policy.UpdateCallback = method; 
    ... 
    return policy; 
} 
0

मुझे लगता है कि आप यह कर सकते हैं

` 
    private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry, Func method) 
    { 
     var policy = new CacheItemPolicy(); 

     switch (type) 
     { 
      case (CacheType.Absolute): 
        Action updateCallBackHandler = null; 
        updateCallBackHandler = delegate(CacheEntryUpdateArguments arguments) 
        {       
         var newData = FetchYourDataWithCustomParameters(); 

         arguments.UpdatedCacheItem = new CacheItem(arguments.Key, newData); 
         arguments.UpdatedCacheItemPolicy = new CacheItemPolicy() 
         { 
          AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry), 
          UpdateCallback = new CacheEntryUpdateCallback(updateCallBackHandler) 
         }; 
        }; 

        policy.UpdateCallback = new CacheEntryUpdateCallback(updateCallBackHandler); 

       break; 
      case (CacheType.Sliding): 
       policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry); 
       break; 
     } 

     return policy; 
    } 
'
संबंधित मुद्दे