Drivers
Redis
Store data in a Redis.
Usage
Unstorage uses
ioredis
internally to connect to Redis.Usage with single Redis instance:
import { createStorage } from "unstorage";
import redisDriver from "unstorage/drivers/redis";
const storage = createStorage({
driver: redisDriver({
base: "unstorage",
host: 'HOSTNAME',
tls: true as any,
port: 6380,
password: 'REDIS_PASSWORD'
}),
});
Usage with Redis cluster (e.g. AWS ElastiCache or Azure Redis Cache):
⚠️ If you connect to a cluster, you have to use hastags
as prefix to avoid the redis error CROSSSLOT Keys in request don't hash to the same slot
. This means, the prefix has to be surrounded by curly braces, which forces the keys into the same hash slot.
const storage = createStorage({
driver: redisDriver({
base: "{unstorage}",
cluster: [
{
port: 6380,
host: "HOSTNAME",
},
],
clusterOptions: {
redisOptions: {
tls: { servername: "HOSTNAME" },
password: "REDIS_PASSWORD",
},
},
}),
});
Options:
base
: Optional prefix to use for all keys. Can be used for namespacing. Has to be used as hastag prefix for redis cluster mode.url
: Url to use for connecting to redis. Takes precedence overhost
option. Has the formatredis://<REDIS_USER>:<REDIS_PASSWORD>@<REDIS_HOST>:<REDIS_PORT>
cluster
: List of redis nodes to use for cluster mode. Takes precedence overurl
andhost
options.clusterOptions
: Options to use for cluster mode.ttl
: Default TTL for all items in seconds.
See ioredis for all available options.
lazyConnect
option is enabled by default so that connection happens on first redis operation.
Transaction options:
ttl
: Supported forsetItem(key, value, { ttl: number /* seconds */ })