protected override void PersistUpdatedItem(IDictionaryItem entity) { entity.UpdatingEntity(); foreach (var translation in entity.Translations) { translation.Value = translation.Value.ToValidXmlString(); } var dto = DictionaryItemFactory.BuildDto(entity); Database.Update(dto); foreach (var translation in entity.Translations) { var textDto = DictionaryTranslationFactory.BuildDto(translation, entity.Key); if (translation.HasIdentity) { Database.Update(textDto); } else { translation.Id = Convert.ToInt32(Database.Insert(textDto)); translation.Key = entity.Key; } } entity.ResetDirtyProperties(); //Clear the cache entries that exist by uniqueid/item key IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, string>(entity.ItemKey)); IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, Guid>(entity.Key)); }
/// <summary> /// Gets a specific PreValue by its Id /// </summary> /// <param name="preValueId">Id of the PreValue to retrieve the value from</param> /// <returns>PreValue as a string</returns> public string GetPreValueAsString(int preValueId) { var collections = IsolatedCache.GetCacheItemsByKeySearch <PreValueCollection>(CacheKeys.DataTypePreValuesCacheKey + "_"); var preValue = collections.SelectMany(x => x.FormatAsDictionary().Values).FirstOrDefault(x => x.Id == preValueId); if (preValue != null) { return(preValue.Value); } var dto = Database.FirstOrDefault <DataTypePreValueDto>("WHERE id = @preValueId", new { preValueId }); if (dto == null) { return(string.Empty); } var collection = GetCachedPreValueCollection(dto.DataTypeNodeId); if (collection == null) { return(string.Empty); } preValue = collection.FormatAsDictionary().Values.FirstOrDefault(x => x.Id == preValueId); return(preValue == null ? string.Empty : preValue.Value); }
public IUserGroup?Get(string alias) { try { // need to do a simple query to get the id - put this cache var id = IsolatedCache.GetCacheItem(GetByAliasCacheKey(alias), () => { var groupId = Database.ExecuteScalar <int?>("SELECT id FROM umbracoUserGroup WHERE userGroupAlias=@alias", new { alias }); if (groupId.HasValue == false) { throw new InvalidOperationException("No group found with alias " + alias); } return(groupId.Value); }); // return from the normal method which will cache return(Get(id)); } catch (InvalidOperationException) { // if this is caught it's because we threw this in the caching method return(null); } }
/// <summary> /// This is the underlying method that processes most queries for this repository /// </summary> /// <param name="sqlFull"> /// The full SQL to select all member data /// </param> /// <param name="pagingSqlQuery"> /// The Id SQL to just return all member ids - used to process the properties for the member item /// </param> /// <param name="withCache"></param> /// <returns></returns> private IEnumerable <IMember> ProcessQuery(Sql sqlFull, PagingSqlQuery pagingSqlQuery, bool withCache = false) { // fetch returns a list so it's ok to iterate it in this method var dtos = Database.Fetch <MemberDto, ContentVersionDto, ContentDto, NodeDto>(sqlFull); //This is a tuple list identifying if the content item came from the cache or not var content = new List <Tuple <IMember, bool> >(); var defs = new DocumentDefinitionCollection(); foreach (var dto in dtos) { // if the cache contains the item, use it if (withCache) { var cached = IsolatedCache.GetCacheItem <IMember>(GetCacheIdKey <IMember>(dto.NodeId)); //only use this cached version if the dto returned is the same version - this is just a safety check, members dont //store different versions, but just in case someone corrupts some data we'll double check to be sure. if (cached != null && cached.Version == dto.ContentVersionDto.VersionId) { content.Add(new Tuple <IMember, bool>(cached, true)); continue; } } // else, need to fetch from the database // content type repository is full-cache so OK to get each one independently var contentType = _memberTypeRepository.Get(dto.ContentVersionDto.ContentDto.ContentTypeId); // need properties if (defs.AddOrUpdate(new DocumentDefinition(dto.ContentVersionDto, contentType))) { content.Add(new Tuple <IMember, bool>(MemberFactory.BuildEntity(dto, contentType), false)); } } // load all properties for all documents from database in 1 query var propertyData = GetPropertyCollection(pagingSqlQuery, defs); // assign property data foreach (var contentItem in content) { var cc = contentItem.Item1; var fromCache = contentItem.Item2; //if this has come from cache, we do not need to build up it's structure if (fromCache) { continue; } cc.Properties = propertyData[cc.Version]; //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 cc.ResetDirtyProperties(false); } return(content.Select(x => x.Item1).ToArray()); }
protected override void PersistDeletedItem(ILanguage entity) { base.PersistDeletedItem(entity); //Clear the cache entries that exist by key/iso IsolatedCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.IsoCode)); IsolatedCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.CultureName)); }
private IEnumerable <IMember> MapDtosToContent(List <MemberDto> dtos, bool withCache = false) { var temps = new List <TempContent <Member> >(); var contentTypes = new Dictionary <int, IMemberType?>(); var content = new Member[dtos.Count]; for (var i = 0; i < dtos.Count; i++) { MemberDto dto = dtos[i]; if (withCache) { // if the cache contains the (proper version of the) item, use it IMember?cached = IsolatedCache.GetCacheItem <IMember>(RepositoryCacheKeys.GetKey <IMember, int>(dto.NodeId)); if (cached != null && cached.VersionId == dto.ContentVersionDto.Id) { content[i] = (Member)cached; continue; } } // else, need to build it // get the content type - the repository is full cache *but* still deep-clones // whatever comes out of it, so use our own local index here to avoid this var contentTypeId = dto.ContentDto.ContentTypeId; if (contentTypes.TryGetValue(contentTypeId, out IMemberType? contentType) == false) { contentTypes[contentTypeId] = contentType = _memberTypeRepository.Get(contentTypeId); } Member c = content[i] = ContentBaseFactory.BuildEntity(dto, contentType); // need properties var versionId = dto.ContentVersionDto.Id; temps.Add(new TempContent <Member>(dto.NodeId, versionId, 0, contentType, c)); } // load all properties for all documents from database in 1 query - indexed by version id IDictionary <int, PropertyCollection> properties = GetPropertyCollections(temps); // assign properties foreach (TempContent <Member> temp in temps) { if (temp.Content is not null) { temp.Content.Properties = properties[temp.VersionId]; // reset dirty initial properties (U4-1946) temp.Content.ResetDirtyProperties(false); } } return(content); }
/// <inheritdoc /> protected override void PersistUpdatedItem(IConsent entity) { ((EntityBase)entity).UpdatingEntity(); var dto = ConsentFactory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); IsolatedCache.ClearCacheItem(RepositoryCacheKeys.GetKey <IConsent>(entity.Id)); }
private PreValueCollection GetCachedPreValueCollection(int datetypeId) { var key = GetPrefixedCacheKey(datetypeId); return(IsolatedCache.GetCacheItem <PreValueCollection>(key, () => { var dtos = Database.Fetch <DataTypePreValueDto>("WHERE datatypeNodeId = @Id", new { Id = datetypeId }); var list = dtos.Select(x => new Tuple <PreValue, string, int>(new PreValue(x.Id, x.Value, x.SortOrder), x.Alias, x.SortOrder)).ToList(); var collection = PreValueConverter.ConvertToPreValuesCollection(list); return collection; }, TimeSpan.FromMinutes(20), isSliding: true)); }
protected override void PersistDeletedItem(IDictionaryItem entity) { RecursiveDelete(entity.Key); Database.Delete <LanguageTextDto>("WHERE UniqueId = @Id", new { Id = entity.Key }); Database.Delete <DictionaryDto>("WHERE id = @Id", new { Id = entity.Key }); //Clear the cache entries that exist by uniqueid/item key IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, string>(entity.ItemKey)); IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, Guid>(entity.Key)); entity.DeleteDate = DateTime.Now; }
protected override void PersistUpdatedItem(IDataTypeDefinition entity) { entity.Name = EnsureUniqueNodeName(entity.Name, entity.Id); //Cannot change to a duplicate alias var exists = Database.ExecuteScalar <int>(@"SELECT COUNT(*) FROM cmsDataType INNER JOIN umbracoNode ON cmsDataType.nodeId = umbracoNode.id WHERE umbracoNode." + SqlSyntax.GetQuotedColumnName("text") + @"= @name AND umbracoNode.id <> @id", new { id = entity.Id, name = entity.Name }); if (exists > 0) { throw new DuplicateNameException("A data type with the name " + entity.Name + " already exists"); } //Updates Modified date ((DataTypeDefinition)entity).UpdatingEntity(); //Look up parent to get and set the correct Path if ParentId has changed if (entity.IsPropertyDirty("ParentId")) { var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId }); entity.Path = string.Concat(parent.Path, ",", entity.Id); entity.Level = parent.Level + 1; var maxSortOrder = Database.ExecuteScalar <int>( "SELECT coalesce(max(sortOrder),0) FROM umbracoNode WHERE parentid = @ParentId AND nodeObjectType = @NodeObjectType", new { ParentId = entity.ParentId, NodeObjectType = NodeObjectTypeId }); entity.SortOrder = maxSortOrder + 1; } var factory = new DataTypeDefinitionFactory(NodeObjectTypeId); //Look up DataTypeDefinition entry to get Primary for updating the DTO var dataTypeDto = Database.SingleOrDefault <DataTypeDto>("WHERE nodeId = @Id", new { Id = entity.Id }); factory.SetPrimaryKey(dataTypeDto.PrimaryKey); var dto = factory.BuildDto(entity); //Updates the (base) node data - umbracoNode var nodeDto = dto.NodeDto; Database.Update(nodeDto); Database.Update(dto); //NOTE: This is a special case, we need to clear the custom cache for pre-values here so they are not stale if devs // are querying for them in the Saved event (before the distributed call cache is clearing it) IsolatedCache.ClearCacheItem(GetPrefixedCacheKey(entity.Id)); entity.ResetDirtyProperties(); }
public IMemberGroup GetByName(string name) { return(IsolatedCache.GetCacheItem <IMemberGroup>( typeof(IMemberGroup).FullName + "." + name, () => { var qry = Query <IMemberGroup>().Where(group => group.Name.Equals(name)); var result = Get(qry); return result.FirstOrDefault(); }, //cache for 5 mins since that is the default in the Runtime app cache TimeSpan.FromMinutes(5), //sliding is true true)); }
public IMemberGroup GetByName(string name) { return(IsolatedCache.GetCacheItem <IMemberGroup>( string.Format("{0}.{1}", typeof(IMemberGroup).FullName, name), () => { var qry = new Query <IMemberGroup>().Where(group => group.Name.Equals(name)); var result = GetByQuery(qry); return result.FirstOrDefault(); }, //cache for 5 mins since that is the default in the RuntimeCacheProvider TimeSpan.FromMinutes(5), //sliding is true true)); }
protected override void PersistUpdatedItem(ILanguage entity) { ((Entity)entity).UpdatingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); //Clear the cache entries that exist by key/iso IsolatedCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.IsoCode)); IsolatedCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.CultureName)); }
private void RecursiveDelete(Guid parentId) { var list = Database.Fetch <DictionaryDto>("WHERE parent = @ParentId", new { ParentId = parentId }); foreach (var dto in list) { RecursiveDelete(dto.UniqueId); Database.Delete <LanguageTextDto>("WHERE UniqueId = @Id", new { Id = dto.UniqueId }); Database.Delete <DictionaryDto>("WHERE id = @Id", new { Id = dto.UniqueId }); //Clear the cache entries that exist by uniqueid/item key IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, string>(dto.Key)); IsolatedCache.Clear(RepositoryCacheKeys.GetKey <IDictionaryItem, Guid>(dto.UniqueId)); } }