Runtime Caches
These recipes let an administrator enumerate and manage the runtime caches for R and Python. Posit Connect populates these caches with the package dependencies for your content and makes those packages available when running your content.
Enumerate Runtime Caches
This example uses the GET /v1/system/caches/runtime
endpoint to enumerate the runtime caches created by Posit Connect.
library(httr)
# The connectServer URL must have a trailing slash.
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
<- GET(
resp paste0(connectServer, "__api__/v1/system/caches/runtime"),
add_headers(Authorization = paste("Key", connectAPIKey))
)<- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
details print(details$caches)
import os
import requests
import pprint
# The connectServer URL must have a trailing slash.
= os.getenv("CONNECT_SERVER")
connectServer = os.getenv("CONNECT_API_KEY")
connectAPIKey
= requests.get(
resp f"{connectServer}__api__/v1/system/caches/runtime",
= {
headers "Authorization": f"Key {connectAPIKey}",
},
)= resp.json()
payload "caches"]) pprint.pprint(payload[
Delete Runtime Caches
This example uses the DELETE /v1/system/caches/runtime
to request the removal of a named runtime cache.
The GET /v1/tasks/{id}
endpoint is used to poll for completion of that requested operation.
library(httr)
# The connectServer URL must have a trailing slash.
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
= list(
instructions language = "R",
version = "3.6.3",
image_name = "Local"
)
<- DELETE(
resp paste0(connectServer, "__api__/v1/system/caches/runtime"),
add_headers(Authorization = paste("Key", connectAPIKey)),
body = instructions,
encode = "json"
)<- as.data.frame(httr::content(resp, as = "parsed"))
details print(details)
<- details$task_id
taskId while(TRUE) {
<- GET(
resp paste0(connectServer, "__api__/v1/tasks/", taskId, "?wait=1"),
add_headers(Authorization = paste("Key", connectAPIKey))
)<- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
details if (details$finished) {
if (nchar(details$error) > 0) {
cat(paste0("operation erred: ", details$error, "\n"))
else {
} cat("operation complete.\n")
}break
} }
import os
import requests
import pprint
# The connectServer URL must have a trailing slash.
= os.getenv("CONNECT_SERVER")
connectServer = os.getenv("CONNECT_API_KEY")
connectAPIKey
= {
instructions "language": "Python",
"version": "3.8.2",
"image_name": "Local"
}
= requests.delete(
resp f"{connectServer}__api__/v1/system/caches/runtime",
= {
headers "Authorization": f"Key {connectAPIKey}",
},= instructions,
json
)= resp.json()
payload
pprint.pprint(payload)
= payload["task_id"]
taskId while True:
= requests.get(
resp f"{connectServer}__api__/v1/tasks/{taskId}?wait=1",
= {
headers "Authorization": "Key " + connectAPIKey,
}
)= resp.json()
payload if payload["finished"]:
if payload["error"]:
print("operation erred: "+payload["error"])
else:
print("operation complete.")
break