/// <summary> /// Добавить объект в кеш. /// </summary> /// <param name="dataobject"> /// Объект данных. /// </param> private void AddLivingDataObject(DataObject dataobject) { if (NoCaches) { throw new DOCacheNotFoundException(); } WeakReference wr = new WeakReference(dataobject); TypeKeyPair tkp = new TypeKeyPair(dataobject.GetType(), dataobject.__PrimaryKey); if (_lastCacheIndex > _objectCaches.Count - 1) { LogService.LogDebug(_lastCacheIndex + ":" + _objectCaches.Count); } Dictionary <TypeKeyPair, WeakReference> sl = (Dictionary <TypeKeyPair, WeakReference>)_objectCaches[_lastCacheIndex]; try { sl.Remove(tkp); } catch { } try { sl.Add(tkp, wr); } catch { } }
/// <summary> /// Сравнение двух пар. /// </summary> /// <param name="obj">Объект для сравнения.</param> /// <returns>Результат сравнения.</returns> public int CompareTo(object obj) { TypeKeyPair typeKeyPair = (TypeKeyPair)obj; int result = Type.GetHashCode().CompareTo(typeKeyPair.Type.GetHashCode()); result = result == 0 ? ((IComparable)Key).CompareTo(typeKeyPair.Key) : result; return(result); }
/// <summary> /// Получить "живой" внутри приложения объект данных по указанию /// типа объекта данных и первичного ключа. /// Возвращается null, если объект не найден или он уже "умер". /// </summary> /// <param name="typeofdataobject"> /// The typeofdataobject. /// </param> /// <param name="key"> /// The key. /// </param> /// <returns> /// The <see cref="DataObject"/>. /// </returns> private DataObject PrvGetLivingDataObject(Type typeofdataobject, object key) { key = Information.TranslateValueToPrimaryKeyType(typeofdataobject, key); DataObject result; if (NoCaches) { return(null); } WeakReference wr = null; try { Dictionary <TypeKeyPair, WeakReference> cacheList = (Dictionary <TypeKeyPair, WeakReference>)_objectCaches[_lastCacheIndex]; TypeKeyPair typeKeyPair = new TypeKeyPair(typeofdataobject, key); if (cacheList.ContainsKey(typeKeyPair)) { wr = cacheList[typeKeyPair]; } } catch (Exception ex) { LogService.LogDebug(ex); } if (wr != null) { if (wr.IsAlive) { result = (DataObject)wr.Target; } else { PrvRemoveLivingDataObject(typeofdataobject, key); result = null; } } else { result = null; } return(result); }
/// <summary> /// Удалить "живой" объект данных из кеша. /// </summary> /// <param name="typeofdataobject"> /// Тип объекта данных. /// </param> /// <param name="key"> /// Ключ объекта данных. /// </param> private void PrvRemoveLivingDataObject(Type typeofdataobject, object key) { if (!NoCaches) { key = Information.TranslateValueToPrimaryKeyType(typeofdataobject, key); TypeKeyPair pairkey = new TypeKeyPair(typeofdataobject, key); if (_lastCacheIndex == -1) { return; } Dictionary <TypeKeyPair, WeakReference> sl = (Dictionary <TypeKeyPair, WeakReference>)_objectCaches[_lastCacheIndex]; if (sl.ContainsKey(pairkey)) { WeakReference wr = sl[pairkey]; if (!wr.IsAlive) { sl.Remove(pairkey); } } } }