コード例 #1
0
 /// <summary>
 /// Fügt ein neues Objekt in den Cache ein
 /// </summary>
 /// <param name="Cacheable">If set to true, the object will be ignored if it already exists</param>
 /// <param name="ignoreIfExists">If set to true, existing objects will be ignored</param>
 public void AddObject(ICacheable Cacheable, bool ignoreIfExists = true)
 {
     lock (_lockObj)
     {
         if (enableCaching)
         {
             if (Cacheable == null)
             {
                 throw new CoreException("S-0000002", "9c2917a6-41a7-4dc6-876c-43e6336a610f", ExceptionType.Unexpected);
             }
             else
             {
                 // If we have a TypedWeakReference here, we need to store it by the type of its target
                 if (Cacheable.GetType().BaseType == typeof(TypedWeakReference))
                 {
                     var wr   = (Cacheable as TypedWeakReference);
                     var _key = new CacheKeyItem(wr.TargetType, PrepareKey(Cacheable.Key));
                     if (wr.IsAlive && !cache.ContainsKey(_key))
                     {
                         cache.Add(_key, Cacheable);
                     }
                     // If it not not alive, override the object
                     else if (!wr.IsAlive)
                     {
                         cache[_key] = Cacheable;
                     }
                     else
                     {
                         if (!ignoreIfExists)
                         {
                             throw new CoreException("S-0000005", "17771eac-511e-4d83-bd95-288417867990", ExceptionType.Unexpected, () => Cacheable.Key.ToString());
                         }
                     }
                 }
                 else
                 {
                     if (!cache.ContainsKey(new CacheKeyItem(Cacheable.GetType(), PrepareKey(Cacheable.Key))))
                     {
                         cache.Add(new CacheKeyItem(Cacheable.GetType(), PrepareKey(Cacheable.Key)), Cacheable);
                     }
                     else
                     {
                         if (!ignoreIfExists)
                         {
                             throw new CoreException("S-0000005", "8ba4d284-62f7-4c53-8c95-2c9efe5ff3fd", ExceptionType.Unexpected, () => Cacheable.Key.ToString());
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #2
0
 /// <summary>
 /// Fügt ein neues Objekt in den Cache ein
 /// </summary>
 /// <param name="Cacheable">If set to true, the object will be ignored if it already exists</param>
 /// <param name="ignoreIfExists">If set to true, existing objects will be ignored</param>
 public void AddObject(ICacheable Cacheable, bool ignoreIfExists = true)
 {
     lock (_lockObj)
     {
         if (enableCaching)
         {
             if (Cacheable == null)
             {
                 throw new BaseException(10001, "Das Cache-Object darf nicht null sein");
             }
             else
             {
                 // If we have a TypedWeakReference here, we need to store it by the type of its target
                 if (Cacheable.GetType().BaseType == typeof(TypedWeakReference))
                 {
                     var wr   = (Cacheable as TypedWeakReference);
                     var _key = new CacheKeyItem(wr.TargetType, PrepareKey(Cacheable.Key));
                     if (wr.IsAlive && !cache.ContainsKey(_key))
                     {
                         cache.Add(_key, Cacheable);
                     }
                     // If it not not alive, override the object
                     else if (!wr.IsAlive)
                     {
                         cache[_key] = Cacheable;
                     }
                     else
                     {
                         if (!ignoreIfExists)
                         {
                             throw new BaseException(10003, String.Format("Ein Cache-Objekt mit dem gleichen Schlüssel ist bereits vorhanden. (Key: {0})", Cacheable.Key.ToString()));
                         }
                     }
                 }
                 else
                 {
                     if (!cache.ContainsKey(new CacheKeyItem(Cacheable.GetType(), PrepareKey(Cacheable.Key))))
                     {
                         cache.Add(new CacheKeyItem(Cacheable.GetType(), PrepareKey(Cacheable.Key)), Cacheable);
                     }
                     else
                     {
                         if (!ignoreIfExists)
                         {
                             throw new BaseException(10003, String.Format("Ein Cache-Objekt mit dem gleichen Schlüssel ist bereits vorhanden. (Key: {0})", Cacheable.Key.ToString()));
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #3
0
 public void Add(ICacheable obj)
 {
     if (_collection.ContainsKey(obj.Id))
     {
         TrelloConfiguration.Log.Info($"Discovered second instance of {obj.GetType().Name} with ID {obj.Id}");
     }
     _collection[obj.Id] = obj;
 }
コード例 #4
0
        private static Type GetRealType(this ICacheable entity)
        {
            var entityType = entity.GetType();

            if (entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies")
            {
                entityType = entityType.BaseType;
            }
            return(entityType);
        }
コード例 #5
0
        /// <summary>
        /// Checks if a object has been cached past the defined caching time or if internally the object has been marked as expired
        /// </summary>
        /// <param name="value">Object to check for expiry</param>
        /// <returns>True if the object has expired</returns>
        protected bool CheckIsExpired(ICacheable value)
        {
            bool     isExpired = false;
            TimeSpan timeToExpire;

            if (_cacheExpiryTimes.TryGetValue(value.GetType(), out timeToExpire))
            {
                isExpired = value.TimeCachedUtc.HasValue && value.TimeCachedUtc.Value.Add(timeToExpire) < DateTime.UtcNow;
            }

            return(isExpired || value.HasExpired);
        }
コード例 #6
0
 private static IJsonCacheable ExtractData(ICacheable obj)
 {
     return(_jsonExtraction[obj.GetType()](obj));
 }
コード例 #7
0
ファイル: CacheHelper.cs プロジェクト: sanjukz/FIL
 private static string GetKeyName(ICacheable data)
 {
     return(GetKeyName(data.GetType(), data.CacheKey));
 }
コード例 #8
0
ファイル: CacheManager.cs プロジェクト: yukiyuki/MyToolkit
        private ICacheable AddItem(ICacheable item, HashSet <ICacheable> mergedObjects, bool mergeFields)
        {
            var isMerging   = false;
            var currentItem = GetItem(item.GetType(), item.Id);

            if (currentItem == null)
            {
                currentItem = item;

                // add item to list
                var type = GetBaseType(item.GetType());
                if (!list.ContainsKey(type.Name))
                {
                    var group = new Dictionary <int, ICacheable>();
                    list[type.Name] = @group;
                }
                list[type.Name][item.Id] = item;
            }
            else
            {
                isMerging = true;
            }

            if (mergedObjects.Contains(currentItem))
            {
                return(currentItem);
            }
            mergedObjects.Add(currentItem);             // used to avoid recursions

            if (isMerging)
            {
                Debug.WriteLine("CacheManager: Merging item " + item.GetType().Name + "." + item.Id);
            }
            else
            {
                Debug.WriteLine("CacheManager: Adding item " + item.GetType().Name + "." + item.Id);
            }

            // copy new values into old object
            if (mergeFields)
            {
                var type = item.GetType();
                foreach (var property in type.GetProperties())
                {
                    var attr = property.GetCustomAttributes(typeof(DataMemberAttribute), true).FirstOrDefault();
                    if (attr != null)
                    {
                        var isCacheableProperty = false;

                        var value = property.GetValue(item, null);
                        if (value != null)
                        {
                            if (value is ICacheable)
                            {
                                value = AddItem((ICacheable)value, mergedObjects, !mergedObjects.Contains((ICacheable)value));
                                isCacheableProperty = true;
                            }
                            else if (value is IList)
                            {
                                var listType         = value.GetType();
                                var genericArguments = listType.GetGenericArguments();
                                if (genericArguments.Count() == 1)
                                {
                                    var listItemType = genericArguments[0];
                                    if (listItemType.GetInterfaces().Contains(typeof(ICacheable)))
                                    {
                                        var ofType = typeof(Enumerable).GetMethod("OfType");
                                        ofType = ofType.MakeGenericMethod(listItemType);

                                        isCacheableProperty = true;
                                        value = Activator.CreateInstance(listType, ofType.Invoke(null,
                                                                                                 new object[] {
                                            ((IEnumerable <ICacheable>)value)
                                            .Select(i => AddItem(i, mergedObjects, !mergedObjects.Contains(i)))
                                            .OfType <object>()
                                            .ToArray()
                                        })
                                                                         );
                                    }
                                }
                            }
                        }

                        if ((isMerging || isCacheableProperty) && property.CanWrite)
                        {
                            if (value == null)
                            {
                                var idProperty = type.GetProperty(property.Name + "Id");
                                if (idProperty != null)                                 // check if value is null or not loaded
                                {
                                    var id = (int?)idProperty.GetValue(item, null);
                                    if (!id.HasValue)                                     // if property changed to null => set null in current cached object
                                    {
                                        property.SetValue(currentItem, null, null);
                                    }
                                }
                            }
                            else
                            {
                                property.SetValue(currentItem, value, null);
                            }
                        }
                    }
                }
            }
            return(currentItem);
        }
コード例 #9
0
        public void Set(ICacheable obj)
        {
            if (obj == null)
            {
                return;
            }

            string fullName = obj.GetType().FullName;

            if (!IsNeedCached(fullName))
            {
                return;
            }

            SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(fullName);
            Dictionary <string, MyCacheObject> caches = GetCaches(fullName);

            if (!config.LockObj.TryEnterWriteLock(WriteLockTimeout))
            {
                return;
            }

            try
            {
                string        id = obj.Id.ToLower();
                MyCacheObject myCacheObject;
                if (caches.ContainsKey(id))
                {
                    myCacheObject = caches[id];
                    if (myCacheObject.Obj.Version >= obj.Version)
                    {
                        return;
                    }

                    myCacheObject.Obj         = obj;
                    myCacheObject.OverdueTime = DateTime.Now.AddSeconds(config.Second);
                }
                else
                {
                    myCacheObject = new MyCacheObject(obj, DateTime.Now.AddSeconds(config.Second));
                    caches.Add(id, myCacheObject);
                }

                try
                {
                    config.Setted(obj);
                }
                catch (Exception ex)
                {
                    LogProxy.Error(ex, false);
                }
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, false);
            }
            finally
            {
                config.LockObj.ExitWriteLock();
            }
        }