/// <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()); } } } } } } }
/// <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())); } } } } } } }
/// <summary> /// Entfernt ein Cache-Objekt aus dem speicher /// </summary> /// <param name="Key">Eindeutiger Schlüssel, anhand dessen das Objekt identifiziert wird</param> public void RemoveObjectNoException <T>(string Key) { lock (_lockObj) { if (enableCaching) { if (Key == null) { throw new CoreException("S-0000004", "cc0a2887-93a8-494c-903c-3eb0cfdb1e43", ExceptionType.Unexpected); } else { var _key = new CacheKeyItem(typeof(T), PrepareKey(Key)); if (cache.ContainsKey(_key)) { cache[_key].OnRemove(); cache.Remove(_key); } } } } }
/// <summary> /// Entfernt ein Cache-Objekt aus dem speicher /// </summary> /// <param name="Key">Eindeutiger Schlüssel, anhand dessen das Objekt identifiziert wird</param> public void RemoveObjectNoException <T>(string Key) { lock (_lockObj) { if (enableCaching) { if (Key == null) { throw new BaseException(10001, "Der Cache-Object-Key darf nicht null sein"); } else { var _key = new CacheKeyItem(typeof(T), PrepareKey(Key)); if (cache.ContainsKey(_key)) { cache[_key].OnRemove(); cache.Remove(_key); } } } } }