Sitecore Smart HTML Cache Clearance

Smart HTML Cache clearance – As the name suggests it clears HTML Cache for a site in a smarter way.

In Sitecore Content tree if you are building a site for two or more different countries/regions and as you publish any content in any of the sites that you can observe via Cache.aspx that HTML Cache for each site gets cleared. Which should not happen.

So in order to achieve this goal, I did analysis and found that we have to override the default implementation of cache clearance when we publish any item in the content tree. But what if we publish any template or media item or anything other than content than at that time whole HTML cache should get clear. Also when we publish the whole site at that time HTML cache for all the sites should get clear. Smart HTML Cache Clearance will handle all the scenarios efficiently and accurately when you publish any item on local or on remote site. Also when it needs to clear the entire HTML Cache for all the sites than the default implementation, as written by Sitecore, will handle our job.

So whenever we face such challenges we google it, so similarly I did and found the blog of Mark Stiles ( https://markstiles.net/Blog/2011/05/26/partial-html-cache-clearing.aspx), which helped me to achieve the objective but that was for remote publishing, what if Sitecore is enabled on the production server? Or what if we want to test it in local? So for that, I tried to modify the code and got the solution.

Step 1: Create a class called SmartHtmlCacheClearer in your library.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

publicclassSmartHtmlCacheClearer

{

#region Fields

privatereadonly ArrayList _sites=newArrayList();

#endregion

#region Properties

publicArrayListSites

{

get

{

returnthis._sites;

}

}

#endregion

#region Methods

/// This method is called when publish:end or publish:end:remote events are triggered.

/// This method will verify that whether to clear the HTML cache for any particular site or for all sites and it will also clear the HTML Cache for particular site if verfication is for particular site.

privatevoidClearHtmlCache(Database database,Guid ItemGuid)

{

if(database!=null)

{

Item currentItem=database.GetItem(newID(ItemGuid));

if(currentItem!=null)

{

Item startItem=currentItem.HomeItem();

//If startItem is not Site Root item then we will clear full HTML caches