using System.Collections.Generic; using System.Threading.Tasks; namespace Model { [ObjectEvent] public class DBCacheComponentEvent : ObjectEvent, IAwake { public void Awake() { this.Get().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 = new DBTaskQueue(); this.tasks.Add(taskQueue); taskQueue.Start(); } } public Task Add(Entity entity, string collectionName = "") { TaskCompletionSource tcs = new TaskCompletionSource(); this.AddToCache(entity, collectionName); if (collectionName == "") { collectionName = entity.GetType().Name; } DBSaveTask task = new DBSaveTask(entity, collectionName, tcs); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); return tcs.Task; } public Task AddBatch(List entitys, string collectionName) { TaskCompletionSource tcs = new TaskCompletionSource(); DBSaveBatchTask task = new DBSaveBatchTask(entitys, collectionName, tcs); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); return tcs.Task; } public void AddToCache(Entity entity, string collectionName = "") { if (collectionName == "") { collectionName = entity.GetType().Name; } Dictionary collection; if (!this.cache.TryGetValue(collectionName, out collection)) { collection = new Dictionary(); this.cache.Add(collectionName, collection); } collection[entity.Id] = entity; } public Entity GetFromCache(string collectionName, long id) { Dictionary d; if (!this.cache.TryGetValue(collectionName, out d)) { return null; } Entity 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) { Entity entity = GetFromCache(collectionName, id); if (entity != null) { return Task.FromResult(entity); } TaskCompletionSource tcs = new TaskCompletionSource(); this.tasks[(int)((ulong)id % taskCount)].Add(new DBQueryTask(id, collectionName, tcs)); return tcs.Task; } public Task> GetBatch(string collectionName, List idList) { List entitys = new List(); bool isAllInCache = true; foreach (long id in idList) { Entity entity = this.GetFromCache(collectionName, id); if (entity == null) { isAllInCache = false; break; } entitys.Add(entity); } if (isAllInCache) { return Task.FromResult(entitys); } TaskCompletionSource> tcs = new TaskCompletionSource>(); DBQueryBatchTask dbQueryBatchTask = new DBQueryBatchTask(idList, collectionName, tcs); this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask); return tcs.Task; } public Task> GetJson(string collectionName, string json) { TaskCompletionSource> tcs = new TaskCompletionSource>(); DBQueryJsonTask dbQueryJsonTask = new DBQueryJsonTask(collectionName, json, tcs); this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask); return tcs.Task; } } }