From 85d3b9214e5fccf4b1c9c918d9d2fe4d6f75f66a Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Thu, 2 Nov 2023 20:43:59 +1100 Subject: [PATCH] Add action cache and cache now playing requests --- app/actions/recently_played/index.rb | 7 +++- app/content/pages/now.md | 10 ++--- app/queries/posts/recently_played.rb | 2 +- app/templates/layouts/app.html.slim | 6 +-- app/templates/recently_played/index.html.slim | 1 + app/views/recently_played/index.rb | 4 +- config/providers/view_cache.rb | 5 +++ lib/adamantium/view_cache/cacher.rb | 39 +++++++++++++++++++ slices/admin/views/apple_music/index.rb | 2 +- 9 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 config/providers/view_cache.rb create mode 100644 lib/adamantium/view_cache/cacher.rb diff --git a/app/actions/recently_played/index.rb b/app/actions/recently_played/index.rb index 24d3437..0f727f6 100644 --- a/app/actions/recently_played/index.rb +++ b/app/actions/recently_played/index.rb @@ -1,10 +1,13 @@ +require "time_math" + module Adamantium module Actions module RecentlyPlayed class Index < Action - include Deps["views.recently_played.index"] + include Deps["views.recently_played.index", "view_cache.cacher"] + def handle(req, res) - res.render index + res.body = cacher.call(key: "recently_played", content: index.call.to_str, expiry: TimeMath.min.advance(Time.now, +10)) end end end diff --git a/app/content/pages/now.md b/app/content/pages/now.md index 9b5cbd6..f465902 100644 --- a/app/content/pages/now.md +++ b/app/content/pages/now.md @@ -1,10 +1,10 @@ # Now -### Work +### 🏒 Work I'm currently a technical lead at [Culture Amp](https://cultureamp.com), where I lead a small team working in a Rails monolith and across many microservices. Previously I worked with the wonderful humans of [Icelab](https://icelab.com.au) on a wide range of interesting and valuable projects. -### Web +### πŸ•ΈοΈ Web I have been working on building this site to act as a repository for all my nonsense online, and as a way of encouraging me to write more! @@ -21,17 +21,17 @@ Where possible, I will try and post as much as I can here, and then syndicate ou - [Mastodon](https://social.dnitza.com/@daniel) - [Raindrop](https://raindrop.io/nitza/public-32480451) -### Life +### 🌱 Life I currently live in Canberra with my partner and [our dogs](https://instagram.com/barkly_and_crumpet). We've been here since 2015, it's great. You should visit. In my spare time I like to tinker on various Ruby projects (including the software that powers this blog), make things with [Processing](https://processing.org), explore the various [hiking trails](/hikes) around Canberra and potter around [in the garden](/tagged/garden). -### Currently listening to +### 🎧 Currently listening to
-### 2023 Goals +### πŸ₯… 2023 Goals - 25 Hikes - Tracking these as I remember on [this page](/hikes). diff --git a/app/queries/posts/recently_played.rb b/app/queries/posts/recently_played.rb index 4e41a6c..d6e887a 100644 --- a/app/queries/posts/recently_played.rb +++ b/app/queries/posts/recently_played.rb @@ -33,7 +33,7 @@ module Adamantium JWT.encode( authentication_payload, private_key, - 'ES256', + "ES256", kid: settings.apple_music_key ) end diff --git a/app/templates/layouts/app.html.slim b/app/templates/layouts/app.html.slim index d9e7464..7bf7afa 100644 --- a/app/templates/layouts/app.html.slim +++ b/app/templates/layouts/app.html.slim @@ -46,13 +46,13 @@ html a href="/" rel="me" class="u-url u-uid" h1 class="p-name uppercase text-sm md:text-sm text-gray-400 dark:text-gray-400" = Hanami.app.settings.site_name nav class="space-x-1 text-sm md:text-sm uppercase md:block" - a class="p-1 rounded hover:bg-pink-100 hover:text-pink-400 dark:hover:bg-pink-200 #{context.current_path.start_with?('/now') ? 'text-pink-600 bg-pink-50 dark:bg-pink-900 dark:text-pink-400' : 'text-gray-400'}" href="/now" Now + a class="transition-colors p-1 rounded hover:bg-pink-100 hover:text-pink-400 dark:hover:bg-pink-800 #{context.current_path.start_with?('/now') ? 'text-pink-600 bg-pink-50 dark:bg-pink-900 dark:text-pink-400' : 'text-gray-400'}" href="/now" Now span class="text-gray-400 dark:text-gray-600" = "/" - a class="p-1 rounded hover:bg-blue-100 hover:text-blue-400 dark:hover:bg-blue-200 #{context.current_path.start_with?('/post') ? 'text-blue-400 bg-blue-50 dark:bg-blue-900 dark:text-blue-400' : 'text-gray-400'}" href="/posts" Writing + a class="transition-colors p-1 rounded hover:bg-emerald-100 hover:text-emerald-400 dark:hover:bg-emerald-800 #{context.current_path.start_with?('/post') ? 'text-emerald-400 bg-emerald-50 dark:bg-emerald-900 dark:text-emerald-400' : 'text-gray-400'}" href="/posts" Writing span class="text-gray-400 dark:text-gray-600" = "/" - a class="p-1 rounded text-gray-400 hover:bg-orange-100 hover:text-orange-400 dark:hover:bg-orange-200" href="#{Hanami.app.settings.micropub_site_url}/feeds/rss" RSS + a class="transition-colors p-1 rounded text-gray-400 hover:bg-orange-100 hover:text-orange-400 dark:hover:bg-orange-800" href="#{Hanami.app.settings.micropub_site_url}/feeds/rss" RSS == yield div class="px-4 max-w-screen-md mx-auto pb-10" p class="float-left text-gray-200 dark:text-indigo-900" Β© 2023 Daniel Nitsikopoulos. All rights reserved. diff --git a/app/templates/recently_played/index.html.slim b/app/templates/recently_played/index.html.slim index 74ef5c2..ec3a979 100644 --- a/app/templates/recently_played/index.html.slim +++ b/app/templates/recently_played/index.html.slim @@ -5,5 +5,6 @@ div class="grid grid-cols-4 gap-4" img class="rounded transition-transform ease-out hover:scale-105" src="#{album[:image]}" span class="inline-block text-sm" = album[:name] + br span class="inline-block text-sm font-bold" = album[:artist] \ No newline at end of file diff --git a/app/views/recently_played/index.rb b/app/views/recently_played/index.rb index 113f9cc..afa4642 100644 --- a/app/views/recently_played/index.rb +++ b/app/views/recently_played/index.rb @@ -8,11 +8,11 @@ module Adamantium expose :recently_played_music do |recently_played_result| # raise recently_played_result["data"].inspect - JSON.parse(recently_played_result)["data"].reject{ |a| a["type"] != "albums" }.map do |album| + JSON.parse(recently_played_result)["data"].reject { |a| a["type"] != "albums" }.map do |album| { artist: album["attributes"]["artistName"], name: album["attributes"]["name"], - image: album["attributes"]["artwork"]["url"].gsub("{w}", "256").gsub("{h}", "256"), + image: album["attributes"]["artwork"]["url"].gsub("{w}", "512").gsub("{h}", "512"), href: album["attributes"]["url"] } end diff --git a/config/providers/view_cache.rb b/config/providers/view_cache.rb new file mode 100644 index 0000000..b9467dd --- /dev/null +++ b/config/providers/view_cache.rb @@ -0,0 +1,5 @@ +Hanami.app.register_provider :view_cache, namespace: true do + start do + register "cacher", Adamantium::ViewCache::Cacher.new + end +end diff --git a/lib/adamantium/view_cache/cacher.rb b/lib/adamantium/view_cache/cacher.rb new file mode 100644 index 0000000..40e25a7 --- /dev/null +++ b/lib/adamantium/view_cache/cacher.rb @@ -0,0 +1,39 @@ +require "json" + +module Adamantium + module ViewCache + class Cacher + def call(key:, content:, expiry:) + cached_content = read(key: key) + + return cached_content if cached_content + + data = JSON.generate(expire: expiry.to_i, content: content) + + path = "#{key}.json" + + File.write(File.join(Hanami.app.root, "tmp", path), data) + + content + end + + private + + def read(key:) + filename = "#{key}.json" + path = File.join(Hanami.app.root, "tmp", filename) + + return nil unless File.exist?(path) + + cached_data = JSON.parse(File.read(path)) + + if Time.strptime(cached_data["expire"].to_s, "%s") < Time.now + File.delete(path) + nil + else + cached_data["content"] + end + end + end + end +end diff --git a/slices/admin/views/apple_music/index.rb b/slices/admin/views/apple_music/index.rb index 54b34a1..e99821c 100644 --- a/slices/admin/views/apple_music/index.rb +++ b/slices/admin/views/apple_music/index.rb @@ -19,7 +19,7 @@ module Admin JWT.encode( authentication_payload, private_key, - 'ES256', + "ES256", kid: settings.apple_music_key ) end