Skip to content

Add default TTL to Cloudflare KV Binding #687

@Buzut

Description

@Buzut

Describe the feature

Currently, unlike Redis, there isn't a default TTL option for Cloudflare KV Binding.

Nevertheless, it's currently possible to set the TTL manually when invoking setItem. Would be a huge addition to allow for a default TTL to be applied (if not overridden by custom TTL).

This would allow, among other things, auto pruning of stale cache entries when using KV as the default cache.

// I had something like so in mind
// /drivers/cloudflare-kv-binding.ts
setItem(key, value, topts) {
  key = r(key);
  const binding = getKVBinding(opts.binding);

  // Determine which TTL to use
  let expirationTtl: number | undefined = undefined;
  if (topts?.ttl) {
    expirationTtl = Math.max(topts.ttl, opts.minTTL ?? 60);
  } else if (opts.ttl) {
    expirationTtl = Math.max(opts.ttl, opts.minTTL ?? 60);
  } else if (opts.minTTL) {
    expirationTtl = opts.minTTL;
  }

  return binding.put(
    key,
    value,
    {
      ...topts,
      expirationTtl,
    }
  );
},

Additional information

  • Would you be willing to help implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions