Redis.Commands.TopK (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis Top-K (TOPK.*) operations.

Top-K is a probabilistic data structure that tracks the K most frequent items (heavy hitters) in a stream of data. It uses a Count-Min Sketch internally to approximate item frequencies, making it very memory-efficient for finding popular items in high-volume streams without storing every element.

All functions in this module are pure and return a command list (a list of strings) suitable for passing to Redis.command/2 or Redis.pipeline/2.

Examples

# Track the top 3 most popular pages
Redis.command(conn, TopK.reserve("popular_pages", 3))

# Record page views and retrieve the current top-K list
Redis.pipeline(conn, [
  TopK.add("popular_pages", ["/home", "/about", "/home", "/pricing"]),
  TopK.list("popular_pages", withcount: true)
])

Summary

Functions

Adds one or more items to the Top-K structure.

Returns the current Top-K list. Pass withcount: true to include counts.

Checks whether one or more items are currently in the Top-K list.

Functions

add(key, items)

@spec add(String.t(), [String.t()]) :: [String.t()]

Adds one or more items to the Top-K structure.

Returns a list where each element is either nil (if no item was evicted) or the name of the item that was evicted to make room.

count(key, items)

@spec count(String.t(), [String.t()]) :: [String.t()]

incrby(key, item_increments)

@spec incrby(String.t(), [{String.t(), integer()}]) :: [String.t()]

info(key)

@spec info(String.t()) :: [String.t()]

list(key, opts \\ [])

@spec list(
  String.t(),
  keyword()
) :: [String.t()]

Returns the current Top-K list. Pass withcount: true to include counts.

query(key, items)

@spec query(String.t(), [String.t()]) :: [String.t()]

Checks whether one or more items are currently in the Top-K list.

reserve(key, topk, opts \\ [])

@spec reserve(String.t(), non_neg_integer(), keyword()) :: [String.t()]