这是用户在 2024-3-21 22:52 为 https://redis.io/docs/data-types/bitfields/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Redis bitfields Redis位域

Introduction to Redis bitfields
Redis位域介绍

Redis bitfields let you set, increment, and get integer values of arbitrary bit length. For example, you can operate on anything from unsigned 1-bit integers to signed 63-bit integers.
Redis位字段允许您设置、增加和获取任意位长度的整数值。例如,您可以操作从无符号1位整数到有符号63位整数的任何内容。

These values are stored using binary-encoded Redis strings. Bitfields support atomic read, write and increment operations, making them a good choice for managing counters and similar numerical values.
这些值使用二进制编码的Redis字符串存储。位字段支持原子读取、写入和增量操作,使其成为管理计数器和类似数值的良好选择。

Basic commands 基本命令

  • BITFIELD atomically sets, increments and reads one or more values.
    原文已经是简体中文了,无需翻译。
  • BITFIELD_RO is a read-only variant of BITFIELD.
    BITFIELD_ROBITFIELD 的只读变体。

Examples 例子

Example 例子

Suppose you want to maintain two metrics for various bicycles: the current price and the number of owners over time. You can represent these counters with a 32-bit wide bitfield per for each bike.
假设您想要记录各种自行车的两个指标:当前价格和随时间变化的所有者数量。您可以使用每辆自行车的32位宽度的位字段来表示这些计数器。

  • Bike 1 initially costs 1,000 (counter in offset 0) and has never had an owner. After being sold, it's now considered used and the price instantly drops to reflect its new condition, and it now has an owner (offset 1). After quite some time, the bike becomes a classic. The original owner sells it for a profit, so the price goes up and the number of owners does as well.Finally, you can look at the bike's current price and number of owners.
    自行车1最初的价格是1,000(偏移量为0),从未有过主人。在被出售后,它现在被认为是二手的,价格立即下降以反映其新的状况,并且现在有了一个主人(偏移量为1)。经过相当长的时间,这辆自行车成为了经典。原始所有者以盈利的方式出售它,所以价格上涨了,拥有者的数量也增加了。最后,你可以看一下这辆自行车的当前价格和拥有者的数量。

Performance 表现

BITFIELD is O(n), where n is the number of counters accessed.
BITFIELD 是O(n),其中n是访问的计数器数量。

Rate this page 评价此页面