Working with Strings and Binary Data
Valkey strings store sequences of bytes, which may include text, serialized objects, or binary arrays. GLIDE clients provide language-specific ways to handle both UTF-8 encoded strings and binary data when passing arguments to commands or receiving responses.
String API
Section titled “String API”Python uses TEncodable as a union type that accepts either:
str: for UTF-8 encoded stringsbytes: for binary data
Type Definition
Section titled “Type Definition”TEncodable = Union[str, bytes]Commands accept both str and bytes arguments. When passing strings, they are automatically encoded to UTF-8. When you need binary data, pass bytes directly.
# Using strings (automatically encoded)await client.set("key", "value")result = await client.get("key") # Returns bytes: b'value'
# Using bytes directlyawait client.set(b"key", b"binary_value")result = await client.get(b"key") # Returns bytes: b'binary_value'
# Converting between str and byteskey = "my_key"await client.set(key.encode(), b"value") # str to bytes with .encode()result = await client.get(key.encode())decoded = result.decode() # bytes to str with .decode()Return Values
Section titled “Return Values”Most commands return bytes by default. Use .decode() to convert to str when needed.
Valkey strings can be passed and received in two ways:
String: for common case UTF-8 converted stringsGlideString: to passbyte[]data using a container object
API Rules
Section titled “API Rules”- Command signatures either take and return
String’s orGlideString’s, but not both. String’s are returned if the commands are passed inString’s. e.gCompletableFuture<String[]> mget(String[] keys)GlideString’s are returned if the commands are passed inGlideString’s. e.gCompletableFuture<GlideString[]> mget(GlideString[] keys)
Arguments for commands that require a String can also be substituted with GlideString in order to pass in or return a binary value.
gs()is a static constructor that can be called with abyte[]orStringargument to convert toGlideString. For example:
byte[] byteKey = Base64.getDecoder().decode("byteKey");client.set(gs(byteKey), gs("GlideString value")).get();- A
GlideString byte[]object can be converted to UTF-8Stringby calling.getString()on theGlideStringobject. For example:
client.get(gs(byteKey)).get(); // "GlideString value" as a GlideStringclient.get(gs(byteKey)).get().getString(); // "GlideString value" as a StringGlideString is a type that maps to Valkey strings and represents either:
string: as a UTF-8 encoded stringBuffer: as abyte[]
Commands accept both string and Buffer arguments:
import {GlideClient} from "@valkey/valkey-glide";
// Using strings (UTF-8)await client.set("key", "value");const result = await client.get("key"); // Returns GlideString (string | Buffer)
// Using Buffer for binary dataconst binaryKey = Buffer.from([0x01, 0x02, 0x03, 0xFE]);const binaryValue = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF]);
await client.set(binaryKey, binaryValue);const value = await client.get(binaryKey); // Returns Buffer
// Converting between string and Bufferconst buffer = Buffer.from("text", "utf-8");const text = buffer.toString("utf-8");Return Types
Section titled “Return Types”- Commands use
stringinstead ofGlideStringas the return type if they contain a simple string value (simple strings in Valkey are always UTF-8 encoded strings). For example,typecommand returns one of pre-defined strings, orsetcommand which returns"OK". - Commands use
Bufferas the return type when they always contain binary data. For example,dumpandfunctionDumpcommands. - Most commands use
GlideStringas the common return type.
Decoder
Section titled “Decoder”While Node.js defaults to string responses, you have full control over the return data type.
You can use the DecoderOption property to toggle between string and byte formats for individual commands.
Additionally, you can define a global preference during client creation, which acts as the default for any command that doesn’t have a specific decoder argument.
Decoder is an optional parameter for commands with two options
Bytes: decodes the response into aBuffer(byte array).String: decodes the response into a string.
If Decoder is not configured, Decoder.String will be used as the default decoder.
Example Usage
Section titled “Example Usage”Here is an example of command implementation to outline arguments using DecoderOption and response type:
public getdel( key: GlideString, options?: DecoderOption, ): Promise<GlideString | null>Here’s a simple example demonstrating how to use Decoder in command API and Buffer.from() to encode binary data:
expect(await client.set(key, value)).toEqual("OK");expect(await client.getdel(key)).toEqual(value);
const valueEncoded = Buffer.from(value);expect(await client.set(key, value)).toEqual("OK");expect(await client.getdel(key, { decoder: Decoder.Bytes })).toEqual( valueEncoded,);Go uses native string type for all Valkey string operations. Go strings can contain arbitrary bytes, including binary data.
Commands accept string arguments and return string values. To work with binary data, convert between []byte and string:
import ( "context" "github.com/valkey-io/valkey-glide/go/v2")
// Using regular stringsresult, err := client.Set(context.Background(), "key", "value")value, err := client.Get(context.Background(), "key")
// Using binary data - convert []byte to stringbinaryKey := string([]byte{0x01, 0x02, 0x03, 0xFE})binaryValue := string([]byte{0xDE, 0xAD, 0xBE, 0xEF})
result, err := client.Set(context.Background(), binaryKey, binaryValue)
// Get returns string, convert back to []byte if neededvalue, err := client.Get(context.Background(), binaryKey)if !value.IsNil() { binaryData := []byte(value.Value())}Type Conversion
Section titled “Type Conversion”string([]byte{...})- Convert[]bytetostring[]byte(stringVar)- Convertstringto[]byte
Go strings are immutable sequences of bytes and can safely represent binary data.
PHP uses native string type for all Valkey string operations. PHP strings are binary-safe and can contain arbitrary bytes.
Commands accept string arguments and return string or mixed values:
// Create client$client = new ValkeyGlide();$client->connect(addresses: [['host' => 'localhost', 'port' => 6379]]);
// Using regular strings$client->set('key', 'value');$result = $client->get('key'); // Returns string: 'value'
// Using binary data - PHP strings are binary-safe$binaryKey = "\x01\x02\x03\xFE";$binaryValue = "\xDE\xAD\xBE\xEF";
$client->set($binaryKey, $binaryValue);$result = $client->get($binaryKey); // Returns binary stringType Handling
Section titled “Type Handling”- PHP strings are binary-safe and can store any byte sequence
- No special type or conversion needed for binary data
- Commands return
stringfor string values,mixedfor commands that may return different types