private async Task <T> GetCacheField <T>(Nuid id, string field_name) { if (!await CacheExist(id)) { return(default(T)); } IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildFields(id); return(await db.HashGetAsync <T>(key, field_name)); }
private async Task <Entity> GetCacheEntity(Nuid entity_id) { try { IRedisDatabase db = GetCache(entity_id); ITransaction query_trans = db.Database.CreateTransaction(); string type = await GetCacheType(entity_id); EntityTransaction trans = new EntityTransaction(entity_id, type); EntityPrefab entity_prefab = Prefabs.GetEntity(trans.Type); if (entity_prefab == null) { throw new Exception($"Prefabs.GetEntity cant found {trans.Type}"); } string field_key = CacheUtils.BuildFields(trans.Id); trans.Fields = query_trans.HashGetAllAsync(field_key); foreach (Table table in trans.Entity.GetTables()) { TablePrefab table_prefab = entity_prefab.tables[table.GetName()]; string table_key = CacheUtils.BuildTable(trans.Id, table.GetName()); Task <HashEntry[]> key_values = query_trans.HashGetAllAsync(table_key); trans.AddTableTrans(table.GetName(), key_values); } bool redis_execute = await query_trans.ExecuteAsync(); if (!redis_execute) { throw new Exception("query_trans ExecuteAsync ERROR!!"); } return(await BuildCacheEntity(trans)); } catch (Exception ex) { _Logger.LogError(ex, "GetCacheEntities Failed"); } return(null); }
private async Task <bool> SetCacheField <T>(Nuid id, string field_name, T field_value) { if (!await CacheExist(id)) { return(false); } bool result = false; try { IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildFields(id); result = await db.HashSetAsync(key, field_name, field_value); } catch (Exception ex) { _Logger.LogError(ex, "'{0} SetCacheField error for field={1} value={2}", id, field_name, field_value); } return(result); }
private async Task BatchCache(object arg) { if (BatchCahceList.Count <= 0) { return; } try { int db = (int)(Identity % CacheUtils.EntityDBs); IRedisDatabase redis = _CacheClient.GetDb(db); ITransaction trans = redis.Database.CreateTransaction(); foreach (NList batch in BatchCahceList) { CacheOption option = (CacheOption)batch.Get <int>(0); Nuid entity_id = batch.Get <Nuid>(1); switch (option) { case CacheOption.SetEntity: { string entity_type = batch.Get <string>(2); string key = CacheUtils.BuildEntities(entity_id); Task task = trans.HashSetAsync(key, entity_id.Unique.ToString(), entity_type); } break; case CacheOption.DelEntity: { string entity_type = batch.Get <string>(2); EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type); if (entity_prefab == null) { continue; } foreach (TablePrefab table_prefab in entity_prefab.tables.Values) { string table_key = CacheUtils.BuildTable(entity_id, table_prefab.name); Task table_task = trans.KeyDeleteAsync(table_key); } string field_key = CacheUtils.BuildFields(entity_id); Task field_task = trans.KeyDeleteAsync(field_key); string entity_key = CacheUtils.BuildEntities(entity_id); Task entity_task = trans.HashDeleteAsync(entity_key, entity_id.Unique.ToString()); } break; case CacheOption.SetField: { string field_name = batch.Get <string>(2); byte[] field_value = batch.Get <byte[]>(3); string key = CacheUtils.BuildFields(entity_id); Task task = trans.HashSetAsync(key, field_name, field_value); } break; case CacheOption.SetRow: { string table_name = batch.Get <string>(2); long row = batch.Get <long>(3); NList row_value = batch.Get <NList>(4); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.HashSetAsync(key, row, ProtoUtils.Serialize(row_value)); } break; case CacheOption.DelRow: { string table_name = batch.Get <string>(2); long row = batch.Get <long>(3); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.HashDeleteAsync(key, row); } break; case CacheOption.ClearTable: { string table_name = batch.Get <string>(2); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.KeyDeleteAsync(key); } break; default: break; } } bool result = await trans.ExecuteAsync(); if (result) { BatchCahceList.Clear(); } else { throw new Exception(); } } catch (Exception ex) { _Logger.LogError(ex, string.Format("{0} BatchCache ExecuteAsync Failed", Identity)); } }
public async Task Destroy(Nuid id) { if (id.Origin != Identity) { INode node = GrainFactory.GetGrain <INode>(id.Origin); if (await node.IsActive()) { await node.Destroy(id); return; } } if (NodeType == NodeType.Grain) { Entity entity = EntityManager.Get(id); if (entity == null) { _Logger.LogError($"{id} Destroy Entity Failed When not found!"); return; } if (EntityManager.Remove(id)) { _Logger.LogError($"{id} Destroy Entity Failed When EntityManager Remove!"); return; } } else if (NodeType == NodeType.Cache) { if (!await CacheExist(id)) { _Logger.LogError($"{id} Destroy Entity Failed when not CacheExist!"); return; } string entity_type = await GetCacheType(id); EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type); if (entity_prefab == null) { _Logger.LogError($"{id} Destroy Entity Failed when not EntityPrefab {entity_type}!"); return; } int db = (int)(Identity % CacheUtils.EntityDBs); IRedisDatabase redis = _CacheClient.GetDb(db); ITransaction trans = redis.Database.CreateTransaction(); foreach (TablePrefab table_prefab in entity_prefab.tables.Values) { string table_key = CacheUtils.BuildTable(id, table_prefab.name); Task table_task = trans.KeyDeleteAsync(table_key); } string field_key = CacheUtils.BuildFields(id); Task field_task = trans.KeyDeleteAsync(field_key); string entity_key = CacheUtils.BuildEntities(id); Task entity_task = trans.HashDeleteAsync(entity_key, id.Unique); bool result = await trans.ExecuteAsync(); if (!result) { _Logger.LogError($"{id} Destroy Entity Failed when ITransaction Execute"); return; } } await CallbackEntity(id, EntityEvent.OnDestroy, NList.Empty); await SyncEntity(id, NList.New().Add(id).Add((int)EntityEvent.OnDestroy)); }
private async Task SetCacheEntities(IReadOnlyList <Entity> entities) { IRedisDatabase db = GetCache(); ITransaction trans = db.Database.CreateTransaction(); { string key = CacheUtils.BuildEntities(Nuid.New(Identity, Identity)); HashEntry[] hashFields = new HashEntry[entities.Count]; for (int i = 0; i < entities.Count; i++) { hashFields[i] = new HashEntry(entities[i].Id.Unique, entities[i].Type); } Task _ = trans.HashSetAsync(key, hashFields); } { foreach (Entity entity in entities) { EntityPrefab entity_prefab = Prefabs.GetEntity(entity.Type); if (entity_prefab == null) { continue; } string fields_key = CacheUtils.BuildFields(entity.Id); Field[] fields = entity.GetFields(); List <HashEntry> cache_fields = new List <HashEntry>(); for (int i = 0; i < fields.Length; i++) { FieldPrefab field_prefab = entity_prefab.fields[fields[i].Name]; if (field_prefab == null) { continue; } string field_value = ""; switch (field_prefab.type) { case VarType.Bool: field_value = JsonUtils.ToJson(fields[i].Get <bool>()); break; case VarType.Int: field_value = JsonUtils.ToJson(fields[i].Get <int>()); break; case VarType.Long: field_value = JsonUtils.ToJson(fields[i].Get <long>()); break; case VarType.Float: field_value = JsonUtils.ToJson(fields[i].Get <float>()); break; case VarType.String: field_value = JsonUtils.ToJson(fields[i].Get <string>()); break; case VarType.Nuid: field_value = JsonUtils.ToJson(fields[i].Get <Nuid>()); break; case VarType.List: field_value = JsonUtils.ToJson(fields[i].Get <NList>()); break; default: break; } cache_fields.Add(new HashEntry(fields[i].Name, field_value)); } Task _ = trans.HashSetAsync(fields_key, cache_fields.ToArray()); Table[] tables = entity.GetTables(); foreach (Table table in tables) { string table_key = CacheUtils.BuildTable(entity.Id, table.GetName()); TablePrefab table_prefab = entity_prefab.tables[table.GetName()]; if (table_prefab == null) { continue; } List <HashEntry> cache_key_values = new List <HashEntry>(); void SetCacheKeyValue <TPrimaryKey>() { Table <TPrimaryKey> t = table as Table <TPrimaryKey>; IReadOnlyList <TPrimaryKey> keys = t.GetPrimaryKeys(); foreach (TPrimaryKey key in keys) { string json = JsonUtils.ToJson(t.GetKeyValue(key)); cache_key_values.Add(new HashEntry(key.ToString(), json)); } } switch (table_prefab.primary_key.type) { case VarType.Bool: SetCacheKeyValue <bool>(); break; case VarType.Int: SetCacheKeyValue <int>(); break; case VarType.Long: SetCacheKeyValue <long>(); break; case VarType.Float: SetCacheKeyValue <float>(); break; case VarType.String: SetCacheKeyValue <string>(); break; case VarType.Nuid: SetCacheKeyValue <Nuid>(); break; default: break; } Task __ = trans.HashSetAsync(table_key, cache_key_values.ToArray()); } } bool redis_execute = await trans.ExecuteAsync(); if (!redis_execute) { throw new Exception("trans ExecuteAsync ERROR!!"); } } }
private async Task <IReadOnlyList <Entity> > GetCacheEntities() { Nuid id = Nuid.New(Identity, Identity); IRedisDatabase db = GetCache(id); string entites_key = CacheUtils.BuildEntities(id); List <Entity> sorts = new List <Entity>(); List <EntityTransaction> entity_trans = new List <EntityTransaction>(); try { HashEntry[] entities = await db.Database.HashGetAllAsync(entites_key); foreach (HashEntry member in entities) { long child_id = long.Parse(member.Name); string type = member.Value; Nuid entity_id = Nuid.New(child_id, Identity); EntityTransaction trans = new EntityTransaction(entity_id, type); if (trans.Entity == null) { continue; } entity_trans.Add(trans); } ITransaction query_trans = db.Database.CreateTransaction(); foreach (EntityTransaction trans in entity_trans) { EntityPrefab entity_prefab = Prefabs.GetEntity(trans.Type); if (entity_prefab == null) { throw new Exception($"Prefabs.GetEntity cant found {trans.Type}"); } string field_key = CacheUtils.BuildFields(trans.Id); trans.Fields = query_trans.HashGetAllAsync(field_key); foreach (Table table in trans.Entity.GetTables()) { TablePrefab table_prefab = entity_prefab.tables[table.GetName()]; string table_key = CacheUtils.BuildTable(trans.Id, table.GetName()); Task <HashEntry[]> key_values = query_trans.HashGetAllAsync(table_key); trans.AddTableTrans(table.GetName(), key_values); } } bool redis_execute = await query_trans.ExecuteAsync(); if (!redis_execute) { throw new Exception("query_trans ExecuteAsync ERROR!!"); } foreach (EntityTransaction trans in entity_trans) { Entity entity = await BuildCacheEntity(trans); sorts.Add(entity); } } catch (Exception ex) { _Logger.LogError(ex, "GetCacheEntities Failed"); } entity_trans.Clear(); sorts.Sort((x, y) => { EntityPrefab entity_x = Prefabs.GetEntity(x.Type); EntityPrefab entity_y = Prefabs.GetEntity(y.Type); return(entity_y.priority - entity_x.priority); }); return(sorts); }