public object HandleCall(MethodInvocation call)
        {
            ICacheHolder cacheHolder = call.Target as ICacheHolder;

            if (cacheHolder != null)
            {
                Hashtable cache = cacheHolder.Cache;
                string    key   = call.ValueSignature;
                if (!cache.ContainsKey(key))
                {
                    cache[key] = call.Proceed();
                    Console.WriteLine("adding result to cache");
                }
                else
                {
                    Console.WriteLine("result fetched from cache");
                }

                return(cache[key]);
            }
            else
            {
                return(call.Proceed());
            }
        }
Ejemplo n.º 2
0
        private CachedFunc <T, TKey, TResult> CreateFunc <T, TKey, TResult>(
            int funcID,
            Func <T, TResult> func,
            Func <T, TKey> keySelector,
            CachedFuncOptions options)
        {
            ICacheHolder <TKey, TResult>        cache = GetCacheHolder <TKey, TResult>(options);
            ConcurrentDictionary <TKey, object> locks = new ConcurrentDictionary <TKey, object>();
            CachedFunc <T, TKey, TResult>       ret   = (input, fallback, nocache) =>
            {
                TResult obj;
                TKey    key = keySelector(input);
                if (!key.Equals(null))
                {
                    if (!nocache)
                    {
                        if (cache.TryGetValue(key, funcID, out obj))
                        {
                            return(obj);
                        }
                    }
                }
                else
                {
                    throw new ArgumentNullException("[input] of the function is null or [keySelector(T)] returns null.");
                }

                object lockObj = locks.GetOrAdd(key, new object());
                Monitor.Enter(lockObj);
                try
                {
                    if (!nocache && cache.TryGetValue(key, funcID, out obj))
                    {
                        return(obj);
                    }
                    var fun = fallback ?? func;
                    if (fun != null)
                    {
                        obj = fun(input);
                        cache.Add(key, funcID, obj);
                        return(obj);
                    }
                    throw new ArgumentNullException("Please provide a [fallback] function for calculating the value. ");
                } finally {
                    locks.TryRemove(key, out object o);
                    Monitor.Exit(lockObj);
                }
            };

            return(ret);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructs a new cache manager for a given component type and with a specific cache holder implementation.
 /// </summary>
 /// <param name="component">The component to which the cache applies (context).</param>
 /// <param name="cacheHolder">The cache holder that contains the entities cached.</param>
 public DefaultCacheManager(Type component, ICacheHolder cacheHolder)
 {
     _component   = component;
     _cacheHolder = cacheHolder;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructs a new cache manager for a given component type and with a specific cache holder implementation.
 /// </summary>
 /// <param name="cacheHolder">The cache holder that contains the entities cached.</param>
 public StaticCacheManager(ICacheHolder cacheHolder)
 {
     this.cacheHolder = cacheHolder;
 }
Ejemplo n.º 5
0
 public DefaultCacheManager(ICacheHolder cacheHolder, ICacheFactory cacheFactory)
 {
     _cacheHolder  = cacheHolder;
     _cacheFactory = cacheFactory;
 }
Ejemplo n.º 6
0
 public DefaultCacheManager(Type component, ICacheHolder cacheHolder)
 {
     this.component   = component;
     this.cacheHolder = cacheHolder;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Constructs a new cache manager for a given component type and with a specific cache holder implementation.
 /// </summary>
 /// <param name="component">The component to which the cache applies (context).</param>
 /// <param name="cacheHolder">The cache holder that contains the entities cached.</param>
 public MemCacheManager(Type component, IMemCacheHolder cacheHolder)
 {
     _component = component;
     _cacheHolder = cacheHolder;
 }