Redis.Commands.CMS (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis Count-Min Sketch (CMS.*) operations.

A Count-Min Sketch is a probabilistic data structure for estimating the frequency of items in a data stream. It uses a fixed-size matrix of counters addressed by multiple hash functions. Frequency estimates may overcount but will never undercount, making it useful for approximate frequency queries where a small positive bias is acceptable.

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

# Initialize by exact dimensions (width x depth)
Redis.command(conn, CMS.initbydim("clicks", 2000, 5))

# Increment counts and query frequencies
Redis.pipeline(conn, [
  CMS.incrby("clicks", [{"page_a", 3}, {"page_b", 1}]),
  CMS.query("clicks", ["page_a", "page_b"])
])

Summary

Functions

Increments the count of one or more items. Each element in the list is a {item, increment} tuple.

Initializes a Count-Min Sketch with the given width and depth.

Initializes a Count-Min Sketch with a desired error rate and probability of exceeding that error. This is a convenience alternative to initbydim/3 that lets Redis choose the matrix dimensions.

Functions

incrby(key, item_increments)

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

Increments the count of one or more items. Each element in the list is a {item, increment} tuple.

info(key)

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

initbydim(key, width, depth)

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

Initializes a Count-Min Sketch with the given width and depth.

The width controls accuracy (more columns = less overcounting) and depth controls confidence (more rows = lower probability of large error).

initbyprob(key, error, probability)

@spec initbyprob(String.t(), float(), float()) :: [String.t()]

Initializes a Count-Min Sketch with a desired error rate and probability of exceeding that error. This is a convenience alternative to initbydim/3 that lets Redis choose the matrix dimensions.

merge(destkey, sources, opts \\ [])

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

query(key, items)

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