Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Mailer 6 posts 26 karma points
    Dec 08, 2011 @ 20:27
    Mark Mailer
    0

    Razor image gallery - Very slow!

    Hi all,

     

    Just started playing around with Umbraco, and looks like I've managed to pick the right time as everything is moving over to Razor (which makes much more sense to me ;) ). Great product, once you get your head around the concept, loving how open the framework is!

    Anyway, I've setup a image gallery (with popup's using Lightbox), and I'm looping through the media structure, first to get the folders and then the child items once a folder has been selected.

    Once it get's to the images part, there's a folder with about 100 photo's in, with their respective thumbnails and it's really slow looping through all the nodes, much slower than using IO.Directory.GetFiles methods.

    I'm using 4.7.1 with a SQLCE database, running on a dev server (2008 R2 - IIS7.5 ASP.NET 4.0).

    What's the best practise? To use the media structure? Or for this, upload to a static folder, and just read that with the File System Object / IO namespace? I'm still not 100% clear of where to write macro's and code and where to just hardcode values into the master pages *confused*

    My code is below, anyone feel free to take it and do with it what you will ;) P.S. It's in VB, I'm bilingual but just old habits with VB!

    Cheers,
    Mark

    ========================================================

    @imports umbraco.MacroEngines
    @Code

    If String.IsNullOrWhiteSpace(HttpContext.Current.Request.QueryString("f")) Then


    For Each i In Model.NodeById(1147).Children

    '@i.Name
    '@i.NodeTypeAlias
    Dim lnk = Model.NiceUrl & "?f=" & i.Id

    @<a href="@lnk" class="lnkShow" style="display:inline-block;border-width:1px;border-style:solid;font-weight:bold;">@i.Name</a>

    Next

    Else

    @<a href="@Model.NiceUrl">Return to Gallery listings</a>
    @<hr />

    For Each i In Model.nodeById(Request.QueryString("f")).Children

    If Model.NodeById(i.Id).NodeTypeAlias = "Image" Then
    Dim f as String = Model.MediaById(i.Id).umbracoFile.ToString.Replace(".jpg","_thumb.jpg")
    Dim l As String = Model.MediaById(i.Id).umbracoFile


    If f.IndexOf("DoF_") > -1 Then
    If DoFhr = False Then
    @<h4>Dan O'Flynn Photography <span><a class='smallLinksBlack' href=""http://www.danoflynnphotography.com/"" target=""_blank"">http://www.danoflynnphotography.com/</a></span></h4>;
    End If
    DoFhr = True
    Else
    If other = False Then
    @<h4>Other</h4>
    End If
    other = True
    End If

    Dim t = Model.MediaById(i.Id).NodeTypeAlias

    @<a class="lnkShow lnkLB" href="@l" style="display:inline-block;border-width:1px;border-style:solid;"><img title="Filename: @f" src="@f" alt="Filename: @f" style="border-width:0px;" /></a>

    End If
    Next
    End If
    End Code

     

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 09, 2011 @ 10:55
    Dave Woestenborghs
    0

    Hi Mark,

    The folder 1147, is that a media node or a content node ?

     

    Dave

  • Mark Mailer 6 posts 26 karma points
    Dec 09, 2011 @ 18:00
    Mark Mailer
    0

    Hi Dave,

    Sorry should have said! Node 1147 is a media noe, but type folder.

    The children are then folder media nodes as well, and their children a image media nodes... It just seems the database looping is a much slower process than just listing a folders contents with the IO...???

    Cheers,
    Mark

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 10, 2011 @ 12:57
  • Mark Mailer 6 posts 26 karma points
    Dec 12, 2011 @ 08:41
    Mark Mailer
    0

    So that's part of the uComponents package? I'll take a look, thank you.

    I'm going to get stuck in today, so should have an update later :)

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 12, 2011 @ 09:43
    Dave Woestenborghs
    0

    Hi Mark,

    The code snippet is not using uComponents. 

  • Mark Mailer 6 posts 26 karma points
    Dec 12, 2011 @ 10:25
    Mark Mailer
    0

    Hi Dave,

    Thanks again for that, just had a reread of the thread and saw the quantum difference! To grab the whole node as a variable in the first instance, and just use that the whole time without any further lookups! Like this it's massively quicker!!

    Hopefully the revised code below (in C# this time) will be of some use, and as I add more, I'll update the code to reflect dynamic folder entry points (or a media picker parameter) as I'm sure I'll keep adding to it... But for now, I hope it gives someone a good start!

    Cheers,
    Mark

    @using umbraco.MacroEngines;
    @{

        string folder = Request.QueryString["f"];
        if (string.IsNullOrWhiteSpace(folder))
        {
            // There's no folder listed in the QueryString so list the sub folders

            foreach (var subFolder in Model.NodeById(1147).Children)
            {
                string lnk = Model.NiceUrl + "?f=" + subFolder.Id;
                <a href="@lnk" class="lnkShow" style="display:inline-block;border-width:1px;border-style:solid;font-weight:bold;">@subFolder.Name</a>
            }
              
        } else {
           
            // We have a folder - Let's load the images
           
            // Set the header and page vars
            <a href="@Model.NiceUrl">Return to Gallery listings</a>
            <hr />
       
            var DoFhr = false;
            var other = false;


            var galleryImages = Model.NodeById(folder).Children;
           
            foreach(var img in galleryImages) {
                if(img.NodeTypeAlias == "Image") {

                string f = img.umbracoFile.Replace(".jpg", "_thumb.jpg");
                string l = img.umbracoFile;
                   
                    if(f.IndexOf("DoF_") > -1) {
                        if(DoFhr == false) {
                            <h4>Dan O'Flynn Photography <span><a class="smallLinksBlack" href="http://www.danoflynnphotography.com/" target="_blank">http://www.danoflynnphotography.com/</a></span></h4>;
                            DoFhr = true;
                        }                   
                    } else {
                        if(other == false) {
                            <h4>Other</h4>
                            other = true;   
                        }
                    }
                   
                              
               
                <a class="lnkShow lnkLB" href="@l" style="display:inline-block;border-width:1px;border-style:solid;"><img title="Filename: @f" src="@f" alt="Filename: @f" style="border-width:0px;" /></a>
                }
            }
        }
       
    }

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 12, 2011 @ 10:35
    Dave Woestenborghs
    0

    Hi Mark,

    In your if section you still have  Model.NodeById(1147).Children in the foreach loop

    This can be stored in a variable too.

     

    Dave

  • Mark Mailer 6 posts 26 karma points
    Dec 12, 2011 @ 10:50
    Mark Mailer
    0

    Very true! Being lazy it's OK as it's just for the folders but as it grows it'll become a problem ;)

    Saying that, I think these should have the same performance:

    Option 1:
    var vegetables = 123; // Node Id
    foreach(var spuds in Model.NodeById(vegetables).Children) {
        // Do stuff with the var "spuds" - All the children are stored in spuds and the parent "NodeById" will not be reference again?
    }

    Option 2:
    var vegetables = Model.NodeById(123);
    foreach(var spuds in vegetables.Children) {
       // Do stuff with var "spuds" - All the children are stored in spuds and the parent "vegetables" will not be reference again?
    }

    Do you think these would execute ultimately the same? Of couse, happy to be corrected if I'm wrong, but I think they would be the same and only have the one database query, either on line 1 or line 2...???

    Cheers,
    Mark

    P.S. Just to say, not being pedantic, just curious as it's nice to keep lines to a minimum and not have a var for every little thing ;)

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 12, 2011 @ 10:53
    Dave Woestenborghs
    0

    I think best practice would even to store the children collection in a variable, because we don't know what's going on under the hood.

    So i would go for option 3

    var vegetables = Model.NodeById(123).Children

    foreach(var spuds in vegetables)
    {

    }

     

  • Mark Mailer 6 posts 26 karma points
    Dec 12, 2011 @ 11:17
    Mark Mailer
    0

    Very true about what's happening under the hood... High five for that one :D

    I'll stick to that idea for all future developments!

    I'm just uploading now, so the Umbraco version of http://www.londoneastaikikai.co.uk/ should be online shortly! This is a site I run but not high demand etc so a good one to get to grips with the CMS... I think I'm starting to "get it", so will no doubt revisit this site again in the future ;)

Please Sign in or register to post replies

Write your reply to:

Draft