简体   繁体   中英

Disk Cache plugin with Sitecore

I have a new Sitecore 8.1 instance up and running. Sitecore loads images via a media handler with URLs like so: "/-/media/Images/FOLDER/IMAGENAME.jpg". The Sitecore Media Handler needs to run so that it can find the image in the Sitecore database and then return the actual image file (image does not exist on disk). I have never been able to get ImageResizer to process Sitecore images since Sitecore is going to steal the request as opposed to letting ImageResizer handle it. Makes sense.

I have implemented a custom pipeline in Sitecore that works really well however:

var settings = new ResizeSettings(HttpContext.Current.Request.QueryString);
ImageBuilder.Current.Build(args.OutputStream.Stream, stream, settings);
args.OutputStream = new MediaStream(stream, args.MediaData.Extension, args.MediaData.MediaItem);

It will then return the resized image to the client successfully. However, I have no idea how to enable disk cache so that it doesn't have to process every resizer request on-demand, nor have I found any resources for this issue.

Is there anything I can do to either avoid my custom pipeline and enable native ImageResizer so that I can use the DiskCache plugin with my scenario (Sitecore Media)? Or is there a way I can modify my pipeline to use the DiskCache class to rig up my own?

Thanks!

Why do you want to use your own imageresizer?

Sure it would be possible, but are you aware of the fact that the Sitecore mediahandler that you already utilize can resize the original uploaded images as well by adding querystring parameters? This solves your caching question and you do not need to build your own plugin.

So, I would strongly recommend you to use the default sitecore functionality for this, as sitecore will automatically keep the resized image on disk (as a disk cache which will greatly improve performance for any following request of the same image with the same parameter) and sitecore will recreate the cache if all these tempfiles are deleted (for example with a new installation/deployment)

You can resize your image by just adding parameters to your image url. So when the URL to your image is /-/media/Images/FOLDER/IMAGENAME.jpg you can set the width by adding aw parameter.
/-/media/Images/FOLDER/IMAGENAME.jpg?w=150 And the height by adding the h parameter.

These are the possible parameters:

  • w: Width in pixels
  • h: Height in pixels
  • mw: Maximum width in pixels
  • mh: Maximum height in pixels
  • la: Language (defaults to context language)
  • vs: Version (defaults to latest version)
  • db: Database name (defaults to context database)
  • bc: Background color (defaults to black)
  • as: Allow stretch (as=1)
  • sc: Scale by floating point number (sc=.25 = 25%)
  • thn: Thumbnail (thn=1)
  • dmc: Disable media caching, both retrieval and storage (dmc=1)

Note that there is one more thing to take into account. Before you start adding querystring values to your medialibrary images, you should be sure to find this configuration setting

      <setting name="Media.RequestProtection.Enabled" value="false">

The default value is true, which will mean that you need to add an additional hash to the querystring of every image before it will be resized. Setting it to false will allow you to experiment with the imageresizing and get familiar with it, but leaving this setting to false is not recommended on a production environment! This means that you will eventually need to set a hash value on every imagerequest for those images where you want to use resizing parameters. The way you can do that is quite simple. Just pass the URL to the image together will all its resizing parameters in the following code:

var url = "/-/media/Images/FOLDER/IMAGENAME.jpg?w=150";
url = Sitecore.Resources.Media.HashingUtils.ProtectAssertUrl(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM