Support Upstash Redis REST SDK in useResponseCache with Redis cache as store
See original GitHub issueCurrently, useReponseCache
offers a LRU and a Redis cache option.
The redis-cache relies on ioredis
as seen here: https://github.com/dotansimha/envelop/blob/cdb32401bc385ca4d3503c4bc6b23ddb1a5909c6/packages/plugins/response-cache-redis/src/redis-cache.ts#L1
/**
* Redis instance
* @see Redis.Redis https://github.com/luin/ioredis
*/
redis: Redis.Redis;
And then any gets, sets, keys, or smember checks use the Redis client; for example:
// find the responseIds for the entity
const responseIds = await store.smembers(entity);
Describe the solution you’d like
Upstash is a serverless database service compatible with Redis® API.
They offer a REST API and a Javascript SDK: https://docs.upstash.com/redis/features/javascriptsdk
The advantage of this over a Redis client in the serverless world (where your GraphQL queries will be done in a Lambda like with RedwoodJS) is that it’s less likely to run into connection limits. Other Redis offerings have 10, 20, 40 connections and base pricing on that where with Upstash the pricing is per usage.
Use upstash-redis in serverless functions if you expect high number of concurrent connections
Serverless functions scale up fast. This can cause some issues if you need persistent connections. REST based upstash-redis fits better in such cases as it does not require a TCP connection with its stateless design.
Also, the Rest client works well on the Edge in a Netlify or Vercel edge handler/function.
However, because the current cache implementation needs a client to invoke get
etc, one cannot easily use the Upstash SDK:
For example (their sample code):
import { auth, set } from '@upstash/redis';
(async () => {
try {
auth('UPSTASH_REDIS_REST_URL', 'UPSTASH_REDIS_REST_TOKEN');
const { data, error } = await set('key', 'value');
if (error) throw error;
console.log(data);
// -> "OK"
} catch (error) {
console.error(error);
}
})();
Notice that here set
is a method, but not like store.set()
on the client.
Perhaps create another redis cache that uses Upstash specifically?
Would this be part of https://github.com/dotansimha/envelop/tree/main/packages/plugins/response-cache-redis/src or an entirely new plugin just for Upstash?
Other
Or, perhaps there is a way to modify the SDK such that there is a client that can be imported (here? https://github.com/upstash/upstash-redis/blob/master/src/index.ts) and then the plugin can have as is?
- Question: The REST API supports
pipeline
but not 100% certain if the SDK does.
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Hey @dthyresson The @upstash/redis api is not compatible with ioredis, because we wanted to provide a nicer typescript experience, however it would be pretty easy to write a small adapter.
Judging from the readme example, I should probably just create another package that exports
createUpstashCache(..)
So you would use it like this:
Please let me know what you think and I’ll implement it afterwards
Hi. I’ll try too look too as well but Tim from the RedwoodJS community has offered to help as well; see: https://community.redwoodjs.com/t/guide-power-of-graphql-caching/2624/21?u=dthyresson