Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMedia/Media/GlassBoundedCache.swift8// Module : SPGlassMedia9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassCore4344/// A bounded in-memory cache with least-recently-used eviction.45///46/// ```swift47/// let cache = GlassBoundedCache<String, Image>(capacity: 48)48/// await cache.insert(image, for: "photo-12")49/// let hit = await cache.value(for: "photo-12")50/// ```51///52/// Reading a value marks it as recently used. When an insertion would exceed the capacity the entry that was53/// used longest ago is dropped. Nothing is written to disk.54public actor GlassBoundedCache<Key: Hashable & Sendable, Value: Sendable> {55 /// Largest number of entries kept; at least 1.56 public nonisolated let capacity: Int57 private var storage: [Key: Value] = [:]58 /// Keys from least to most recently used.59 private var order: [Key] = []6061 public init(capacity: Int) {62 self.capacity = max(capacity, 1)63 }6465 /// The cached value, marked as recently used, or `nil`.66 public func value(for key: Key) -> Value? {67 guard let value = storage[key] else { return nil }68 touch(key)69 return value70 }7172 /// Stores a value as the most recently used entry, evicting the least recently used one if needed.73 public func insert(_ value: Value, for key: Key) {74 storage[key] = value75 touch(key)76 while order.count > capacity {77 storage[order.removeFirst()] = nil78 }79 }8081 public func removeValue(for key: Key) {82 storage[key] = nil83 order.removeAll { $0 == key }84 }8586 public func removeAll() {87 storage.removeAll()88 order.removeAll()89 }9091 public var count: Int { storage.count }9293 /// Keys from least to most recently used.94 public var keysByRecency: [Key] { order }9596 private func touch(_ key: Key) {97 order.removeAll { $0 == key }98 order.append(key)99 }100}101102/// The image cache of a ``GlassGallery``, keyed by item identifier.103public typealias GlassGalleryCache = GlassBoundedCache<String, Image>104