mirror of
https://github.com/AderKonstantin/aderktech-chronark.com-.git
synced 2025-06-08 13:48:42 +03:00
87 lines
2.0 KiB
Plaintext
87 lines
2.0 KiB
Plaintext
---
|
|
title: "@upstash/redis"
|
|
description: A fully typed Redis client built for Upstash Redis and HTTP, perfect for serverless and edge runtimes.
|
|
date: "2022-03-14"
|
|
url: https://upstash.com/redis
|
|
repository: upstash/upstash-redis
|
|
|
|
---
|
|
|
|
|
|
`@upstash/redis` is an HTTP/REST based Redis client for typescript, built on top
|
|
of [Upstash REST API](https://docs.upstash.com/features/restapi).
|
|
|
|
It is the only connectionless (HTTP based) Redis client and designed for:
|
|
|
|
- Serverless functions (AWS Lambda ...)
|
|
- Cloudflare Workers (see
|
|
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers))
|
|
- Fastly Compute@Edge (see
|
|
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly))
|
|
- Next.js, Jamstack ...
|
|
- Client side web/mobile applications
|
|
- WebAssembly
|
|
- and other environments where HTTP is preferred over TCP.
|
|
|
|
See
|
|
[the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
|
|
supported.
|
|
|
|
## Quick Start
|
|
|
|
### Install
|
|
|
|
#### Node.js
|
|
|
|
```bash
|
|
npm install @upstash/redis
|
|
```
|
|
|
|
#### Deno
|
|
|
|
```ts
|
|
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
|
|
```
|
|
|
|
### Create database
|
|
|
|
Create a new redis database on [upstash](https://console.upstash.com/)
|
|
|
|
## Basic Usage:
|
|
|
|
```ts
|
|
import { Redis } from "@upstash/redis"
|
|
|
|
const redis = new Redis({
|
|
url: <UPSTASH_REDIS_REST_URL>,
|
|
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
})
|
|
|
|
// string
|
|
await redis.set('key', 'value');
|
|
let data = await redis.get('key');
|
|
console.log(data)
|
|
|
|
await redis.set('key2', 'value2', {ex: 1});
|
|
|
|
// sorted set
|
|
await redis.zadd('scores', { score: 1, member: 'team1' })
|
|
data = await redis.zrange('scores', 0, 100 )
|
|
console.log(data)
|
|
|
|
// list
|
|
await redis.lpush('elements', 'magnesium')
|
|
data = await redis.lrange('elements', 0, 100 )
|
|
console.log(data)
|
|
|
|
// hash
|
|
await redis.hset('people', {name: 'joe'})
|
|
data = await redis.hget('people', 'name' )
|
|
console.log(data)
|
|
|
|
// sets
|
|
await redis.sadd('animals', 'cat')
|
|
data = await redis.spop('animals', 1)
|
|
console.log(data)
|
|
```
|