Github: https://github.com/mahdavipanah/keyv-upstash
keyv-upstash is a storage adapter for Keyv that connects it to Upstash Redis, a serverless Redis platform. With this adapter, you get a simple, efficient, and flexible solution for key-value storage in serverless applications.
Keyv is a versatile key-value storage library that supports multiple backends through adapters. It provides:
TTL-based Expiry: Ideal for caching or persistent storage.
Namespace Support: Avoids key collisions in shared environments.
Extensibility: Easy to build custom modules or add features like compression.
Keyv works with many adapters, such as Redis, SQLite, MongoDB, and now, keyv-upstash for Upstash Redis.
keyv-upstash extends Keyv's capabilities by integrating it with Upstash Redis, offering:
Serverless Compatibility: Upstash Redis works without managing connections, scaling automatically, perfect for serverless apps.
Flexible: Compatible with Keyv’s ecosystem and supports third-party extensions.
Cache Layering: Combine with Cacheable for multi-layered caching.
No Vendor Lock-In: Is fully compatible with serverless-redis-http so you can setup your own serverless Redis and use this adapter with it.
Follow these steps to integrate keyv-upstash:
Install Keyv and the Upstash adapter:
npm install keyv keyv-upstash
Optional: Install Cacheable for layered caching:
npm install cacheable
Make sure you have a Redis database created in Upstash. Here’s how to use keyv-upstash in your project:
import Keyv from 'keyv'; import { KeyvUpstash } from 'keyv-upstash'; const keyv = new Keyv({ store: new KeyvUpstash({ url: 'your-upstash-redis-url', token: 'your-upstash-redis-token', }), }); // Set a key-value pair await keyv.set('foo', 'bar'); // Retrieve the value const value = await keyv.get('foo'); console.log(value); // 'bar'
Namespaces prevent key collisions and allow scoped clearing:
const keyv = new Keyv({ store: new KeyvUpstash({ url: 'your-upstash-redis-url', token: 'your-upstash-redis-token', namespace: 'my-namespace', }), }); await keyv.set('foo', 'bar'); // Stored as 'my-namespace::foo'
Combine keyv-upstash with Cacheable for multi-layer caching:
import { Cacheable } from 'cacheable'; const redisStore = new KeyvUpstash({ url: 'your-upstash-redis-url', token: 'your-upstash-redis-token', }); const cache = new Cacheable({ primary: new Map(), // Fast in-memory caching secondary: redisStore, // Persistent Redis caching }); await cache.set('foo', 'bar', { ttl: 1000 }); // Stores in both layers const value = await cache.get('foo'); // Fast lookup from memory or Redis console.log(value); // 'bar'
Improve performance with setMany and getMany:
await keyv.setMany([ { key: 'key1', value: 'value1' }, { key: 'key2', value: 'value2' }, ]); const values = await keyv.getMany(['key1', 'key2']); console.log(values); // ['value1', 'value2']
Customize your setup with options like defaultTtl, keyPrefixSeparator, and clearBatchSize.
The above is the detailed content of Introducing keyv-upstash: Seamless Key-Value Storage for Serverless Redis. For more information, please follow other related articles on the PHP Chinese website!