/// <summary> /// Salva o registro informado no cache. Caso já exista, o mesmo será substituído. /// </summary> /// <param name="cache">Instancia do cache onde o registro será salvo.</param> /// <param name="keyFactory">Factory usado para criar as chave do registro no cache.</param> /// <param name="typeName">Nome do tipo que o registro representa.</param> /// <param name="record">Registro com os dados que serão salvos.</param> /// <param name="isUpdated">Identifica se ocorreu uma atualização.</param> /// <returns></returns> public static ulong Save(Cache cache, IRecordKeyFactory keyFactory, Colosoft.Reflection.TypeName typeName, CacheItemRecord record, out bool isUpdated) { var keyGenerator = keyFactory.CreateGenerator(typeName); var key = keyGenerator.GetKey(record); var operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); TypeInfoMap typeInfoMap = cache.GetTypeInfoMap(); var handleId = typeInfoMap.GetHandleId(typeName); isUpdated = false; if (typeInfoMap.GetAttribList(handleId).Count == 0) { return(0); } if (cache.Contains(key, operationContext)) { cache.Remove(key, operationContext); isUpdated = true; } var queryInfo = GetCacheEntryQueryInfo(cache, typeInfoMap, record); var result = cache.Insert(key, record, null, null, new Colosoft.Caching.Policies.PriorityEvictionHint(CacheItemPriority.Normal), null, null, queryInfo, new BitSet(), null, 0, LockAccessType.IGNORE_LOCK, null, null, operationContext); return(result); }
/// <summary> /// Recupera as informações de consulta para a entrada do cache /// </summary> /// <param name="cache">Instancia do cache.</param> /// <param name="typeInfoMap"></param> /// <param name="record"></param> /// <returns></returns> private static System.Collections.Hashtable GetCacheEntryQueryInfo(Cache cache, TypeInfoMap typeInfoMap, CacheItemRecord record) { var queryInfo = new System.Collections.Hashtable(); if (typeInfoMap != null) { queryInfo["query-info"] = Colosoft.Caching.Loaders.CacheLoaderUtil.GetQueryInfo(record, typeInfoMap); } queryInfo["tag-info"] = Colosoft.Caching.Loaders.CacheLoaderUtil.GetTagInfo(record, new Tag[0]); var hashtable2 = Colosoft.Caching.Loaders.CacheLoaderUtil.GetNamedTagsInfo(record, new NamedTagsDictionary(), typeInfoMap); if (hashtable2 != null) { queryInfo["named-tag-info"] = hashtable2; } return(queryInfo); }
/// <summary> /// Executa a ação para atualizar os dados do registro no cache. /// </summary> /// <param name="action"></param> /// <returns></returns> private PersistenceActionResult ExecuteUpdateAction(PersistenceAction action) { var typeName = new Reflection.TypeName(action.EntityFullName); var typeMetadata = _typeSchema.GetTypeMetadata(action.EntityFullName); if (typeMetadata == null) { return new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_TypeMetadataNotFound, action.EntityFullName).Format(), } } ; var query = GetActionQuery(action, typeMetadata).CreateQueryInfo(); Queries.QueryResultSet queryResult = null; try { queryResult = ((CacheDataSource)_sourceContext.DataSource).ExecuteInCache(query); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_ExecuteQueryInCacheError, Colosoft.Diagnostics.ExceptionFormatter.FormatException(ex, true)).Format() }); } Colosoft.Query.Record.RecordDescriptor recordDescriptor = null; try { recordDescriptor = new Colosoft.Query.Record.RecordDescriptor(action.EntityFullName, GetRecordFields(action.EntityFullName, typeMetadata)); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } var keyGenerator = _keyFactory.Value.CreateGenerator(typeName); var typeInfoMap = Cache.GetTypeInfoMap(); var keysResult = queryResult.SearchKeysResult.ToArray(); if (keysResult.Length > 0) { foreach (var key in keysResult) { var record = CacheDataSource.CreateRecord(Cache, key, ref recordDescriptor, query); var recordValues = new object[recordDescriptor.Count]; record.GetValues(recordValues); foreach (var actionParameter in action.Parameters) { var indexOf = 0; for (; indexOf < recordDescriptor.Count; indexOf++) { if (StringComparer.InvariantCultureIgnoreCase.Equals(recordDescriptor[indexOf].Name, actionParameter.Name)) { break; } } if (indexOf < recordDescriptor.Count) { recordValues[indexOf] = actionParameter.Value; } } var newRecord = new CacheItemRecord(new Reflection.TypeName(action.EntityFullName), recordValues, recordDescriptor); var newKey = keyGenerator.GetKey(newRecord); try { var queryInfo = GetCacheEntryQueryInfo(Cache, typeInfoMap, newRecord); Cache.Remove(key, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); var insertResult = Cache.Insert(newKey, newRecord, null, null, new Colosoft.Caching.Policies.PriorityEvictionHint(CacheItemPriority.Normal), null, null, queryInfo, new BitSet(), null, 0, LockAccessType.IGNORE_LOCK, null, null, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } } } else { } return(new PersistenceActionResult { Success = true }); }
/// <summary> /// Executa a ação para inserir um novo registro no cache. /// </summary> /// <param name="action"></param> /// <returns></returns> private PersistenceActionResult ExecuteInsertAction(PersistenceAction action) { var typeMetadata = _typeSchema.GetTypeMetadata(action.EntityFullName); if (typeMetadata == null) { return new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_TypeMetadataNotFound, action.EntityFullName).Format(), } } ; IEnumerable <Colosoft.Query.Record.Field> recordFields = null; try { recordFields = GetRecordFields(action.EntityFullName, typeMetadata); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } var recordDescriptor = new Colosoft.Query.Record.RecordDescriptor(action.EntityFullName, recordFields); var recordValues = new object[recordDescriptor.Count]; foreach (var actionParameter in action.Parameters) { var indexOf = 0; for (; indexOf < recordDescriptor.Count; indexOf++) { if (StringComparer.InvariantCultureIgnoreCase.Equals(recordDescriptor[indexOf].Name, actionParameter.Name)) { break; } } if (indexOf < recordDescriptor.Count) { recordValues[indexOf] = actionParameter.Value; } else { return new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_PropertyMetadataNotFound, actionParameter.Name, action.EntityFullName).Format() } }; } var record = new CacheItemRecord(new Reflection.TypeName(action.EntityFullName), recordValues, recordDescriptor); var typeName = new Reflection.TypeName(action.EntityFullName); var keyGenerator = _keyFactory.Value.CreateGenerator(typeName); var key = keyGenerator.GetKey(record); var queryInfo = GetCacheEntryQueryInfo(Cache, Cache.GetTypeInfoMap(), record); var insertResult = Cache.Insert(key, record, null, null, new Colosoft.Caching.Policies.PriorityEvictionHint(CacheItemPriority.Normal), null, null, queryInfo, new BitSet(), null, 0, LockAccessType.IGNORE_LOCK, null, null, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); return(new PersistenceActionResult { Success = true }); }