using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; namespace ETModel { [ObjectSystem] public class DbCacheComponentSystem : AwakeSystem { public override void Awake(DBCacheComponent self) { self.Awake(); } } /// /// 用来缓存数据 /// public class DBCacheComponent : Component { public Dictionary> cache = new Dictionary>(); public const int taskCount = 32; public List tasks = new List(taskCount); public void Awake() { for (int i = 0; i < taskCount; ++i) { DBTaskQueue taskQueue = ComponentFactory.Create(); this.tasks.Add(taskQueue); } } public Task Add(ComponentWithId component, string collectionName = "") { TaskCompletionSource tcs = new TaskCompletionSource(); if (string.IsNullOrEmpty(collectionName)) { collectionName = component.GetType().Name; } DBSaveTask task = ComponentFactory.CreateWithId>(component.Id, component, collectionName, tcs); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); return tcs.Task; } public Task AddBatch(List components, string collectionName) { TaskCompletionSource tcs = new TaskCompletionSource(); DBSaveBatchTask task = ComponentFactory.Create, string, TaskCompletionSource>(components, collectionName, tcs); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); return tcs.Task; } public void AddToCache(ComponentWithId component, string collectionName = "") { if (string.IsNullOrEmpty(collectionName)) { collectionName = component.GetType().Name; } Dictionary collection; if (!this.cache.TryGetValue(collectionName, out collection)) { collection = new Dictionary(); this.cache.Add(collectionName, collection); } collection[component.Id] = component; } public ComponentWithId GetFromCache(string collectionName, long id) { Dictionary d; if (!this.cache.TryGetValue(collectionName, out d)) { return null; } ComponentWithId result; if (!d.TryGetValue(id, out result)) { return null; } return result; } public void RemoveFromCache(string collectionName, long id) { Dictionary d; if (!this.cache.TryGetValue(collectionName, out d)) { return; } d.Remove(id); } public Task Get(string collectionName, long id) { ComponentWithId component = GetFromCache(collectionName, id); if (component != null) { return Task.FromResult(component); } TaskCompletionSource tcs = new TaskCompletionSource(); DBQueryTask dbQueryTask = ComponentFactory.CreateWithId>(id, collectionName, tcs); this.tasks[(int)((ulong)id % taskCount)].Add(dbQueryTask); return tcs.Task; } public Task> GetBatch(string collectionName, List idList) { List components = new List(); bool isAllInCache = true; foreach (long id in idList) { ComponentWithId component = this.GetFromCache(collectionName, id); if (component == null) { isAllInCache = false; break; } components.Add(component); } if (isAllInCache) { return Task.FromResult(components); } TaskCompletionSource> tcs = new TaskCompletionSource>(); DBQueryBatchTask dbQueryBatchTask = ComponentFactory.Create, string, TaskCompletionSource>>(idList, collectionName, tcs); this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask); return tcs.Task; } public Task> Get(string collectionName, Expression> func) where T : ComponentWithId { var vistor = new ExpressionVistor(func); return GetJson(collectionName,vistor.Output); } public Task> GetJson(string collectionName, string json) { TaskCompletionSource> tcs = new TaskCompletionSource>(); DBQueryJsonTask dbQueryJsonTask = ComponentFactory.Create>>(collectionName, json, tcs); this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask); return tcs.Task; } } }