Interface KVNamespace

All Superinterfaces:
org.teavm.jso.JSObject

public interface KVNamespace extends org.teavm.jso.JSObject
A Workers KV namespace. Declared with kv(), named KV by default.

Reads are eventually consistent and cached at the edge for at least 60 seconds. A write is visible in the region that made it straight away and elsewhere within about a minute, so a read-after-write in another region can return the old value.

Every method here blocks and returns a value. Nothing needs a callback or a future.

 KVNamespace kv = env.kv();
 String visits = kv.get("visits");
 kv.put("visits", String.valueOf(visits == null ? 1 : Integer.parseInt(visits) + 1));
Since:
1.0.0
  • Method Details

    • get

      default String get(String key)
      Reads a value as text.
      Parameters:
      key - the key
      Returns:
      the value, or null when the key is absent
    • getJson

      default TSObject getJson(String key)
      Reads a value and parses it as JSON.
      Parameters:
      key - the key
      Returns:
      the parsed value, or null when the key is absent
    • getBytes

      default org.teavm.jso.typedarrays.ArrayBuffer getBytes(String key)
      Reads a value as bytes.
      Parameters:
      key - the key
      Returns:
      the bytes, or null when the key is absent
    • getWithMetadata

      default KVNamespace.Entry getWithMetadata(String key)
      Reads a value together with the metadata stored beside it.
      Parameters:
      key - the key
      Returns:
      the value and its metadata; KVNamespace.Entry.value() is null when absent
    • put

      default void put(String key, String value)
      Writes a value.
      Parameters:
      key - the key
      value - the value
    • put

      default void put(String key, String value, int ttlSeconds)
      Writes a value that expires.
      Parameters:
      key - the key
      value - the value
      ttlSeconds - how long the value lives, at least 60
    • putWithMetadata

      default void putWithMetadata(String key, String value, TSObject metadata)
      Writes a value with metadata, which list() returns without a second read.
      Parameters:
      key - the key
      value - the value
      metadata - the metadata, at most 1024 bytes once serialised
    • putBytes

      default void putBytes(String key, org.teavm.jso.typedarrays.ArrayBuffer value)
      Writes bytes.
      Parameters:
      key - the key
      value - the bytes
    • putBytes

      default void putBytes(String key, byte[] value)
      Writes bytes.
      Parameters:
      key - the key
      value - the bytes
    • delete

      default void delete(String key)
      Removes a key. Succeeds whether or not it was present.
      Parameters:
      key - the key
    • list

      default KVNamespace.Listing list()
      Returns the first page of keys.
      Returns:
      the first page of keys
    • list

      default KVNamespace.Listing list(String prefix)
      Lists keys under a prefix.
      Parameters:
      prefix - the prefix
      Returns:
      the first page of matching keys
    • list

      default KVNamespace.Listing list(String prefix, String cursor, int limit)
      Lists keys from where a previous page left off.
      Parameters:
      prefix - the prefix, or null for all keys
      cursor - KVNamespace.Listing.getCursor() from the previous page
      limit - how many keys to return, at most 1000
      Returns:
      the next page
    • listAll

      default List<String> listAll(String prefix)
      Returns every key under a prefix, following the cursor until the listing completes.
      Returns:
      every key under a prefix, following the cursor until the listing completes
    • getText

      org.teavm.jso.core.JSPromise<org.teavm.jso.core.JSString> getText(String key)
    • getWithType

      org.teavm.jso.core.JSPromise<TSObject> getWithType(String key, String type)
    • getBuffer

      org.teavm.jso.core.JSPromise<org.teavm.jso.typedarrays.ArrayBuffer> getBuffer(String key, String type)
    • getMetadata

      org.teavm.jso.core.JSPromise<KVNamespace.Entry> getMetadata(String key, String type)
    • putText

      org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> putText(String key, String value)
    • putText

      org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> putText(String key, String value, org.teavm.jso.JSObject options)
    • putBuffer

      org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> putBuffer(String key, org.teavm.jso.typedarrays.ArrayBuffer value)
    • remove

      org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> remove(String key)
    • listKeys

      org.teavm.jso.core.JSPromise<KVNamespace.Listing> listKeys(org.teavm.jso.JSObject options)