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