Add action cache and cache now playing requests

This commit is contained in:
2023-11-02 20:43:59 +11:00
parent e0c03ac5ae
commit 85d3b9214e
9 changed files with 62 additions and 14 deletions

View File

@@ -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

View File

@@ -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
<div hx-get="/recently_played" hx-trigger="load"></div>
### 2023 Goals
### 🥅 2023 Goals
- 25 Hikes
- Tracking these as I remember on [this page](/hikes).

View File

@@ -33,7 +33,7 @@ module Adamantium
JWT.encode(
authentication_payload,
private_key,
'ES256',
"ES256",
kid: settings.apple_music_key
)
end

View File

@@ -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.

View File

@@ -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]

View File

@@ -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

View File

@@ -0,0 +1,5 @@
Hanami.app.register_provider :view_cache, namespace: true do
start do
register "cacher", Adamantium::ViewCache::Cacher.new
end
end

View File

@@ -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

View File

@@ -19,7 +19,7 @@ module Admin
JWT.encode(
authentication_payload,
private_key,
'ES256',
"ES256",
kid: settings.apple_music_key
)
end