mirror of
https://github.com/unjs/unstorage.git
synced 2025-12-08 21:26:09 +00:00
feat(redis): support native ttl (#169)
This commit is contained in:
parent
f97b449d68
commit
97e01491db
@ -49,7 +49,12 @@ const storage = createStorage({
|
|||||||
- `url`: Url to use for connecting to redis. Takes precedence over `host` option. Has the format `redis://<REDIS_USER>:<REDIS_PASSWORD>@<REDIS_HOST>:<REDIS_PORT>`
|
- `url`: Url to use for connecting to redis. Takes precedence over `host` option. Has the format `redis://<REDIS_USER>:<REDIS_PASSWORD>@<REDIS_HOST>:<REDIS_PORT>`
|
||||||
- `cluster`: List of redis nodes to use for cluster mode. Takes precedence over `url` and `host` options.
|
- `cluster`: List of redis nodes to use for cluster mode. Takes precedence over `url` and `host` options.
|
||||||
- `clusterOptions`: Options to use for cluster mode.
|
- `clusterOptions`: Options to use for cluster mode.
|
||||||
|
- `ttl`: Default TTL for all items in **seconds**.
|
||||||
|
|
||||||
See [ioredis](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options) for all available options.
|
See [ioredis](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options) for all available options.
|
||||||
|
|
||||||
`lazyConnect` option is enabled by default so that connection happens on first redis operation.
|
`lazyConnect` option is enabled by default so that connection happens on first redis operation.
|
||||||
|
|
||||||
|
**Transaction options:**
|
||||||
|
|
||||||
|
- `ttl`: Supported for `setItem(key, value, { ttl: number /* seconds */ })`
|
||||||
|
|||||||
@ -26,6 +26,11 @@ export interface RedisOptions extends _RedisOptions {
|
|||||||
* Options to use for cluster mode.
|
* Options to use for cluster mode.
|
||||||
*/
|
*/
|
||||||
clusterOptions?: ClusterOptions;
|
clusterOptions?: ClusterOptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default TTL for all items in seconds.
|
||||||
|
*/
|
||||||
|
ttl?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineDriver((opts: RedisOptions = {}) => {
|
export default defineDriver((opts: RedisOptions = {}) => {
|
||||||
@ -60,8 +65,13 @@ export default defineDriver((opts: RedisOptions = {}) => {
|
|||||||
const value = await getRedisClient().get(p(key));
|
const value = await getRedisClient().get(p(key));
|
||||||
return value === null ? null : value;
|
return value === null ? null : value;
|
||||||
},
|
},
|
||||||
async setItem(key, value) {
|
async setItem(key, value, tOptions) {
|
||||||
await getRedisClient().set(p(key), value);
|
let ttl = tOptions?.ttl ?? opts.ttl;
|
||||||
|
if (ttl) {
|
||||||
|
await getRedisClient().set(p(key), value, "EX", tOptions.ttl);
|
||||||
|
} else {
|
||||||
|
await getRedisClient().set(p(key), value);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async removeItem(key) {
|
async removeItem(key) {
|
||||||
await getRedisClient().del(p(key));
|
await getRedisClient().del(p(key));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user