This quick start guide shows you how to:
- Get started with Valkey
- Store data under a key in Valkey
- Retrieve data with a key from Valkey
- Scan the keyspace for keys that match a specific pattern
The examples in this article refer to a simple bicycle inventory.
Setup
See the installation guides to install Valkey on your local machine.
Connect
The first step is to connect to Valkey. There are client connectors for most programming languages.
You can also connect using valkey-cli, the command line interface.
The following example shows how to connect to a Valkey server that runs on localhost (-h 127.0.0.1
) and listens on the default port (-p 6379
):
$ valkey-cli -h 127.0.0.1 -p 6379
Store and retrieve data
Valkey stands for Remote Dictionary Server. You can use the same data types as in your local programming environment but on the server side within Valkey.
Similar to byte arrays, Strings store sequences of bytes, including text, serialized objects, counter values, and binary arrays. The following example shows you how to set and get a string value:
127.0.0.1:6379> SET bike:1 "Process 134"
OK
127.0.0.1:6379> GET bike:1
"Process 134"
Hashes are the equivalent of dictionaries (dicts or hash maps). Among other things, you can use hashes to represent plain objects and to store groupings of counters. The following example explains how to set and access field values of an object:
127.0.0.1:6379> HSET bike:1 model Deimos brand Ergonom type 'Enduro bikes' price 4972
(integer) 4
127.0.0.1:6379> HGET bike:1 model
"Deimos"
127.0.0.1:6379> HGET bike:1 price
"4972"
127.0.0.1:6379> HGETALL bike:1
1) "model"
2) "Deimos"
3) "brand"
4) "Ergonom"
5) "type"
6) "Enduro bikes"
7) "price"
8) "4972"
You can get a complete overview of available data types in this documentation site's data types section. Each data type has commands allowing you to manipulate or retrieve data. The commands reference provides a sophisticated explanation.
Scan the keyspace
Each item within Valkey has a unique key. All items live within the Valkey keyspace. You can scan the Valkey keyspace via the SCAN command. Here is an example that scans for the first 100 keys that have the prefix bike:
:
127.0.0.1:6379> SCAN 0 MATCH "bike:*" COUNT 100
1) "0"
2) 1) "bike:4"
2) "bike:3"
3) "bike:5"
4) "bike:1"
5) "bike:2"
SCAN returns a cursor position, allowing you to scan iteratively for the next batch of keys until you reach the cursor value 0.