Database Reference
In-Depth Information
Listing 3.4 demonstrates basic commands for how to insert and retrieve single values,
lists, and hash structures using the Redis command-line interface.
Listing 3.4 Examples of using Redis for single values, lists and hashes
# Setting values by key
> SET user:1:name 'Michael'
> SET user:2:name 'Ben'
# Retrieve a list of keys matching a pattern
> KEYS user*
1) "user:2:name"
2) "user:1:name"
# Retrieving a value using the respective key
> GET user:2:name
"Ben"
# Using lists - LPUSH adds values to the head of a list
> LPUSH names 'Jared'
> LPUSH names 'Melinda'
# Retrieve a range of values from a list
> LRANGE names 0 1
1) "Melinda"
2) "Jared"
# RPUSH adds values to the tail of the list
> RPUSH names 'Debra'
> LRANGE names 0 2
1) "Melinda"
2) "Jared"
3) "Debra"
# Using hashes - HMSET sets fields to their respective values for a
# hash stored at a key
> HMSET employees:1 name 'Michael' city 'San Francisco' role 'Author'
# Retrieve the "name" attribute of the hash "employees:1"
> HGET employees:1 name
"Michael"
 
Search WWH ::




Custom Search