Created
June 20, 2014 18:04
-
-
Save steviemcg/1b0a9e2f9a21c24fdecc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Sitecore.Caching; | |
| using Sitecore.ContentSearch; | |
| namespace MyProject | |
| { | |
| public class MyContext | |
| { | |
| private static readonly ItemsContext _items; | |
| public static ItemsContext Items | |
| { | |
| get | |
| { | |
| return _items; | |
| } | |
| } | |
| static MyContext() | |
| { | |
| _items = new ItemsContext(); | |
| } | |
| /// <summary> | |
| /// An example of executing code just once per request | |
| /// The actual example is quite contrived :) | |
| /// </summary> | |
| public static ISearchIndex SearchIndex | |
| { | |
| get | |
| { | |
| const string key = "ContentSearchIndex"; | |
| if (Items[key] == null) | |
| { | |
| if (Sitecore.Context.Database == null) | |
| { | |
| throw new Exception("Sitecore.Context.Database cannot be null"); | |
| } | |
| Items[key] = Sitecore.Context.Database.Name == "web" | |
| ? ContentSearchManager.GetIndex("sitecore_web_index") | |
| : ContentSearchManager.GetIndex("sitecore_master_index"); | |
| } | |
| return (ISearchIndex)Items[key]; | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Especially as it doesn't account for the database potentially being switched during the request ;-)