Esempio n. 1
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = Cache;

            lock (cache.SyncRoot)
            {
                _object = info.Object; // BVChanges: adding _object field

                CompoundValue key = (CompoundValue)info.Items["CacheKey"];

                if (key == null)
                {
                    return;
                }

                int  maxCacheTime = _instanceMaxCacheTime ?? MaxCacheTime;
                bool isWeak       = _instanceIsWeak ?? IsWeak;

                CacheAspectItem item = new CacheAspectItem();

                item.ReturnValue  = info.ReturnValue;
                item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
                                    DateTime.MaxValue:
                                    DateTime.Now.AddMilliseconds(maxCacheTime);

                ParameterInfo[] pis = info.CallMethodInfo.Parameters;

                int n = 0;

                foreach (ParameterInfo pi in pis)
                {
                    if (pi.ParameterType.IsByRef)
                    {
                        n++;
                    }
                }

                if (n > 0)
                {
                    item.RefValues = new object[n];

                    n = 0;

                    for (int i = 0; i < pis.Length; i++)
                    {
                        if (pis[i].ParameterType.IsByRef)
                        {
                            item.RefValues[n++] = info.ParameterValues[i];
                        }
                    }
                }

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }
Esempio n. 2
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;

            lock (cache.SyncRoot)
            {
                var key = (CompoundValue)info.Items["CacheKey"];

                if (key == null)
                {
                    return;
                }

                var maxCacheTime = _instanceMaxCacheTime ?? MaxCacheTime;
                var isWeak       = _instanceIsWeak ?? IsWeak;

                var item = new CacheAspectItem
                {
                    ReturnValue  = info.ReturnValue,
                    MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0 ?
                                   DateTime.MaxValue :
                                   DateTime.Now.AddMilliseconds(maxCacheTime),
                };

                var pis = info.CallMethodInfo.Parameters;
                var n   = 0;

                foreach (var pi in pis)
                {
                    if (pi.ParameterType.IsByRef)
                    {
                        n++;
                    }
                }

                if (n > 0)
                {
                    item.RefValues = new object[n];

                    n = 0;

                    for (var i = 0; i < pis.Length; i++)
                    {
                        if (pis[i].ParameterType.IsByRef)
                        {
                            item.RefValues[n++] = info.ParameterValues[i];
                        }
                    }
                }

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }
Esempio n. 3
0
		protected override void AfterCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			IDictionary cache = GetCache(info);

			lock (cache.SyncRoot)
			{
				CompoundValue key = (CompoundValue)info.Items["CacheKey"];

				if (key == null)
					return;

				int  maxCacheTime = MaxCacheTime;
				bool isWeak       = IsWeak;

				if (!string.IsNullOrEmpty(info.ConfigString))
				{
					ConfigParameters cp = GetConfigParameters(info);

					if (cp.MaxCacheTime != null) maxCacheTime = (int) cp.MaxCacheTime;
					if (cp.IsWeak       != null) isWeak       = (bool)cp.IsWeak;
				}

				CacheAspectItem item = new CacheAspectItem();

				item.ReturnValue  = info.ReturnValue;
				item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
					DateTime.MaxValue:
					DateTime.Now.AddMilliseconds(maxCacheTime);

				ParameterInfo[] pis = info.CallMethodInfo.Parameters;

				int n = 0;

				foreach (ParameterInfo pi in pis)
					if (pi.ParameterType.IsByRef)
						n++;

				if (n > 0)
				{
					item.RefValues = new object[n];

					n = 0;

					for (int i = 0; i < pis.Length; i++)
						if (pis[i].ParameterType.IsByRef)
							item.RefValues[n++] = info.ParameterValues[i];
				}

				cache[key] = isWeak? (object)new WeakReference(item): item;
			}
		}
Esempio n. 4
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = GetCache(info);

            lock (cache.SyncRoot)
            {
                CompoundValue   key  = GetKey(info);
                CacheAspectItem item = GetItem(cache, key);

                if (item != null && !item.IsExpired)
                {
                    info.InterceptResult = InterceptResult.Return;
                    info.ReturnValue     = item.ReturnValue;

                    if (item.RefValues != null)
                    {
                        ParameterInfo[] pis = info.CallMethodInfo.Parameters;
                        int             n   = 0;

                        for (int i = 0; i < pis.Length; i++)
                        {
                            if (pis[i].ParameterType.IsByRef)
                            {
                                info.ParameterValues[i] = item.RefValues[n++];
                            }
                        }
                    }

                    info.Cached = true;
                }
                else
                {
                    info.Items["CacheKey"] = key;
                }
            }
        }
Esempio n. 5
0
        private static void SetReturnFromCache(InterceptCallInfo info, CacheAspectItem item)
        {
            info.InterceptResult = InterceptResult.Return;
            info.ReturnValue     = item.ReturnValue;

            if (item.RefValues != null)
            {
                var pis = info.CallMethodInfo.Parameters;
                var n   = 0;

                for (var i = 0; i < pis.Length; i++)
                {
                    if (pis[i].ParameterType.IsByRef)
                    {
                        info.ParameterValues[i] = item.RefValues[n++];
                    }
                }
            }

            info.Cached = true;
        }
Esempio n. 6
0
		protected override void AfterCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			var cache = Cache;

			lock (cache.SyncRoot)
			{
				var key = (CompoundValue)info.Items["CacheKey"];

				if (key == null)
					return;

				var maxCacheTime = _instanceMaxCacheTime ?? MaxCacheTime;
				var isWeak       = _instanceIsWeak       ?? IsWeak;

				var item = new CacheAspectItem
				{
					ReturnValue = info.ReturnValue,
					MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0 ?
						DateTime.MaxValue :
						DateTime.Now.AddMilliseconds(maxCacheTime),
				};

				var pis = info.CallMethodInfo.Parameters;
				var n   = 0;

				foreach (var pi in pis)
					if (pi.ParameterType.IsByRef)
						n++;

				if (n > 0)
				{
					item.RefValues = new object[n];

					n = 0;

					for (var i = 0; i < pis.Length; i++)
						if (pis[i].ParameterType.IsByRef)
							item.RefValues[n++] = info.ParameterValues[i];
				}

				cache[key] = isWeak? (object)new WeakReference(item): item;
			}
		}
Esempio n. 7
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = GetCache(info);

            lock (cache.SyncRoot)
            {
                CompoundValue key = (CompoundValue)info.Items["CacheKey"];

                if (key == null)
                {
                    return;
                }

                int  maxCacheTime = MaxCacheTime;
                bool isWeak       = IsWeak;

                if (!string.IsNullOrEmpty(info.ConfigString))
                {
                    ConfigParameters cp = GetConfigParameters(info);

                    if (cp.MaxCacheTime != null)
                    {
                        maxCacheTime = (int)cp.MaxCacheTime;
                    }
                    if (cp.IsWeak != null)
                    {
                        isWeak = (bool)cp.IsWeak;
                    }
                }

                CacheAspectItem item = new CacheAspectItem();

                item.ReturnValue  = info.ReturnValue;
                item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
                                    DateTime.MaxValue:
                                    DateTime.Now.AddMilliseconds(maxCacheTime);

                ParameterInfo[] pis = info.CallMethodInfo.Parameters;

                int n = 0;

                foreach (ParameterInfo pi in pis)
                {
                    if (pi.ParameterType.IsByRef)
                    {
                        n++;
                    }
                }

                if (n > 0)
                {
                    item.RefValues = new object[n];

                    n = 0;

                    for (int i = 0; i < pis.Length; i++)
                    {
                        if (pis[i].ParameterType.IsByRef)
                        {
                            item.RefValues[n++] = info.ParameterValues[i];
                        }
                    }
                }

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }
Esempio n. 8
0
            private static void Cleanup(object state)
            {
                if (!Monitor.TryEnter(_caches.SyncRoot, 10))
                {
                    // The Cache is busy, skip this turn.
                    //
                    return;
                }

                DateTime start          = DateTime.Now;
                int      objectsInCache = 0;

                try
                {
                    _workTimes++;

                    List <DictionaryEntry> list = new List <DictionaryEntry>();

                    foreach (IDictionary cache in _caches)
                    {
                        lock (cache.SyncRoot)
                        {
                            foreach (DictionaryEntry de in cache)
                            {
                                WeakReference wr = de.Value as WeakReference;

                                bool isExpired;

                                if (wr != null)
                                {
                                    CacheAspectItem ca = wr.Target as CacheAspectItem;

                                    isExpired = ca == null || ca.IsExpired;
                                }
                                else
                                {
                                    isExpired = ((CacheAspectItem)de.Value).IsExpired;
                                }

                                if (isExpired)
                                {
                                    list.Add(de);
                                }
                            }

                            foreach (DictionaryEntry de in list)
                            {
                                cache.Remove(de.Key);
                                _objectsExpired++;
                            }

                            list.Clear();

                            objectsInCache += cache.Count;
                        }
                    }

                    _objectsInCache = objectsInCache;
                }
                finally
                {
                    _workTime += DateTime.Now - start;

                    Monitor.Exit(_caches.SyncRoot);
                }
            }
Esempio n. 9
0
		private static void SetReturnFromCache(InterceptCallInfo info, CacheAspectItem item)
		{
			info.InterceptResult = InterceptResult.Return;
			info.ReturnValue     = item.ReturnValue;

			if (item.RefValues != null)
			{
				var pis = info.CallMethodInfo.Parameters;
				var n = 0;

				for (var i = 0; i < pis.Length; i++)
					if (pis[i].ParameterType.IsByRef)
						info.ParameterValues[i] = item.RefValues[n++];
			}

			info.Cached = true;
		}