Skip to content

Commit b1d99d3

Browse files
committed
Implemented caching mechanism for tab completion results. Would appreciate a quick code review.
1 parent f41f1e5 commit b1d99d3

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

Microsoft.Azure.ArgumentCompleters.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function StorageAccount_StorageAccountNameCompleter
2121

2222
$CacheKey = 'StorageAccount_StorageAccountNameCache';
2323
$StorageAccountNameCache = Get-CompletionPrivateData -Key $CacheKey;
24+
25+
### Return the cached value if it has not expired
2426
if ($StorageAccountNameCache) {
2527
return $StorageAccountNameCache;
2628
}

TabExpansion++.psm1

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,19 @@ function Set-CompletionPrivateData
134134
$Key,
135135

136136
[object]
137-
$Value)
137+
$Value,
138138

139-
$completionPrivateData[$key] = $value
139+
[ValidateNotNullOrEmpty()]
140+
[int]
141+
$ExpirationSeconds = 20
142+
)
143+
144+
$Cache = [PSCustomObject]@{
145+
Value = $Value;
146+
ExpirationTime = (Get-Date).AddSeconds($ExpirationSeconds);
147+
};
148+
Add-Content -Path TabExpansion.log -Value ('Setting cache expiration time to: {0}. Key is: {1}' -f $Cache.ExpirationTime, $Key);
149+
$completionPrivateData[$key] = $Cache;
140150
}
141151

142152
#############################################################################
@@ -148,8 +158,12 @@ function Get-CompletionPrivateData
148158
[string]
149159
$Key)
150160

151-
Flush-BackgroundResultsQueue
152-
return $completionPrivateData[$key]
161+
Flush-BackgroundResultsQueue;
162+
163+
$cacheValue = $completionPrivateData[$key];
164+
if ((Get-Date) -lt $cacheValue.ExpirationTime) {
165+
return $cacheValue.Value;
166+
}
153167
}
154168

155169
#############################################################################
@@ -562,7 +576,15 @@ function Update-ArgumentCompleter
562576
#############################################################################
563577
#
564578
# .SYNOPSIS
579+
# Retrieves a list of argument completers that have been loaded into the
580+
# PowerShell session.
581+
#
582+
# .PARAMETER Name
583+
# The name of the argument complete to retrieve. This parameter supports
584+
# wildcards (asterisk).
565585
#
586+
# .EXAMPLE
587+
# Get-ArgumentComplete
566588
function Get-ArgumentCompleter
567589
{
568590
[CmdletBinding()]
@@ -796,7 +818,7 @@ function Flush-BackgroundResultsQueue
796818
$parameters = $item.Value
797819
if ($item.ArgumentCompleter)
798820
{
799-
Register-ArgumentCompleter @parameters
821+
TabExpansion++\Register-ArgumentCompleter @parameters
800822
}
801823
elseif ($item.InitializationData)
802824
{

0 commit comments

Comments
 (0)