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.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

resp <- GET(
  paste0(connectServer, "__api__/v1/system/caches/runtime"),
  add_headers(Authorization = paste("Key", connectAPIKey))
)
details <- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
print(details$caches)
import os
import requests
import pprint

# The connectServer URL must have a trailing slash.
connectServer = os.getenv("CONNECT_SERVER")
connectAPIKey = os.getenv("CONNECT_API_KEY")

resp = requests.get(
    f"{connectServer}__api__/v1/system/caches/runtime",
    headers = {
        "Authorization": f"Key {connectAPIKey}",
    },
)
payload = resp.json()
pprint.pprint(payload["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.

library(httr)

# The connectServer URL must have a trailing slash.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

instructions = list(
  language = "R",
  version = "3.6.3",
  image_name = "Local"
)

resp <- DELETE(
  paste0(connectServer, "__api__/v1/system/caches/runtime"),
  add_headers(Authorization = paste("Key", connectAPIKey)),
  body = instructions,
  encode = "json"
)
details <- as.data.frame(httr::content(resp, as = "parsed"))
print(details)

taskId <- details$task_id
while(TRUE) {
  resp <- GET(
    paste0(connectServer, "__api__/v1/tasks/", taskId, "?wait=1"),
    add_headers(Authorization = paste("Key", connectAPIKey))
  )
  details <- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
  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.
connectServer = os.getenv("CONNECT_SERVER")
connectAPIKey = os.getenv("CONNECT_API_KEY")

instructions = {
    "language": "Python",
    "version": "3.8.2",
    "image_name": "Local"
}

resp = requests.delete(
    f"{connectServer}__api__/v1/system/caches/runtime",
    headers = {
        "Authorization": f"Key {connectAPIKey}",
    },
    json = instructions,
)
payload = resp.json()
pprint.pprint(payload)

taskId = payload["task_id"]
while True:
    resp = requests.get(
        f"{connectServer}__api__/v1/tasks/{taskId}?wait=1",
        headers = {
            "Authorization": "Key " + connectAPIKey,
        }
    )
    payload = resp.json()
    if payload["finished"]:
        if payload["error"]:
            print("operation erred: "+payload["error"])
        else:
            print("operation complete.")
        break