diff --git a/cache-sync.wit b/cache-sync.wit new file mode 100644 index 0000000..01e0d8e --- /dev/null +++ b/cache-sync.wit @@ -0,0 +1,53 @@ +/// FastEdge cache interface (synchronous variant). +interface cache-sync { + use cache-types.{payload, error}; + + /// Get the value associated with `key`. + /// + /// Returns: + /// - `ok(some(value))` if the key exists. + /// - `ok(none)` if the key does not exist. + /// - `err(error)` if the operation fails. + get: func(key: string) -> result, error>; + + /// Set the value for `key` with an optional expiry. + /// + /// If the key already exists, its current value is overwritten. + /// If the key does not exist, a new key-value pair is created. + /// + /// `ttl-ms` is the time-to-live in milliseconds. Pass `none` for no expiry. + /// + /// Returns `err(error)` if the operation fails. + set: func(key: string, value: payload, ttl-ms: option) -> result<_, error>; + + /// Delete the key-value pair associated with `key`. + /// + /// If the key does not exist, this operation is a no-op. + /// + /// Returns `err(error)` if the operation fails. + delete: func(key: string) -> result<_, error>; + + /// Check whether `key` exists in the cache. + /// + /// Returns: + /// - `ok(true)` if the key exists. + /// - `ok(false)` if the key does not exist. + /// - `err(error)` if the operation fails. + exists: func(key: string) -> result; + + /// Increment the integer value stored at `key` by `delta`. + /// + /// If the key does not exist, it is initialised to `0` before incrementing. + /// The operation is atomic. `delta` may be negative to decrement. + /// + /// Returns the new value after the increment, or `err(error)` if the + /// operation fails (for example, if the stored value is not an integer). + incr: func(key: string, delta: s64) -> result; + + /// Set or update the expiry of `key` to `ttl-ms` milliseconds from now. + /// + /// If the key does not exist, returns `ok(false)`. + /// If the expiry was updated successfully, returns `ok(true)`. + /// Returns `err(error)` if the operation fails. + expire: func(key: string, ttl-ms: u64) -> result; +} diff --git a/cache-types.wit b/cache-types.wit new file mode 100644 index 0000000..ea98bf8 --- /dev/null +++ b/cache-types.wit @@ -0,0 +1,15 @@ +/// Shared cache types for cache interfaces. +interface cache-types { + type payload = list; + + /// The set of errors that may be returned by cache operations. + variant error { + /// The requesting component does not have access to the specified cache + /// (which may or may not exist). + access-denied, + /// An unexpected internal error occurred. + internal-error, + /// An implementation-specific error occurred (for example, I/O). + other(string) + } +} diff --git a/world.wit b/world.wit index 33587c9..f319593 100644 --- a/world.wit +++ b/world.wit @@ -7,6 +7,7 @@ world reactor { import secret; import key-value; import utils; + import cache-sync; export http-handler; } \ No newline at end of file