Esempio n. 1
0
    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());
        }
        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);
        }
        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));
        }
Esempio n. 5
0
 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));
 }
Esempio n. 6
0
 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));
 }