Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP add Effect for a KV cache #17

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ examples/sleep
examples/file
examples/dir
examples/env
examples/command
examples/command
examples/cache
25 changes: 25 additions & 0 deletions examples/cache.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
app "cache"
packages { pf: "../platform/main.roc" }
imports [
# pf.Stdout,
pf.Cache,
pf.Task.{ Task },
pf.Http.{ Request, Response },
]
provides [main] to pf

main : Request -> Task Response []
main = \_ ->

key = 123
value = "The value is 123" |> Str.toUtf8

# Store value in cache
{} <- Cache.set key value |> Task.await

# Retrieve value from cache
result <- Cache.get key |> Task.attempt

when result is
Ok bytes if bytes == value -> Task.ok { status: 200, headers: [], body: value }
_ -> Task.ok { status: 500, headers: [], body: Str.toUtf8 "Unable to retrieve value from cache"}
15 changes: 15 additions & 0 deletions platform/Cache.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface Cache
exposes [get,set]
imports [Effect, InternalTask, Task.{ Task }]

get : U64 -> Task (List U8) [NotFound]
get = \key ->
Effect.getKV key
|> InternalTask.fromEffect


set : U64, List U8 -> Task {} *
set = \key, value ->
Effect.setKV key value
|> Effect.map Ok
|> InternalTask.fromEffect
6 changes: 6 additions & 0 deletions platform/Effect.roc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ hosted Effect
sleepMillis,
commandStatus,
commandOutput,
getKV,
setKV,
]
imports [
InternalHttp,
Expand Down Expand Up @@ -86,3 +88,7 @@ sleepMillis : U64 -> Effect {}

commandStatus : Box InternalCommand.InternalCommand -> Effect (Result {} InternalCommand.InternalCommandErr)
commandOutput : Box InternalCommand.InternalCommand -> Effect InternalCommand.InternalOutput

# Cache
getKV : U64 -> Effect (Result (List U8) InternalError.CacheError)
setKV : U64, List U8 -> Effect {}
6 changes: 4 additions & 2 deletions platform/InternalError.roc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
interface InternalError
exposes [InternalError, InternalDirReadErr, InternalDirDeleteErr]
exposes [InternalError, CacheError, InternalDirReadErr, InternalDirDeleteErr]
imports [Path.{ Path }]

InternalError : [
IOError Str,
IOError Str,
EOF,
]

CacheError : [NotFound]

InternalDirReadErr : [DirReadErr Path Str]

InternalDirDeleteErr : [DirDeleteErr Path Str]
4 changes: 4 additions & 0 deletions platform/glue-gen.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env bash

# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail

# Remove old glue flies
rm -rf platform/src/glue
Expand Down
3 changes: 2 additions & 1 deletion platform/main-manual-glue.roc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ GlueTypes : [
M InternalFile.WriteErr,
N InternalError.InternalDirReadErr,
O InternalError.InternalDirDeleteErr,
P InternalPath.UnwrappedPath,
P InternalError.CacheError,
Q InternalPath.UnwrappedPath,
]

mainForHost : GlueTypes
Expand Down
5 changes: 3 additions & 2 deletions platform/src/glue/roc_std/src/lib.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions platform/src/glue/roc_std/src/roc_box.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 82 additions & 2 deletions platform/src/glue/roc_std/src/roc_list.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading