Skip to content

Instantly share code, notes, and snippets.

@waf
Last active February 7, 2024 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waf/19efd0feb648d659899ff9183d85dcaf to your computer and use it in GitHub Desktop.
Save waf/19efd0feb648d659899ff9183d85dcaf to your computer and use it in GitHub Desktop.
Get gzip/deflate-compressed content from redis in powershell
# one time setup
Install-Module PsRedis
# connect
Import-Module PsRedis
Connect-Redis -ConnectionString "my-redis-server.example.com"
# create function, given a redis key, get the bytes, ungzip/deflate them and return the string.
function Get-RedisKeyAndUncompress($key) {
# get ambient connection
$db = $Global:PsRedisCacheConnection.GetDatabase($Global:PsRedisDatabaseIndex)
# read bytes into memory stream
$bytes = [Byte[]]$db.StringGet($key)
$mem = New-Object -TypeName System.IO.MemoryStream(,$bytes)
# decompress
$deflate = New-Object System.IO.Compression.DeflateStream(@($mem, [System.IO.Compression.CompressionMode]::Decompress))
$decompressed = New-Object System.IO.MemoryStream
$deflate.CopyTo($decompressed)
$deflate.Dispose()
# encode to string
return [System.Text.Encoding]::UTF8.GetString(@($decompressed.ToArray()))
}
@ap76ap
Copy link

ap76ap commented Feb 7, 2024

To inflate from python side something similar to this is required:

import redis
import zlib

# Get / Check Connection
conn = redis.Redis(
            host=LIVE_SERVER,
            port=REDIS_PORT,
            decode_responses=False)
print(conn.ping())

# Get key from redis and inflate
blob = zlib.decompress(conn.get(KEY) , -15)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment