Skip to main content

Posts

Showing posts from September, 2023

String Interning in Sitecore

I am always looking at different ways of optimizing application resources in Sitecore. After all, low resources means low cost!! I came across the concept of String Interning and I got interested into it. Next, I was thinking how can I make use of it in my Sitecore instances. I started digging little bit and I found Sitecore is smart enough to already make use of it. I will share some of the findings in this post. What is String Interning?  In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.  What this means is if you have a string object str1 with value "Hello" and then if your code initializes another string object str2 with same value "Hello", string interning will prevent creation of second object for str2 as String Constants Pool already has a string object with same value. It will assign the reference of str1 to str2 and str2 will reuse the object that was created for str1. This obv

Sitecore CD servers triggering index update jobs?

The Problem Recently, I was checking my CD server logs for an issue that was reported and stumbled across some log entries which mentioned about index update jobs getting triggered as in snapshot below- This made me think that should these jobs actually get triggered from CDs?? The answer is NO!  Why?? Because web indexes already get updated from CM servers during item publish events etc. (if the indexes are already maintained by the CM instance). Doing the same from CD servers is redundant and increasing unwanted load of index update jobs on CD servers. Also, CD servers are only supposed to READ from indexes and not WRITE to them (in most cases) Then why are these jobs getting created? I started looking into my index configurations on CD servers and from what I could see, all the web indexes in our case on the CD instances are configured with  "onPublishEndAsync", "remoteRebuild" or "rebuildAfterFullPublish" strategies. As per Sitecore best practice, if

Script to read contents of media files attached to Sitecore media library items

Ever wondered how can we read contents of files uploaded to media library without rendering them? Recently, we needed a PowerShell script to read the content of our SVGs and create a list of SVGs which contained a pattern in them. After a little research, I came up with this script -  $item =Get-Item -Path "/sitecore/media library/<filepath here>" $mediaItem = New-Object 'Sitecore.Data.Items.MediaItem' $item; $blobField = $mediaItem.InnerItem.Fields["blob"] if($blobField){         $blobStream = $blobField.GetBlobStream()         if($blobStream){             try                 {                 $contents = New-Object byte[] $blobStream.Length                 $blobStream.Read($contents, 0, $blobStream.Length) | Out-Null             }              finally              {                     $blobStream.Close()                 }             $fileText = ([System.Text.Encoding]::Default.GetString($contents))         } } In the above script, $fileText will co