Skip to content

Commit

Permalink
Merge pull request #24 from Lombiq/issue/COLI-1199
Browse files Browse the repository at this point in the history
COLI-1199: Combinator: Clear cache for a fresh deployment
  • Loading branch information
BenedekFarkas authored Oct 9, 2023
2 parents 7013445 + 9102731 commit d9422ef
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 11 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/validate-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Validate Pull Request

on:
push:
pull_request:
types: [opened, synchronize]

jobs:
validate-pull-request:
uses: Lombiq/GitHub-Actions/.github/workflows/validate-pull-request.yml@dev
7 changes: 7 additions & 0 deletions Constants/FileNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Piedone.Combinator.Constants
{
public static class FileNames
{
public const string ClearCache = "_" + nameof(ClearCache) + ".txt";
}
}
9 changes: 9 additions & 0 deletions Constants/FolderNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Piedone.Combinator.Constants
{
public static class FolderNames
{
public const string PiedoneModules = "_" + nameof(PiedoneModules);
public const string Combinator = nameof(Combinator);
public const string Sites = nameof(Sites);
}
}
51 changes: 51 additions & 0 deletions EventHandlers/CombinatorCacheClearingShellEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.AppData;
using Piedone.Combinator.Constants;
using Piedone.Combinator.Services;

namespace Piedone.Combinator.EventHandlers
{
[OrchardFeature("Piedone.Combinator")]
public class CombinatorCacheClearingShellEventHandler : IOrchardShellEvents
{
private readonly ICacheFileService _cacheFileService;
private readonly IAppDataFolder _appDataFolder;
private readonly ShellSettings _shellSettings;

/// <summary>
/// Gets or sets a value indicating whether emptying Combinator cache is disabled, primarily through HostComponents.config.
/// </summary>
public bool IsDisabled { get; set; } = true;

public CombinatorCacheClearingShellEventHandler(
ICacheFileService cacheFileService,
IAppDataFolder appDataFolder,
ShellSettings shellSettings)
{
_cacheFileService = cacheFileService;
_appDataFolder = appDataFolder;
_shellSettings = shellSettings;
}

public void Activated()
{
if (IsDisabled) return;

var pathToClearCacheFile = _appDataFolder.Combine(
FolderNames.Sites, _shellSettings.Name, FolderNames.PiedoneModules, FolderNames.Combinator, FileNames.ClearCache);

if (!_appDataFolder.FileExists(pathToClearCacheFile)) return;

_cacheFileService.Empty();

_appDataFolder.DeleteFile(pathToClearCacheFile);
}

public void Terminating()
{
// Terminating event does not need to be implemented.
}
}
}
3 changes: 3 additions & 0 deletions Piedone.Combinator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@
<Content Include="Module.txt" />
</ItemGroup>
<ItemGroup>
<Compile Include="Constants\FolderNames.cs" />
<Compile Include="Constants\FileNames.cs" />
<Compile Include="Commands\CombinatorCommands.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Drivers\CombinatorSettingsPartDriver.cs" />
Expand All @@ -170,6 +172,7 @@
<Compile Include="EventHandlers\ICombinatorEventMonitor.cs" />
<Compile Include="EventHandlers\ICombinatorResourceEventHandler.cs" />
<Compile Include="EventHandlers\LessPreprocessor.cs" />
<Compile Include="EventHandlers\CombinatorCacheClearingShellEventHandler.cs" />
<Compile Include="EventHandlers\SassPreprocessor.cs" />
<Compile Include="Extensions\CombinedFileRecordExtensions.cs" />
<Compile Include="Extensions\RequireSettingsExtensions.cs" />
Expand Down
40 changes: 29 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,35 @@ An Orchard CMS module that combines and minifies external stylesheets and javasc
- With custom IStorageProvider can work in cloud hosting too (if there is no write access to the Media folder anyway)
- Import/export settings
- Administration page:
- Adjust combination exclusion filter
- Enable/disable combination of CDN resources
- Set up resource domain
- Enable/disable minification and adjust exclusion filter
- Enable/disable image embedding and adjust exclusion filter
- Enable/disable image sprite generation
- Define resource sets
- Enable/disable for admin site
- Empty cache

The module is also available for [DotNest](http://dotnest.com/) sites.
- Adjust combination exclusion filter
- Enable/disable combination of CDN resources
- Set up resource domain
- Enable/disable minification and adjust exclusion filter
- Enable/disable image embedding and adjust exclusion filter
- Enable/disable image sprite generation
- Define resource sets
- Enable/disable for admin site
- Empty cache
- The Combinator cache can be emptied when the Activated shell event fires if:
- A marker file with the name *_ClearCache.txt* is present in the *Orchard.Web/App_Data/Sites/<tenant_name>/_PiedoneModules/Combinator* folder. This file will then be deleted to have the cache cleared only on the first shell start, e.g. after a new deployment.
- The `CombinatorCacheClearingShellEventHandler` class's `IsDisabled` property is set to `false` (default is `true`) by adding the following section to *Orchard.Web/Config/HostComponents.config*:
```
<Component Type="Piedone.Combinator.EventHandlers.CombinatorCacheClearingShellEventHandler">
<Properties>
<Property Name="IsDisabled" Value="false"/>
</Properties>
</Component>
```
Alternatively, the same can be achieved through a file transformation ([see this for details](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations)) to only activate this feature for a given build configuration, e.g., by adding the following to *HostComponents.Release.config* for `Release` mode.
```
<Component Type="Piedone.Combinator.EventHandlers.CombinatorCacheClearingShellEventHandler" xdt:Transform="Insert">
<Properties>
<Property Name="IsDisabled" Value="false" xdt:Transform="Insert" />
</Properties>
</Component>
```
The module is also available for [DotNest](https://dotnest.com) sites.
You can download an install the module from the [Orchard Gallery](http://orchardproject.net/gallery/List/Modules/Orchard.Module.Piedone.Combinator).
For known issues and future plans please see the [Issue Tracker](https://github.com/Lombiq/Combinator/issues).
Expand Down

0 comments on commit d9422ef

Please sign in to comment.