Quando viene effettuata una richiesta, questa restituisce le informazioni dell’applicazione e le memorizza nella cache all’interno dell’oggetto Application stesso. Questo è utile se hai bisogno di accedere nuovamente a queste informazioni in un tempo relativamente breve, ovvero quando non vale la pena effettuare una nuova richiesta API per ottenere dati aggiornati. In questi casi, puoi accedere a Application.cache.
import squarecloud as square
client = square.Client('API_KEY')
async def example():
app = await client.app('application_id')
# Note that, as no requests have been made, * in the cache will be None
print(app.cache.logs) # None
print(app.cache.status) # None
print(app.cache.backup) # None
# Now let's make some requests
await app.logs()
await app.status()
await app.backup()
# The cache has been updated 🤯
print(app.cache.logs) # LogsData(...)
print(app.cache.status) # StatusData(...)
print(app.cache.backup) # BackupData(...)
Effettuare richieste senza aggiornare la cache
Se, per qualche motivo, non vuoi aggiornare la cache quando effettui una richiesta, puoi passare l’argomento update_cache=False.
import squarecloud as square
client = square.Client('API_KEY')
async def example():
app = await client.app('application_id')
await app.status(update_cache=False)
print(app.cache.status) # None
Se gli argomenti che passi a cache.update non sono un’istanza di StatusData, LogsData o BackupData, verrà sollevato un errore SquareException.
Svuotare manualmente la cache
Puoi svuotare manualmente la cache usando cache.clear.
import squarecloud as square
client = square.Client('API_KEY')
async def example():
app = await client.app('application_id')
await app.status()
print(app.cache.status) # StatusData(...)
app.cache.clear()
print(app.cache.status) # None
Aggiornare manualmente la cache
Puoi anche aggiornarla manualmente usando il metodo cache.update.
import squarecloud as square
client = square.Client('API_KEY')
async def example():
app = await client.app('application_id')
logs = await app.logs()
status = await app.status()
backup = await app.backup()
app.cache.clear() # Clears the cache
app.cache.update(status, logs, backup) # Updates the cache
print(app.cache.logs) # LogsData(...)
print(app.cache.status) # StatusData(...)
print(app.cache.backup) # BackupData(...)