Skip to content

Commit

Permalink
(chocolatey#3193) Add ability to ignore current http cache
Browse files Browse the repository at this point in the history
The changes here will add a global option that can be used
by the user to ignore any existing http cache that has been
created prior to the execution of the current command.

Using the option will still create a new cache for the running
execution and reuse that created cache in the current context,
and can be reused normally when not using the argument in the
future.
  • Loading branch information
AdmiringWorm authored and gep13 committed Jun 15, 2023
1 parent bdba4f7 commit 3d42e33
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function script:chocoCmdOperations($commands, $command, $filter, $currentArgumen
$script:chocoCommands = @('-?','search','list','info','install','outdated','upgrade','uninstall','new','pack','push','-h','--help','pin','source','config','feature','apikey','export','help','template','--version')

# ensure these all have a space to start, or they will cause issues
$allcommands = " --debug --verbose --trace --noop --help -? --online --accept-license --confirm --limit-output --no-progress --log-file='' --execution-timeout='' --cache-location='' --proxy='' --proxy-user='' --proxy-password='' --proxy-bypass-list='' --proxy-bypass-on-local --force --no-color --skip-compatibility-checks"
$allcommands = " --debug --verbose --trace --noop --help -? --online --accept-license --confirm --limit-output --no-progress --log-file='' --execution-timeout='' --cache-location='' --proxy='' --proxy-user='' --proxy-password='' --proxy-bypass-list='' --proxy-bypass-on-local --force --no-color --skip-compatibility-checks --ignore-http-cache"

$commandOptions = @{
list = "--id-only --pre --exact --by-id-only --id-starts-with --detailed --prerelease --include-programs --source='' --page='' --page-size=''"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,15 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
.Add("skipcompatibilitychecks|skip-compatibility-checks",
"SkipCompatibilityChecks - Prevent warnings being shown before and after command execution when a runtime compatibility problem is found between the version of Chocolatey and the Chocolatey Licensed Extension. Available in 1.1.0+",
option => config.DisableCompatibilityChecks = option != null)
.Add("ignore-http-cache",
"IgnoreHttpCache - Ignore any HTTP caches that have previously been created when querying sources, and create new caches. Available in 2.1.0+",
option =>
{
if (option != null)
{
config.CacheExpirationInMinutes = -1;
}
});
;
},
(unparsedArgs) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ChocolateyConfiguration()
RegularOutput = true;
PromptForConfirmation = true;
DisableCompatibilityChecks = false;
CacheExpirationInMinutes = 30;
SourceType = SourceTypes.Normal;
Information = new InformationCommandConfiguration();
Features = new FeaturesConfiguration();
Expand Down Expand Up @@ -340,6 +341,15 @@ private void AppendOutput(StringBuilder propertyValues, string append)
public bool ApplyInstallArgumentsToDependencies { get; set; }
public bool IgnoreDependencies { get; set; }

/// <summary>
/// Gets or sets the time before the cache is considered to have expired in minutes.
/// </summary>
/// <value>
/// The cache expiration in minutes.
/// </value>
/// <remarks>specifying a negative number disables the caching completely.</remarks>
public int CacheExpirationInMinutes { get; set; }

public bool AllowDowngrade { get; set; }
public bool ForceDependencies { get; set; }
public string DownloadChecksum { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace chocolatey.infrastructure.app.nuget
namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Threading;
using Alphaleonis.Win32.Filesystem;
using configuration;
using chocolatey.infrastructure.app.configuration;
using NuGet.Protocol.Core.Types;

public class ChocolateySourceCacheContext : SourceCacheContext
Expand All @@ -22,6 +17,16 @@ public class ChocolateySourceCacheContext : SourceCacheContext
public ChocolateySourceCacheContext(ChocolateyConfiguration config)
{
_chocolateyCacheLocation = config.CacheLocation;

if (config.CacheExpirationInMinutes <= 0)
{
MaxAge = DateTime.UtcNow;
RefreshMemoryCache = true;
}
else
{
MaxAge = DateTime.UtcNow.AddMinutes(-config.CacheExpirationInMinutes);
}
}

public override string GeneratedTempFolder
Expand Down

0 comments on commit 3d42e33

Please sign in to comment.