unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

[redis] support native binary storage

Open cjpearson opened this issue 11 months ago • 1 comments

Describe the feature

Currently the redis driver does not implement getItemRaw/setItemRaw. It uses the default method of serializeRawData + setItem.

Since ioredis can binary data, it should be unnecessary to encode it as base64. Directly setting a Buffer would have less overhead and be more space-efficient. However, there would be compatibility issues with existing data. Perhaps it could be implemented as an option or separate driver?

https://github.com/redis/ioredis#handle-binary-data

Here's an example implementation. It would still need to be updated to handle non-Buffer values.

async getItemRaw(key) {
  const value = await getRedisClient().getBuffer(p(key));
  return value ?? null;
},
async setItemRaw(key, value, tOptions) {
  if (value instanceof Uint8Array) {
    value = Buffer.from(value, value.byteOffset, value.byteLength)
  }
  const ttl = tOptions?.ttl ?? opts.ttl;
  if (ttl) {
    await getRedisClient().setBuffer(p(key), value, "EX", ttl);
  } else {
    await getRedisClient().setBuffer(p(key), value);
  }
},

This could perhaps be done with https://github.com/unjs/unstorage/issues/528 and I think that would avoid the breaking aspects. For example setItemRaw could continue to base64 encode and save as a string, but setItem(k, v, { type: 'bytes'}), could save as binary.

Additional information

  • [X] Would you be willing to help implement this feature?

cjpearson avatar Jan 02 '25 12:01 cjpearson

Feel free to make a PR 👍🏼

pi0 avatar Jan 02 '25 13:01 pi0