วันนี้เราประกาศ @upstash/redis v1.0.0
@upstash/redis เป็น Redis Client แบบ Connectionless (HTTP-based) 唯一ตัวที่ออกแบบสำหรับ Serverless และเข้ากันได้กับ Cloudflare Workers และ Fastly
อะไรใหม่ใน v1.0.0?
Version 1.0.0 เป็น Complete Rewrite ของ Library ก่อนหน้า Feature Requests ใหญ่ที่สุดที่ได้รับคือ Pipeline Support และ Improved Typesafety เนื่องจากลักษณะของ Features ใหม่เหล่านี้ v1 ไม่ Backwards Compatible แต่ Version เก่ายังทำงานได้ เพราะ Underlying REST API ไม่เปลี่ยน
Commands ส่วนใหญ่ใช้ Syntax เดิมที่คุ้นเคยกับ Redis เช่น redis.setex("key", 2, "value") สำหรับบางอย่าง เช่น hset เราเสนอ API ที่ใช้ง่ายกว่า ลด (De)serialization ที่ต้องทำเอง:
await redis.hset("123", {
id: "123",
name: { first: "andreas" },
});
Internal จะ Serialize เป็น hset 123 id 123 name {"first":"andreas"} ส่งไป Upstash Objects ถูก Serialize อัตโนมัติ ไม่ต้องทำเองแล้ว
หากไม่แน่ใจ Command ใช้ยังไง TypeScript ช่วยได้ถ้า Editor รองรับ Intellisense ดู Test ที่ Cover ทุกอย่างและเป็น Examples ได้
Changes สำคัญที่สุด:
- Explicit Authentication
- Optimized สำหรับ Platforms ต่างๆ (Node.js, Cloudflare Workers, Fastly)
- Improved Type System
- Errors ถูก Throw
- Pipeline Support
Explicit Authentication
Automatic Authentication ตอน Import Specific Functions ทำให้ Behavior จริงต่างจากที่คาดหวัง 导致 Wasted Time Debugging ปัญหาที่ป้องกันได้ โดยเฉพาะ Platforms ต่างๆ ที่ Handle Environment Variables ต่างกัน ดังนั้นเรากำหนดให้ Authenticate Client อย่างชัดเจน
เราเข้าใจว่า Use Cases ส่วนใหญ่เก็บใน Environment และ Require แค่ครั้งเดียว Setup Redis Client @upstash/redis มี Shortcut: ตั้ง UPSTASH_REDIS_REST_URL และ UPSTASH_REDIS_REST_TOKEN เป็น Environment Variable แล้ว Load บน Deployment Platform ที่เลือก หากไม่ Configure ถูกต้อง จะ Throw Error
Node.js
import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
Cloudflare Workers
import { Redis } from "@upstash/redis/cloudflare";
const redis = Redis.fromEnv();
Typescript
Version ใหม่รองรับ Strict Types ทุก Command ช่วยหา Mistakes เร็วกว่า Types เป็น Optional และ Default Infer Types ได้ทุกที่
import { Redis } from "@upstash/redis";
const redis = new Redis({ /* auth */ });
type User = { id: string; email: string; };
const user = { id: "abc", email: "andreas@upstash.com" };
await redis.set<User>("userKey", user);
const user = await redis.get<User>("userKey");
// -> user is typed as User
Redis มี Commands มากมาย Optional Arguments และ Combinations ต่างๆ TypeScript Check Invalid Combinations ก่อน Execute Code
redis.set("key", "value", {
ex: 60, // expire in 60 seconds
nx: true, // only create if key does not yet exist
});
Error Handling
Version ก่อน Return Raw Response จาก Upstash HTTP API ต้อง Check Error เองแล้ว Throw ใน JavaScript คาดหวังว่า Error ถูก Throw เมื่อผิดพลาด เราทำเช่นนั้น
ตอนนี้ Commands Return Data อย่างเดียว และ Auto Throw หากผิดพลาด
// `v0.2.1`
const { data, error } = await set("key", "value");
if (error) {
throw new Error(error);
}
// '^v1.0.0'
const data = await redis.set("key", "value");
Pipelines
Upstash รองรับ Pipelining Redis Commands ผ่าน HTTP Batch Requests รวมส่ง Bulk ลด Execution Time และประหยัดเงิน โดยเฉพาะ Serverless Functions
:::note Pipeline Execution ไม่ใช่ Atomic แม้แต่ละ Command ใน Pipeline จะ Execute In Order Commands จาก Clients อื่นอาจ Interleave กับ Pipeline :::
สร้าง Pipeline ใหม่จาก redis Instance:
import { Redis } from "@upstash/redis";
const redis = new Redis({ /* auth */ });
const p = redis.pipeline();
Pipeline API ออกแบบให้ Chain Commands ได้ หลัง Add Commands ทั้งหมด ส่ง Request รับ Array ทุก Element ตรงกับ Command เรียงลำดับเดียวกัน
p.set("key", 2);
// หรือ Chain โดยตรง
p.incr("key").decrby("key", 2);
const res = await p.exec();
// res[0] => "OK" | result of p.set("key", 2)
// res[1] => 3 | result of p.incr("key")
// res[2] => 1 | result of p.decrby("key", 2)
How do I get started?
ไป console.upstash.com สร้าง Database ใหม่ ตาม Instructions ใน Quick-start Guide
หากใช้ Version เก่า ดู Migrate to v1 Section on GitHub
Future Work
Upstash Redis อัปเดตเข้ากันได้กับ redis v6.2 รวม Commands ใหม่และ Extend อื่นๆ @upstash/redis จะรองรับเร็วๆ นี้
Platform เพิ่มเติม: วางแผนรองรับ Deno เร็วๆ นี้
Closing Words
การเปลี่ยนแปลงใหญ่นี้ต้องใช้ความพยายามจากคุณในการ Update โปรดแจ้ง Experience ของคุณ
เรายินดีรับ Feedback เพื่อปรับปรุง Experience ทำงานกับ Upstash Products อย่า猶豫 ติดต่อ GitHub, Discord หรือ Twitter สำหรับ Bugs, Feature Requests หรือ Help