发起请求时,它会返回应用信息并将其缓存在 Application 对象自身中。如果你需要在相对较短的时间内再次访问这些信息(也就是说不值得为获取更新数据而发起新的 API 请求),这会很有用。在这种情况下,你可以访问 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(...)
发起请求但不更新缓存
如果出于某种原因,你不希望在发起请求时更新缓存,可以传入 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
如果你传给 cache.update 的参数不是 StatusData、LogsData 或 BackupData 的实例,将会抛出 SquareException 错误。
手动清除缓存
你可以使用 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
手动更新缓存
你还可以使用 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(...)