|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "active_support" |
| 4 | +require "active_support/cache" |
| 5 | +require "active_support/notifications" |
| 6 | +require "active_support/core_ext/object/json" |
| 7 | +require "digest" |
| 8 | +require "fileutils" |
| 9 | + |
| 10 | +module ActiveSupport |
| 11 | + module Cache |
| 12 | + # A cache store implementation that stores cache entries as files |
| 13 | + # suitable for version control. Each cache entry is stored as two files: |
| 14 | + # - #{hash}.key: the full cache key |
| 15 | + # - #{hash}.value: the serialized cache value |
| 16 | + # |
| 17 | + # This store does NOT honor expiration parameters. |
| 18 | + # |
| 19 | + # Example usage: |
| 20 | + # config.cache_store = :source_control_cache_store, cache_path: "tmp/cache" |
| 21 | + class SourceControlCacheStore < Store |
| 22 | + attr_reader :cache_path |
| 23 | + |
| 24 | + # Initialize a new SourceControlCacheStore |
| 25 | + # |
| 26 | + # @param cache_path [String] The directory where cache files will be stored |
| 27 | + # @param options [Hash] Additional options (currently unused) |
| 28 | + def initialize(cache_path:, **options) |
| 29 | + super(options) |
| 30 | + @cache_path = cache_path |
| 31 | + FileUtils.mkdir_p(@cache_path) |
| 32 | + end |
| 33 | + |
| 34 | + # Clear all cache entries |
| 35 | + def clear(options = nil) |
| 36 | + if File.directory?(@cache_path) |
| 37 | + Dir.glob(File.join(@cache_path, "*")).each do |file| |
| 38 | + File.delete(file) if File.file?(file) |
| 39 | + end |
| 40 | + end |
| 41 | + true |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + # Read an entry from the cache |
| 47 | + # |
| 48 | + # @param key [String] The cache key |
| 49 | + # @param options [Hash] Options (unused) |
| 50 | + # @return [Object, nil] The cached value or nil if not found |
| 51 | + def read_entry(key, **options) |
| 52 | + hash = hash_key(key) |
| 53 | + value_file = value_path(hash) |
| 54 | + |
| 55 | + return nil unless File.exist?(value_file) |
| 56 | + |
| 57 | + value = File.read(value_file) |
| 58 | + entry = deserialize_entry(value) |
| 59 | + |
| 60 | + # Ignore expiration by creating a new entry without expiration |
| 61 | + return entry unless entry.is_a?(ActiveSupport::Cache::Entry) |
| 62 | + |
| 63 | + # Create a new entry that never expires |
| 64 | + ActiveSupport::Cache::Entry.new(entry.value, expires_in: nil) |
| 65 | + rescue StandardError |
| 66 | + # If we can't read or deserialize, treat as cache miss |
| 67 | + nil |
| 68 | + end |
| 69 | + |
| 70 | + # Write an entry to the cache |
| 71 | + # |
| 72 | + # @param key [String] The cache key |
| 73 | + # @param entry [ActiveSupport::Cache::Entry] The cache entry |
| 74 | + # @param options [Hash] Options (expiration is ignored) |
| 75 | + # @return [Boolean] Returns true on success, false on failure |
| 76 | + def write_entry(key, entry, **options) |
| 77 | + hash = hash_key(key) |
| 78 | + |
| 79 | + # Write the key file |
| 80 | + File.write(key_path(hash), key) |
| 81 | + |
| 82 | + # Write the value file |
| 83 | + File.write(value_path(hash), serialize_entry(entry, **options)) |
| 84 | + |
| 85 | + true |
| 86 | + rescue StandardError |
| 87 | + # Return false if write fails (permissions, disk space, etc.) |
| 88 | + false |
| 89 | + end |
| 90 | + |
| 91 | + # Delete an entry from the cache |
| 92 | + # |
| 93 | + # @param key [String] The cache key |
| 94 | + # @param options [Hash] Options (unused) |
| 95 | + # @return [Boolean] Returns true if any file was deleted |
| 96 | + def delete_entry(key, **options) |
| 97 | + hash = hash_key(key) |
| 98 | + key_file = key_path(hash) |
| 99 | + value_file = value_path(hash) |
| 100 | + |
| 101 | + deleted = false |
| 102 | + |
| 103 | + begin |
| 104 | + deleted = true if File.exist?(key_file) && File.delete(key_file) |
| 105 | + rescue StandardError |
| 106 | + # Ignore errors, continue trying to delete value file |
| 107 | + end |
| 108 | + |
| 109 | + begin |
| 110 | + deleted = true if File.exist?(value_file) && File.delete(value_file) |
| 111 | + rescue StandardError |
| 112 | + # Ignore errors |
| 113 | + end |
| 114 | + |
| 115 | + deleted |
| 116 | + end |
| 117 | + |
| 118 | + # Generate a hash for the given key |
| 119 | + # |
| 120 | + # @param key [String] The cache key |
| 121 | + # @return [String] The SHA256 hash of the key |
| 122 | + def hash_key(key) |
| 123 | + ::Digest::SHA256.hexdigest(key.to_s) |
| 124 | + end |
| 125 | + |
| 126 | + # Get the path for the key file |
| 127 | + # |
| 128 | + # @param hash [String] The hash of the key |
| 129 | + # @return [String] The full path to the key file |
| 130 | + def key_path(hash) |
| 131 | + File.join(@cache_path, "#{hash}.key") |
| 132 | + end |
| 133 | + |
| 134 | + # Get the path for the value file |
| 135 | + # |
| 136 | + # @param hash [String] The hash of the key |
| 137 | + # @return [String] The full path to the value file |
| 138 | + def value_path(hash) |
| 139 | + File.join(@cache_path, "#{hash}.value") |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments