Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import Cryptoimport EffemModelsimport Foundation
/// Keys for the `pi_cache` table and the TTL that goes with each one.////// Keys that the Go service used unchanged keep their format so the warm/// cache carries over at cutover. Searches are normalised (trimmed,/// lowercased, spaces collapsed) and episode lists drop the requested size,/// which the Go service did not do; those entries start cold.enum CacheKeys { static let searchTTL: TimeInterval = 3600 static let podcastTTL: TimeInterval = 24 * 3600 static let episodesTTL: TimeInterval = 15 * 60 static let episodeTTL: TimeInterval = 6 * 3600 static let trendingTTL: TimeInterval = 30 * 60 static let recentTTL: TimeInterval = 15 * 60
/// The upstream size fetched for any episode list; requests are trimmed /// from this one cached copy. static let episodesUpstreamMax = 100 static let searchUpstreamMax = 100 static let trendingUpstreamMax = 100 static let recentUpstreamMax = 100
static func normalizedQuery(_ query: String) -> String { query.split(whereSeparator: \.isWhitespace).joined(separator: " ").lowercased() }
static func search(_ kind: String, query: String) -> String { "search:\(kind):\(normalizedQuery(query)):\(searchUpstreamMax)" }
static func podcast(feedID: Int64) -> String { "podcast:\(feedID)" } static func podcast(itunesID: Int64) -> String { "podcast:itunes:\(itunesID)" } /// Keyed by a digest of the address in its one spelling, so every way of /// writing a feed's address shares a copy. A digest, because the key /// column holds 512 characters and an address can run to four times /// that: under the address itself a long one could not be stored, and /// the lookup would fail every time for a feed that was found. static func podcast(feedURL: FeedAddress) -> String { let digest = SHA256.hash(data: Data(feedURL.absoluteString.utf8)) return "podcast:url:" + digest.map { String(format: "%02x", $0) }.joined() } static func episodes(feedID: Int64) -> String { "episodes:\(feedID)" } static func episodes(feedURL: String) -> String { "episodes:url:\(feedURL)" } /// What one feed has published since `since`. One copy per feed, so that /// a show many libraries hold is fetched once for all of them; `since` /// is the start of a day (see ``NewEpisodesWindow``), which is what lets /// callers who last looked at different moments share it. static func newEpisodes(feedID: Int64, since: Date) -> String { "\(newEpisodesPrefix)\(Int(since.timeIntervalSince1970)):\(feedID)" } /// What every such key begins with. static let newEpisodesPrefix = "episodes:since:" /// A single episode with its full text. The key says so, which keeps the /// answers cached before full text was asked for, cut to a hundred words, /// from being served in its place. static func episode(id: Int64) -> String { "episode:full:\(id)" } static func trending(lang: String, categories: String) -> String { "trending:\(trendingUpstreamMax):\(lang):\(categories)" } static func recentEpisodes() -> String { "recent:episodes:\(recentUpstreamMax)" }}