types in redis

Redis 支持多种数据类型,如字符串、列表、集合、有序集合、哈希、位图、超几何分布和地理空间索引等,适用于各种数据结构存储。例如,字符串用于存储文本或二进制数据,列表实现为链接列表,集合存储唯一元素,有序集合按分数排序成员,哈希用于存储对象,位图作为位数组,超几何分布用于近似唯一计数,地理空间索引则用于存储地理位置数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Redis supports several different data types that allow you to store a variety of data structures. As of the latest Redis versions, the primary data types include:

  1. Strings:

    • The simplest type, storing text or binary data up to 512 MB in size.
    • Examples: "Hello, World!", "12345", "\x00\x01\x02"
  2. Lists:

    • Ordered collections of strings.
    • Implemented as linked lists, allowing operations like LPUSH, RPUSH, LPOP, RPOP, and LRANGE.
    • Example: ["apple", "banana", "cherry"]
  3. Sets:

    • Unordered collections of unique strings.
    • Support operations like SADD, SREM, SPOP, SMEMBERS, and set operations like SINTER, SUNION, SDIFF.
    • Example: {"apple", "banana", "cherry"}
  4. Sorted Sets (ZSets):

    • Similar to Sets but with an associated score for each member, allowing the members to be ordered.
    • Support operations like ZADD, ZREM, ZRANGE, ZRANGEBYSCORE, ZINCRBY.
    • Example: {("apple", 1), ("banana", 2), ("cherry", 3)}
  5. Hashes:

    • Maps of fields and values, similar to dictionaries or hash tables.
    • Useful for storing objects.
    • Support operations like HSET, HGET, HMSET, HGETALL.
    • Example: {"name": "Alice", "age": "30", "city": "Wonderland"}
  6. Bitmaps:

    • Strings that can be used as bit arrays.
    • Allow bit-level operations.
    • Support operations like SETBIT, GETBIT, BITCOUNT.
  7. HyperLogLogs:

    • Probabilistic data structure used for counting unique items with high efficiency.
    • Support operations like PFADD, PFCOUNT, PFMERGE.
    • Useful for approximate distinct counting.
  8. Geospatial Indexes:

    • Specialized form of sorted sets for storing geospatial data.
    • Support operations like GEOADD, GEORADIUS, GEODIST, GEOPOS.
    • Example: Store locations with latitude and longitude.
  9. Streams:

    • Log-based data structure for storing an append-only series of messages.
    • Support operations like XADD, XRANGE, XREAD, XGROUP.
    • Useful for real-time data processing and message brokering.

Additional Features and Data Structures

  • Modules:

    • Redis can be extended with custom modules that provide new data types and functionalities.
  • Keys and Keyspace Notifications:

    • Although not a data type, Redis provides notifications for events related to keys.

Example Commands for Each Type

  • String: SET key "value", GET key
  • List: LPUSH mylist "world", RPUSH mylist "hello", LRANGE mylist 0 -1
  • Set: SADD myset "hello", SADD myset "world", SMEMBERS myset
  • Sorted Set: ZADD myzset 1 "one", ZADD myzset 2 "two", ZRANGE myzset 0 -1 WITHSCORES
  • Hash: HSET myhash field1 "foo", HSET myhash field2 "bar", HGETALL myhash
  • Bitmap: SETBIT mybitkey 7 1, GETBIT mybitkey 7, BITCOUNT mybitkey
  • HyperLogLog: PFADD myhll "foo" "bar" "baz", PFCOUNT myhll
  • Geospatial: GEOADD mygeo 13.361389 38.115556 "Palermo", GEORADIUS mygeo 15 37 200 km
  • Stream: XADD mystream * name Alice, XRANGE mystream - +

Redis's diverse data types make it a versatile tool for various use cases, from simple caching to complex data structures and real-time data processing.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值