tidbyt / pixlet

Build apps for pixel-based displays ✨
https://tidbyt.com
Apache License 2.0
756 stars 107 forks source link

Tidbyt is not invalidating cache on device #865

Closed lurein closed 10 months ago

lurein commented 1 year ago

Hi! I have an app set to grab live data from our backend server -> on the local server (running pixlet serve), refreshing the browser page invalidates the cache and pulls the fresh stat, but on the actual tidbyt, the data stays stale from the moment the app was rendered onwards on the device, even when the app cycles through the rotation

(I instal the app on the device with a pixlet push --installation-id...

Some guidance on what's going wrong with the code below would be awesome

load("render.star", "render")
load("http.star", "http")
load("encoding/base64.star", "base64")
load("humanize.star", "humanize")

GC_MONTHLY_SPEND_URL = "https://[REDACTED]/clearedSpendThisMonth"

BTC_ICON = base64.decode("""
iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAEMUlEQVR4Ae2bwbHUMAyGHww1UAAXKIYZeuBCCdRACVxeD8xQDFwogCYAsc8zWj87chzrl7L757DJWk4k/19kJdnswwMXKkAFqAAVoAJzCryY2+2ce7359v7PSOS/PnwP0yXM8Ygwq/qMgtD+oqDcPJAZGAXM53ePZfP/+tPb1+56vbzyeGNfjsBoSfH15++hKa+172jbTQMZFaHX78uPj89M3lAI5JnksQ2vYt23va+eatpecramyxAUDCnYddHOgCgVECSMIr4FpVVHyr4e6zRAUDBaIlpQWvt4taWsIV6DbR23vreIPDEkvhQZEiVCDUMEad2ht/pJX48lBRCPgW0d06oLAqUGg4Li/ihgSxixRWWH+K5FlzZrkRtDTzh3mSFF9JmTwROGxBWaITOCFDFXrvVVlrfgVtwEohTKACYMyGh2aJGUdlObVjGXg2p/Edly1zWkRXUEWmu/VW0EsqGk96P2lmsCaakS2Ha6RydH5vXRuhXII8ejk0gBLN/oaYtTlkUEbCcQsOCWOwKxFALbCQQsuOWOQCyFwHYCAQtuuSMQSyGwnUDAglvuCMRSCGwnELDgljsCsRQC2wkELLjljkAshcB2AgELbrkjEEshsD3sJQcZZ9YfjPSLDhLnkR/FZP89S2iGzLw5uGdwZ+wbCuSMgnnHHA6EWXKNOBzIdTjx3+r6gY6IQAzFkQVdQiEQBaTODjSMFECyXPrWMBQn6Obd34f0QERkh5APA9LLjJ5AyNM0CoaMMaSG9GAgRe/5ioQRAoQweqfCpR36svUMjOgzdlu+9daQKWv9MG7niDAgM9lxOzKPjwQGZDyk++4Juew9Q3bUl9tRtYsZ8pSQ9Z895Y866D/rSCjMkMYMGZktBNIAUpo0GNQUximrqG+sUdMXgWyAqOvKRtdlJgIxpERDIRADCNpMIGjFDX8EYgiENkOe9sqrPnvv1vUlJ0IUdK3ojYkZ0lMmqB0GhC/EjRGGAZFwCMWGAqkhOoyZeqL393qEsbfG6ZhWbkMzZGXgt3osAklGNi2Q3mWox0O+LNOVnBtpgUhwPShiW7VkgiFjghb1mcEXKB43ijPxrDoResdx/4Fq9aALmKNXW3viWuWzB0G3u05Zewatg9raLhmz1ceyecRl+Ry1Q6es0aCsfrNQMoMoY3abslCDH7n7PxJLma5EsKPTZBF9a33KDNED0mLXcLRN75N5+/RAtLhnBKDjl23Xol4743dbAQLZ0AhdPyQUAukA0TA6XVyaCaQhaw0DcXVVwiCQosTTuoZRmd2/ut2HSORnu+ppwUBmh2jGDBEV/i0tGBcL9tM1Q8pQsmdKDwY6O0Svu8+QTDAECCRDPH7lk+C9lojMKGOBABFnZ4ESCUN0ggERZ7JkBhMNQ/SBAxGnsmQCkwHERZVAIFmgZIJRoHBNBahAT4G/etYDAmtioMcAAAAASUVORK5CYII=
""")

def main():
    rep = http.get(GC_MONTHLY_SPEND_URL, ttl_seconds = 240) # cache for 4 minutes
    if rep.status_code != 200:
        fail("GC request failed with status %d", rep.status_code)
        total = 0
    total = rep.json()["spend"]

    # for development purposes: check if result was served from cache or not
    if rep.headers.get("Tidbyt-Cache-Status") == "HIT":
        print("Hit! Displaying cached data.")
    else:
        print("Miss! Calling GC API.")

    formattedTotal = humanize.comma(total)

    return render.Root(
        child = render.Column(
                children = [
                render.Row(
                    expanded=True,
                    main_align="space_evenly",
                    cross_align="center",
                    children = [
                        render.Image(src=BTC_ICON, width=17, height=17),
                        render.Text("$%s" % formattedTotal),
                    ],
                ),
                render.Row(
                    expanded = True,
                    main_align = "space_evenly",
                    cross_align = "end",
                    children = [
                        render.Text(content = "Monthly Spend", offset = -1),
                    ],
                ),
            ],
            main_align = "center",
            cross_align = "center",
            expanded = True,
        ),
    )
jmanske commented 11 months ago

pixlet push does not push an "app" to the device, it pushes a static webp image to the device.

"Keep in mind though, the graphic associated with the installation will only change if you push a new one. If you really want to stay up to speed on the price of Bitcoin, you'll have to either keep pushing the graphic, or look into publishing your app. =)" https://tidbyt.dev/docs/integrate/pushing-apps

lurein commented 10 months ago

thank you!!! closing this!