String, List, Set, and Other Data Types Commands in Redis

Redis: Core Data Types and Commands

Redis is a key-value store that supports multiple data types, enabling efficient storage and retrieval of data. The subtopic "Basic Commands and Data Types" focuses on the fundamental data types—String, List, Set, Hash, ZSET, and Geo—and their corresponding commands. This article explores the core operations for each data type, their purposes, and practical examples.

String Commands

Strings are the simplest data type in Redis, representing a single value. Key operations include:

  • SET: Assigns a value to a key.

    SET key value  
    

    Example: SET mykey "Hello, Redis!"

  • GET: Retrieves the value of a key.

    GET key  
    

    Example: GET mykey returns "Hello, Redis!"

  • EXISTS: Checks if a key exists.

    EXISTS key  
    

    Example: EXISTS mykey returns 1 (true) or 0 (false)

  • DEL: Deletes a key.

    DEL key  
    

    Example: DEL mykey removes the key

Strings are ideal for storing simple data like user IDs, timestamps, or configuration settings.

List Commands

Lists are ordered collections of strings. Key commands include:

  • LPUSH: Adds an element to the left of the list.

    LPUSH list key value  
    

    Example: LPUSH mylist "apple"

  • RPUSH: Adds an element to the right of the list.

    RPUSH list key value  
    

    Example: RPUSH mylist "banana"

  • LINDEX: Retrieves the element at a specific index.

    LINDEX list index  
    

    Example: LINDEX mylist 0 returns "apple"

  • LPOP: Removes and returns the first element.

    LPOP list  
    

    Example: LPOP mylist returns "apple"

Lists are useful for maintaining ordered sequences, such as message queues or history logs.

Set Commands

Sets are unordered collections of unique elements. Key commands include:

  • SADD: Adds elements to a set.

    SADD set key value1 value2  
    

    Example: SADD myset "apple" "banana"

  • SMEMBERS: Retrieves all elements in a set.

    SMEMBERS set  
    

    Example: SMEMBERS myset returns ["apple", "banana"]

  • SINTER: Finds the intersection of two sets.

    SINTER set1 set2  
    

    Example: SINTER myset1 myset2 returns ["apple"]

  • SUNION: Finds the union of two sets.

    SUNION set1 set2  
    

    Example: SUNION myset1 myset2 returns ["apple", "banana"]

Sets are commonly used for membership checks, such as tracking unique user IDs or hashtags.

Hash Commands

Hashes store data as a dictionary with keys and values. Key commands include:

  • HSET: Sets a value for a key.

    HSET hash key value  
    

    Example: HSET myhash "name" "Alice"

  • HGET: Retrieves a value for a key.

    HGET hash key  
    

    Example: HGET myhash "name" returns "Alice"

  • HGETALL: Retrieves all key-value pairs.

    HGETALL hash  
    

    Example: HGETALL myhash returns {"name": "Alice"}

  • HDEL: Deletes a key from a hash.

    HDEL hash key  
    

    Example: HDEL myhash "name" removes the key

Hashes are ideal for storing user profiles, where multiple fields (e.g., name, email, age) are stored together.

ZSET Commands

ZSets (sorted sets) are ordered collections of unique elements. Key commands include:

  • ZADD: Adds elements to a zset.

    ZADD zset key value  
    

    Example: ZADD myzset "apple" 1 "banana" 2

  • ZRANGE: Retrieves elements in a range.

    ZRANGE zset min max  
    

    Example: ZRANGE myzset 1 2 returns ["banana"]

  • ZSCORE: Retrieves the score of an element.

    ZSCORE zset key  
    

    Example: ZSCORE myzset "apple" returns 1

  • ZREM: Removes an element from a zset.

    ZREM zset key  
    

    Example: ZREM myzset "apple" removes the element

ZSets are useful for ranking systems, such as tracking user scores or popularity.

Geo Commands

Geo (Geographic) data types store locations. Key commands include:

  • GEORADIUS: Retrieves points within a geographic area.

    GEORADIUS geokey longitude latitude radius  
    

    Example: GEORADIUS mygeo 10 20 10 returns locations within 10 units of (10,20)

  • GEOSAVE: Saves a geographic point.

    GEOSAVE geokey point  
    

    Example: GEOSAVE mygeo "10 20" stores the point

Geo is used for location-based queries, such as finding nearby restaurants or events.

Conclusion

Redis provides a robust framework for managing data through its core data types and commands. Strings, lists, sets, hashes, zsets, and geos are each suited for specific use cases, from simple key-value storage to complex spatial queries. Understanding these commands enables efficient data manipulation and retrieval, making Redis a powerful tool for application development.