Skip to content
Back to top
View this page
Edit this page

# Basic `set` and `get` operations

## Start off by connecting to the valkey server

To understand what `decode_responses=True` does, refer back to this document

    [1]:

    import valkey

    r = valkey.Valkey(decode_responses=True)
    r.ping()

    [1]:

    True

The most basic usage of `set` and `get`

    [2]:

    r.set("full_name", "john doe")

    [2]:

    True

    [3]:

    r.exists("full_name")

    [3]:

    1

    [4]:

    r.get("full_name")

    [4]:

    'john doe'

We can override the existing value by calling `set` method for the same key

    [5]:

    r.set("full_name", "overridee!")

    [5]:

    True

    [6]:

    r.get("full_name")

    [6]:

    'overridee!'

It is also possible to pass an expiration value to the key by using `setex` method

    [7]:

    r.setex("important_key", 100, "important_value")

    [7]:

    True

    [8]:

    r.ttl("important_key")

    [8]:

    100

A dictionary can be inserted like this

    [9]:

    dict_data = {
        "employee_name": "Adam Adams",
        "employee_age": 30,
        "position": "Software Engineer",
    }

    r.mset(dict_data)

    [9]:

    True

To get multiple keys’ values, we can use mget. If a non-existing key is also passed, Valkey return None for that key

    [10]:

    r.mget("employee_name", "employee_age", "position", "non_existing")

    [10]:

    ['Adam Adams', '30', 'Software Engineer', None]