Runtime Caches
These recipes let an administrator enumerate and manage the runtime caches for Python and R. 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 Connect.
import os
import requests
import pprint
# The connect_server URL must have a trailing slash.
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key
= requests.get(
resp f"{connect_server}__api__/v1/system/caches/runtime",
= {
headers "Authorization": f"Key {connect_api_key}",
},
)= resp.json()
payload "caches"]) pprint.pprint(payload[
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)
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.
import os
import pprint
import requests
# The connect_server URL must have a trailing slash.
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key
= {
instructions "language": "Python",
"version": "3.8.2",
"image_name": "Local"
}
= requests.Session()
session
= { "Authorization": f"Key {connect_api_key}" }
headers
= session.delete(
resp f"{connect_server}__api__/v1/system/caches/runtime",
= headers,
headers = instructions,
json
)= resp.json()
payload
pprint.pprint(payload)
if "task_id" in payload:
= payload["task_id"]
task_id while True:
print("waiting for task to finish...")
= session.get(
resp f"{connect_server}__api__/v1/tasks/{task_id}?wait=1",
= headers,
headers
)= resp.json()
payload if payload["finished"]:
if payload["error"]:
print("operation erred: " + payload["error"])
else:
print("operation complete.")
break
else:
print("Error: no task id returned by delete request")
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
} }