All skills
upstash avatar

/upstash-redis-js

@35da719
by upstashupstash/skills27 stars
7

Work with the @upstash/redis TypeScript/JavaScript SDK, a serverless HTTP-based Redis client for Next.js, Vercel, Cloudflare Workers, edge runtimes, and Node.js. Use when adding a cache (cache-aside, write-through, TTL and expiration strategies), session storage and user sessions, a key-value store, leaderboards and rankings with sorted sets, counters, distributed locks, queues with lists, streams and consumer groups, sparse index-addressed arrays and ring buffers (ARSET, ARINSERT, ARRING, ARGREP, AROP), embeddings and nearest-neighbour vector search stored inside Redis (VECTOR commands via redis.vector, separate from @upstash/vector), JSON documents, pipelines and MULTI/EXEC transactions, Lua scripting, read replicas, or full-text search, typo-tolerant search, facets, aggregations, and search over Redis stream entries with Upstash Redis Search (different from regular FT.SEARCH; also available for TCP clients via @upstash/search-redis and @upstash/search-ioredis). Also use when migrating from ioredis or node-redis, when a Redis connection is needed from a serverless function without connection pooling, when integrating @upstash/ratelimit, or when the user says Redis cache, KV store, session store, serverless Redis, or Upstash Redis. Supports automatic serialization/deserialization of JavaScript types.

Use this Skill: https://skilld.dev/gh/upstash/skills/upstash-redis-js

This session only. Nothing lands on disk.

data-structuressorted-sets.md

≈993 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Sorted Sets

Overview

Sorted sets store unique members with associated scores. Members are automatically ordered by score, enabling range queries and rankings.

Good For

  • Leaderboards and rankings
  • Priority queues
  • Time-series data (using timestamp as score)
  • Trending content (using engagement score)

Limitations

  • Maximum members: 2^32 - 1 (4.2 billion) per sorted set
  • Scores are 64-bit floating point numbers
  • Members must be unique (updating a member replaces its score)

Examples

import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();

// Add members with scores
await redis.zadd("leaderboard", { score: 100, member: "player1" });
await redis.zadd(
  "leaderboard",
  { score: 250, member: "player2" },
  { score: 150, member: "player3" }
);

// Update score (replaces if member exists)
await redis.zadd("leaderboard", { score: 300, member: "player1" });

// Increment score
await redis.zincrby("leaderboard", 50, "player3"); // player3 now has 200

// Get number of members
const count = await redis.zcard("leaderboard"); // 3

// Get score of a member
const score = await redis.zscore("leaderboard", "player1"); // 300

// Get rank (position) of member (0-based, lowest score = rank 0)
const rank = await redis.zrank("leaderboard", "player3"); // 0 (lowest score)

// Get reverse rank (highest score = rank 0)
const revRank = await redis.zrevrank("leaderboard", "player1"); // 0 (highest score)

// Get range by rank (ascending order)
const bottom2 = await redis.zrange("leaderboard", 0, 1);
// ["player3", "player2"] - without scores

// Get range with scores
const bottom2WithScores = await redis.zrange("leaderboard", 0, 1, { withScores: true });
// [{ member: "player3", score: 200 }, { member: "player2", score: 250 }]

// Get range in descending order (highest scores first)
const top2 = await redis.zrange("leaderboard", 0, 1, { rev: true });
// ["player1", "player2"]

const top2WithScores = await redis.zrange("leaderboard", 0, 1, { withScores: true, rev: true });
// [{ member: "player1", score: 300 }, { member: "player2", score: 250 }]

// Get all members
const all = await redis.zrange("leaderboard", 0, -1);

// Get range by score
const midRange = await redis.zrange("leaderboard", 150, 250, { byScore: true });
// ["player3", "player2"]

// Get range by score (descending)
const midRangeDesc = await redis.zrange("leaderboard", 250, 150, { rev: true, byScore: true });
// ["player2", "player3"]

// Count members in score range
const countInRange = await redis.zcount("leaderboard", 100, 250); // 2

// Remove members
await redis.zrem("leaderboard", "player2");
await redis.zrem("leaderboard", "player1", "player3"); // Remove multiple

// Remove by rank range
await redis.zadd(
  "scores",
  { score: 1, member: "a" },
  { score: 2, member: "b" },
  { score: 3, member: "c" }
);
await redis.zremrangebyrank("scores", 0, 0); // Remove lowest score (rank 0)

// Remove by score range
await redis.zremrangebyscore("scores", 2, 3); // Remove members with scores 2-3

// Pop members (remove and return)
await redis.zadd("items", { score: 1, member: "a" }, { score: 2, member: "b" });
const lowest = await redis.zpopmin("items"); // Remove and return lowest score
const highest = await redis.zpopmax("items"); // Remove and return highest score

// Pop multiple members
await redis.zadd(
  "items",
  { score: 1, member: "a" },
  { score: 2, member: "b" },
  { score: 3, member: "c" }
);
const twoLowest = await redis.zpopmin("items", 2); // Remove 2 lowest

// Practical example: Time-series data
const now = Date.now();
await redis.zadd(
  "events",
  { score: now - 3600000, member: "event1" }, // 1 hour ago
  { score: now - 1800000, member: "event2" }, // 30 min ago
  { score: now, member: "event3" } // now
);

// Get events from last hour
const lastHour = await redis.zrange("events", now - 3600000, now, { byScore: true });

// Delete entire sorted set
await redis.del("leaderboard");

Source: SKILL.md on GitHub

1 warning10d3 checks · Risk SAFE
  • Gen Agent Trust Hub10d

    The skill provides a comprehensive set of documentation and code examples for using the Upstash Redis SDK. It covers various data structures, performance optimizations, and search capabilities. No malicious code, obfuscation, or unauthorized data access patterns were detected. The skill uses standard environment variables for secret management and references official Upstash packages.

  • Socket10d

    1 alert: gptSecurity

  • Snyk10d

    Risk: LOW · No issues

Signed by skilld at 35da719. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 6 days ago.

Activeupdated 2 weeks ago
metadata
{
  "author": "Upstash",
  "homepage": "https://upstash.com"
}

README badge

README badge for upstash/skills/upstash-redis-js