-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12_GenericCollections.ps1
More file actions
69 lines (59 loc) · 2.5 KB
/
Copy path12_GenericCollections.ps1
File metadata and controls
69 lines (59 loc) · 2.5 KB
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
<#
.SYNOPSIS
OOP Reference: Generic Collections in Classes
.DESCRIPTION
Topic: Strongly-typed collections, TryGetValue, SortedDictionary
Category: Structural
Agent Task: Add a GetExpiredKeys([int]$olderThanDays) method that returns
string[] of key names not accessed in the last N days.
Track last-accessed timestamp per key.
Add Pester tests for TryGet returning false on missing key.
Done Conditions:
- TryGet returns $false and does not throw on missing key
- GetExpiredKeys returns correct subset
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- No concurrent dictionary (covered in object pool)
#>
class TypedKeyStore {
hidden [System.Collections.Generic.Dictionary[string, byte[]]]$_keys
hidden [System.Collections.Generic.List[string]]$_auditLog
hidden [System.Collections.Generic.Dictionary[string, datetime]]$_lastAccessed
TypedKeyStore() {
$this._keys = [System.Collections.Generic.Dictionary[string,byte[]]]::new()
$this._auditLog = [System.Collections.Generic.List[string]]::new()
$this._lastAccessed = [System.Collections.Generic.Dictionary[string,datetime]]::new()
}
[void] Set([string]$name, [byte[]]$key) {
$this._keys[$name] = $key
$this._lastAccessed[$name] = [datetime]::UtcNow
$this._auditLog.Add("SET $name $(Get-Date -Format u)")
}
[byte[]] Get([string]$name) {
$key = $null
if (-not $this._keys.TryGetValue($name, [ref]$key)) {
throw [System.Collections.Generic.KeyNotFoundException]"Key '$name' not found"
}
$this._lastAccessed[$name] = [datetime]::UtcNow
return $key
}
[bool] TryGet([string]$name, [ref]$outKey) {
$result = $this._keys.TryGetValue($name, $outKey)
if ($result) {
$this._lastAccessed[$name] = [datetime]::UtcNow
}
return $result
}
[string[]] GetExpiredKeys([int]$olderThanDays) {
$cutoffDate = [datetime]::UtcNow.AddDays(-$olderThanDays)
$expired = [System.Collections.Generic.List[string]]::new()
foreach ($kvp in $this._lastAccessed.GetEnumerator()) {
if ($kvp.Value -lt $cutoffDate) {
$expired.Add($kvp.Key)
}
}
return $expired.ToArray()
}
[string[]] GetNames() { return @($this._keys.Keys) }
[string[]] GetAuditLog() { return $this._auditLog.ToArray() }
}