provisioning-core/nulib/domain/cache/simple_cache.nu

175 lines
4.2 KiB
Text
Raw Permalink Normal View History

# Simple, working cache implementation
# Focuses on core functionality with clean Nushell patterns
# Core cache operations
export def cache-write [
cache_type: string # "nickel", "sops", "final", etc.
cache_key: string # Unique identifier
data: any # Data to cache
] {
let cache_dir = (get-cache-dir $cache_type)
let cache_file = $"($cache_dir)/($cache_key).json"
# Create directory if needed
if not ($cache_dir | path exists) {
^mkdir -p $cache_dir
}
# Write cache file
$data | to json | save -f $cache_file
}
export def cache-read [
cache_type: string
cache_key: string
] {
let cache_file = $"(get-cache-dir $cache_type)/($cache_key).json"
if ($cache_file | path exists) {
open -r $cache_file | from json
} else {
null
}
}
export def cache-clear [
cache_type: string = "all"
] {
let cache_base = (get-cache-base)
if $cache_type == "all" {
^rm -rf $cache_base
} else {
let type_dir = $"($cache_base)/($cache_type)"
if ($type_dir | path exists) {
^rm -rf $type_dir
}
}
}
export def cache-list [
cache_type: string = "*"
] {
let cache_base = (get-cache-base)
if ($cache_base | path exists) {
let pattern = if $cache_type == "*" {
"/**/*.json"
} else {
$"/($cache_type)/*.json"
}
glob $"($cache_base)($pattern)"
} else {
[]
}
}
# Configuration management
export def cache-config-get [
setting: string = "enabled"
] {
let config = get-cache-config
# Simple dot notation support
if ($setting | str contains ".") {
let parts = ($setting | split row ".")
mut result = $config
for part in $parts {
$result = ($result | get --optional $part)
if ($result == null) {
return null
}
}
$result
} else {
$config | get --optional $setting
}
}
export def cache-config-set [
setting: string
value: any
] {
let config_path = (get-config-file)
let config_dir = ($config_path | path dirname)
# Create config directory if needed
if not ($config_dir | path exists) {
^mkdir -p $config_dir
}
# Load existing config or create new
let config = if ($config_path | path exists) {
open -r $config_path | from json
} else {
{}
}
# Set value
let updated = ($config | upsert $setting $value)
# Save
$updated | to json | save -f $config_path
}
export def get-cache-config [] {
let config_file = (get-config-file)
if ($config_file | path exists) {
open -r $config_file | from json
} else {
{
enabled: true
ttl_final_config: 300
ttl_nickel: 1800
ttl_sops: 900
ttl_provider: 600
}
}
}
# Status display
export def cache-status [] {
let config = (get-cache-config)
let cache_base = (get-cache-base)
print "=== Cache Configuration ==="
let enabled = ($config | get --optional enabled | default true)
let ttl_final = ($config | get --optional ttl_final_config | default 300)
let ttl_nickel = ($config | get --optional ttl_nickel | default 1800)
let ttl_sops = ($config | get --optional ttl_sops | default 900)
let ttl_provider = ($config | get --optional ttl_provider | default 600)
print $"Enabled: ($enabled)"
print $"TTL Final Config: ($ttl_final)s"
print $"TTL Nickel: ($ttl_nickel)s"
print $"TTL SOPS: ($ttl_sops)s"
print $"TTL Provider: ($ttl_provider)s"
print ""
# Cache statistics
if ($cache_base | path exists) {
let files = (glob $"($cache_base)/**/*.json" | where {|f| not ($f | str ends-with ".meta")})
print "=== Cache Statistics ==="
print $"Total cached items: ($files | length)"
print $"Cache location: ($cache_base)"
} else {
print "Cache not initialized yet"
}
}
# Helper functions
def get-cache-base [] {
let home = ($env.HOME | default "")
$"($home)/.provisioning/cache/config"
}
def get-cache-dir [cache_type: string] {
$"(get-cache-base)/($cache_type)"
}
def get-config-file [] {
$"(get-cache-base)/settings.json"
}