using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FLocal.Core { class Registry where TData : IDataObject, new() where TKey : struct { internal static readonly Registry instance = new Registry(); private volatile Dictionary storage; private volatile Dictionary locks; private Registry() { this.storage = new Dictionary(); this.locks = new Dictionary(); } internal TData Get(TKey id, bool forLoadingFromHash) { if(!this.storage.ContainsKey(id)) { lock(this.locks) { if(!this.storage.ContainsKey(id)) { TData obj = new TData(); obj.CreateByIdFromRegistry(id, forLoadingFromHash); this.storage[id] = obj; } } } lock(this.locks) { if(this.storage.ContainsKey(id)) { return this.storage[id]; } } return this.Get(id, forLoadingFromHash); } public bool IsCached(TKey id) { return this.storage.ContainsKey(id); } internal void Clear() { lock(this.locks) { this.storage.Clear(); } } internal void Delete(TKey[] idsToDelete) { lock(this.locks) { foreach(TKey id in idsToDelete) { IDataObject obj = null; if(this.storage.ContainsKey(id)) { obj = this.storage[id]; this.storage.Remove(id); obj.markAsDeletedFromRegistry(); } } } } } }