#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB as a Redis replacement: the Redis protocol on port 6379, a memory limit with LRU eviction, and optional snapshots to disk. PostgreSQL stays on 127.0.0.1 for administration. Runs in the foreground. .EXAMPLE $env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret' .\setup.ps1 -MaxMemory 4gb -Persist #> [CmdletBinding()] param( [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }), # Roles, and the snapshot when -Persist is on. [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }), [string]$Listen = $(if ($env:RDB_KV_LISTEN) { $env:RDB_KV_LISTEN } else { '127.0.0.1' }), [int]$KvPort = $(if ($env:RDB_KV_PORT) { [int]$env:RDB_KV_PORT } else { 6379 }), # 0 is unbounded. [string]$MaxMemory = $(if ($env:RDB_KV_MAX_MEMORY) { $env:RDB_KV_MAX_MEMORY } else { '1gb' }), [ValidateSet('noeviction', 'allkeys-lru', 'volatile-lru', 'allkeys-random', 'volatile-random', 'volatile-ttl')] [string]$Eviction = $(if ($env:RDB_KV_EVICTION) { $env:RDB_KV_EVICTION } else { 'allkeys-lru' }), # Save the keys to disk periodically and at a clean stop. [switch]$Persist = ($env:RDB_KV_PERSIST -eq '1'), [int]$SaveSeconds = $(if ($env:RDB_KV_SAVE_SECS) { [int]$env:RDB_KV_SAVE_SECS } else { 300 }), # Clients must use TLS (redis-cli --tls). [switch]$Tls = ($env:RDB_KV_TLS -eq 'on'), [string]$CertPath = $env:RDB_TLS_CERT, [string]$KeyPath = $env:RDB_TLS_KEY, # The role AUTH logs in as. [string]$User = $(if ($env:RDB_USER) { $env:RDB_USER } else { 'rasterized' }), [int]$PgPort = $(if ($env:RDB_PG_PORT) { [int]$env:RDB_PG_PORT } else { 5432 }) ) $ErrorActionPreference = 'Stop' $cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." } if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD; Redis clients send it with AUTH.' } New-Item -ItemType Directory -Force -Path $Data | Out-Null $Data = (Resolve-Path -LiteralPath $Data).Path $serverArgs = @('--location', $Data, '--protocol', 'pgwire', '--pg-addr', '127.0.0.1', '--pg-port', "$PgPort", '--pg-user', $User, '--kv-addr', $Listen, '--kv-port', "$KvPort", '--kv-user', $User, '--kv-auth', 'password', '--kv-max-memory', $MaxMemory, '--kv-eviction', $Eviction, '--kv-tls', $(if ($Tls) { 'on' } else { 'off' })) if ($Persist) { $serverArgs += @('--kv-persist', '--kv-save-interval-secs', "$SaveSeconds") } if ($CertPath) { # The Redis port uses the PostgreSQL certificate. $serverArgs += @('--pg-cert', (Resolve-Path -LiteralPath $CertPath).Path, '--pg-key', (Resolve-Path -LiteralPath $KeyPath).Path) } $tlsFlags = if ($Tls) { ' --tls --insecure' } else { '' } $scheme = if ($Tls) { 'rediss' } else { 'redis' } Write-Host "Redis: redis-cli -h $Listen -p $KvPort$tlsFlags --askpass PING" Write-Host "URL: ${scheme}://${User}:@${Listen}:$KvPort/0" Write-Host "Admin SQL: psql `"postgresql://$User@127.0.0.1:$PgPort/postgres?sslmode=require`"" Write-Host "Memory limit $MaxMemory, eviction $Eviction, persistence $(if ($Persist) { 'on' } else { 'off' }). Stop with Ctrl+C." Write-Host '' & $cmd.Source @serverArgs exit $LASTEXITCODE